Skip to content

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

Open
@ashharrison90

Description

@ashharrison90

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! 🙇

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions