Skip to content

Mdemblani fix/kotlin spring default response code#23969

Closed
wing328 wants to merge 6 commits into
masterfrom
mdemblani-fix/kotlin-spring-default-response-code
Closed

Mdemblani fix/kotlin spring default response code#23969
wing328 wants to merge 6 commits into
masterfrom
mdemblani-fix/kotlin-spring-default-response-code

Conversation

@wing328

@wing328 wing328 commented Jun 8, 2026

Copy link
Copy Markdown
Member

PR checklist

  • Read the contribution guidelines.
  • Run the following to build the project and update samples:
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

Summary by cubic

Fixes incorrect rendering of OpenAPI default responses in Kotlin Spring by emitting responseCode="default" in Swagger v2 @ApiResponse. This corrects generated docs and prevents crashes when useResponseEntity=false.

  • Bug Fixes
    • Update api.mustache and apiInterface.mustache to render responseCode = "default" when isDefault is true; keep numeric codes otherwise.
    • Add regression test to verify "default" is emitted, explicit 200s remain, and "0" never appears; also confirms no crash with useResponseEntity=false.
    • Regenerate Kotlin Spring Petstore samples to reflect the fix.
    • Addresses [Kotlin][Spring] Response code 200 generated for default responses #17445.

Written for commit 4f48003. Summary will update on new commits.

Review in cubic

mdemblani and others added 6 commits May 20, 2026 18:28
…erationsWithModels

When an OpenAPI spec defines a 'default' response (no specific status code),
the codegen internally represents it as code '0'. The existing logic
unconditionally mapped '0' -> '200', causing generated @apiresponse annotations
to incorrectly use responseCode = "200" instead of responseCode = "default".

Fix: check resp.isDefault before mapping; only fall back to '200' for
non-default zero-coded responses.

Fixes #17445
…e code fix

Updates 8 kotlin-spring sample files to reflect the fix for issue #17445:
createUser, createUsersWithArrayInput, createUsersWithListInput, and logoutUser
operations in the petstore spec have `default` responses; their @apiresponse
annotations now correctly use responseCode = "default" instead of "200".

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…pringHttpStatusLambda

SpringHttpStatusLambda threw IllegalArgumentException for the OpenAPI
'default' response code string, crashing generation for any kotlin-spring
config that uses useResponseEntity=false (which emits @ResponseStatus via
the lambda). Fix by mapping "default" → HttpStatus.OK, matching the
semantic intent of an unspecified fallback response.

Also guard the two other places that embed response.code as a literal:
- returnValue.mustache: HttpStatus.valueOf({{code}}) becomes
  HttpStatus.valueOf(200) for default responses (keeps existing samples)
- api.mustache / apiInterface.mustache swagger1 ApiResponse(code=...):
  code is an int, so default responses fall back to 200

Fixes: generateHttpInterface, generateHttpInterfaceReactiveWithCoroutines,
generateHttpInterfaceReactiveWithReactor, nonReactiveWithoutResponseEntity,
reactiveWithoutResponseEntity test failures.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…tating resp.code

Per reviewer feedback, avoid mutating resp.code to the non-numeric string
"default" in postProcessOperationsWithModels. That change required cascading
fixes to SpringHttpStatusLambda, returnValue.mustache, and swagger1 templates
because resp.code feeds into @ResponseStatus and HttpStatus.valueOf() calls
that require a numeric status code.

Cleaner approach: keep resp.code = "200" (the parent's behaviour) and emit
responseCode = "default" in the swagger2 @apiresponse annotation by testing
resp.isDefault directly in the Mustache template:

  ApiResponse(responseCode = "{{#isDefault}}default{{/isDefault}}{{^isDefault}}{{{code}}}{{/isDefault}}", ...)

This is analogous to how the Java Spring generator handles default responses.
With resp.code remaining numeric, SpringHttpStatusLambda (@ResponseStatus),
returnValue.mustache (HttpStatus.valueOf), and swagger1 ApiResponse(code=...)
all continue to work without modification.

Also reverts the unnecessary SpringHttpStatusLambda "default" case, returnValue
isDefault guard, swagger1 isDefault guards, and SpringHttpStatusLambdaTest
"default" entry added in the previous commit.

Adds a focused regression test (defaultResponseCodeRenderedAsDefault) that:
- verifies ApiResponse(responseCode = "default") for operations with a default
  response (resolves reviewer comment 2)
- verifies ApiResponse(responseCode = "200") for explicit-200 responses
- verifies "0" never appears in generated output
- runs with useResponseEntity=false to confirm no crash (covers issue #17445)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@wing328 wing328 closed this Jun 8, 2026
@wing328 wing328 deleted the mdemblani-fix/kotlin-spring-default-response-code branch June 8, 2026 07:48
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.

2 participants