fix(codegen): respect optional request bodies#5326
Open
MahinAnowar wants to merge 1 commit into
Open
Conversation
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
Review or Edit in CodeSandboxOpen the branch in Web Editor • VS Code • Insiders |
✅ Deploy Preview for redux-starter-kit-docs ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
@reduxjs/rtk-codemods
@rtk-query/codegen-openapi
@rtk-query/graphql-request-base-query
@reduxjs/toolkit
commit: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4824.
Problem
rtk-query-codegen-openapihardcodesrequired: truefor the request-body arg: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.requireddefaults tofalse, so an un-required body should produce an optional arg (body?: T). Query and path params already respect this correctly viaparam.required; only the body branch was wrong.Fix
One line — use the resolved request body's own
requiredflag, defaulting to the spec default:The
requiredvalue flows intocreateQuestionToken(!def.required)(and the flat-arg| undefinedbranch), so the generated property becomes optional when appropriate.Tests
Added
test/fixtures/issue-4824.json(adeleteFooop with a non-required body and acreateBarop withrequired: true) plus a regression test ingenerateEndpoints.test.ts:DeleteFooApiArg = { body?: … }required: truebody →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: User→user?: User,order: Order→order?: Order,body: Blob→body?: Blob, etc.). Notably, petstore'spetbody — which is markedrequired: true— stayspet: 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.