Skip to content

fix(codegen): respect optional request bodies#5326

Open
MahinAnowar wants to merge 1 commit into
reduxjs:masterfrom
MahinAnowar:fix/codegen-optional-request-body
Open

fix(codegen): respect optional request bodies#5326
MahinAnowar wants to merge 1 commit into
reduxjs:masterfrom
MahinAnowar:fix/codegen-optional-request-body

Conversation

@MahinAnowar

Copy link
Copy Markdown

Fixes #4824.

Problem

rtk-query-codegen-openapi hardcodes required: true for the request-body arg:

queryArg[name] = {
  origin: 'body',
  // ...
  required: true, // <- always required
  body,
};

As a result, every operation with a request body generates a non-optional arg (body: T), even when the OpenAPI document does not mark the body as required. Per the OpenAPI 3.x spec, requestBody.required defaults to false, so an un-required body should produce an optional arg (body?: T). Query and path params already respect this correctly via param.required; only the body branch was wrong.

Fix

One line — use the resolved request body's own required flag, defaulting to the spec default:

required: body.required ?? false,

The required value flows into createQuestionToken(!def.required) (and the flat-arg | undefined branch), so the generated property becomes optional when appropriate.

Tests

Added test/fixtures/issue-4824.json (a deleteFoo op with a non-required body and a createBar op with required: true) plus a regression test in generateEndpoints.test.ts:

  • non-required body → DeleteFooApiArg = { body?: … }
  • required: true body → CreateBarApiArg = { body: … } (stays required)

The test fails on the current code (the non-required body is emitted as body:) and passes with the fix.

Snapshot updates

The existing snapshots captured the buggy output, so 15 updated. Every changed line is a request-body arg becoming optional (user: Useruser?: User, order: Orderorder?: Order, body: Blobbody?: Blob, etc.). Notably, petstore's pet body — which is marked required: true — stays pet: Pet, confirming the change only affects bodies the spec leaves optional.

The full package test suite (yarn test, incl. typecheck) passes and Prettier is clean.

The request-body arg was hardcoded to `required: true`, so every operation
with a request body generated a non-optional arg (`body: T`) even when the
OpenAPI spec did not mark the body as required. Per the spec, `requestBody.required`
defaults to `false`, so these args should be optional (`body?: T`).

Use `body.required ?? false` instead, mirroring how query/path params already
use `param.required`. Snapshots update accordingly: bodies not marked
`required: true` now become optional, while bodies with `required: true`
(e.g. petstore's `pet`) stay required.

Fixes reduxjs#4824
@codesandbox

codesandbox Bot commented Jul 1, 2026

Copy link
Copy Markdown

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@netlify

netlify Bot commented Jul 1, 2026

Copy link
Copy Markdown

Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit 9234ed2
🔍 Latest deploy log https://app.netlify.com/projects/redux-starter-kit-docs/deploys/6a44e168fb852900081a5137
😎 Deploy Preview https://deploy-preview-5326--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@pkg-pr-new

pkg-pr-new Bot commented Jul 1, 2026

Copy link
Copy Markdown

Open in StackBlitz

@reduxjs/rtk-codemods

npm i https://pkg.pr.new/@reduxjs/rtk-codemods@9234ed2 -D
yarn add https://pkg.pr.new/@reduxjs/rtk-codemods@9234ed2.tgz -D
pnpm add https://pkg.pr.new/@reduxjs/rtk-codemods@9234ed2.tgz -D
bun add https://pkg.pr.new/@reduxjs/rtk-codemods@9234ed2.tgz -D

@rtk-query/codegen-openapi

npm i https://pkg.pr.new/@rtk-query/codegen-openapi@9234ed2 -D
yarn add https://pkg.pr.new/@rtk-query/codegen-openapi@9234ed2.tgz -D
pnpm add https://pkg.pr.new/@rtk-query/codegen-openapi@9234ed2.tgz -D
bun add https://pkg.pr.new/@rtk-query/codegen-openapi@9234ed2.tgz -D

@rtk-query/graphql-request-base-query

npm i https://pkg.pr.new/@rtk-query/graphql-request-base-query@9234ed2 -D
yarn add https://pkg.pr.new/@rtk-query/graphql-request-base-query@9234ed2.tgz -D
pnpm add https://pkg.pr.new/@rtk-query/graphql-request-base-query@9234ed2.tgz -D
bun add https://pkg.pr.new/@rtk-query/graphql-request-base-query@9234ed2.tgz -D

@reduxjs/toolkit

npm i https://pkg.pr.new/@reduxjs/toolkit@9234ed2 -D
yarn add https://pkg.pr.new/@reduxjs/toolkit@9234ed2.tgz -D
pnpm add https://pkg.pr.new/@reduxjs/toolkit@9234ed2.tgz -D
bun add https://pkg.pr.new/@reduxjs/toolkit@9234ed2.tgz -D

commit: 9234ed2

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

Successfully merging this pull request may close these issues.

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

1 participant