Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rtk-query-codegen-openapi generates incorrect (required) types for optional request body #4824

Open
ashharrison90 opened this issue Jan 21, 2025 · 0 comments

Comments

@ashharrison90
Copy link

Example OpenAPI schema:

{
  "openapi": "3.0.0",
  "info": {
    "title": "Example"
  },
  "paths": {
    "/Foo/{name}": {
      "delete": {
        "tags": [
          "Foo"
        ],
        "operationId": "deleteFoo",
        "parameters": [],
        "requestBody": {
          "content": {
            "*/*": {
              "schema": {
                "$ref": "#/components/schemas/deleteOptions"
              }
            }
          }
        }
      },
      "parameters": [
        {
          "name": "name",
          "in": "path",
          "description": "name of the Foo",
          "required": true,
          "schema": {
            "type": "string",
            "uniqueItems": true
          }
        }
      ]
    }
  },
  "components": {
    "schemas": {
      "deleteOptions": {
        "type": "object",
        "properties": {
          "apiVersion": {
            "type": "string"
          }
        }
      }
    }
  }
}

According to the spec, request bodies are optional by default. But the generated types look like:

import { api } from './factory';
export const addTagTypes = ['Foo'] as const;
const injectedRtkApi = api
  .enhanceEndpoints({
    addTagTypes,
  })
  .injectEndpoints({
    endpoints: (build) => ({
      deleteFoo: build.mutation<DeleteFooApiResponse, DeleteFooApiArg>({
        query: (queryArg) => ({ url: `/Foo/${queryArg.name}`, method: 'DELETE', body: queryArg.deleteOptions }),
        invalidatesTags: ['Foo'],
      }),
    }),
    overrideExisting: false,
  });
export { injectedRtkApi as generatedApi };
export type DeleteFooApiResponse = unknown;
export type DeleteFooApiArg = {
  name: string;
  deleteOptions: DeleteOptions;
};
export type DeleteOptions = {
  apiVersion?: string;
};

Note how deleteOptions is required instead of optional in the DeleteFooApiArg type.

Apologies in advance if I've misread the spec/misunderstood something here! 🙇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant