Skip to content

fix(kotlin-spring): preserve 'default' response code in postProcessOperationsWithModels#23833

Open
mdemblani wants to merge 4 commits into
OpenAPITools:masterfrom
mdemblani:fix/kotlin-spring-default-response-code
Open

fix(kotlin-spring): preserve 'default' response code in postProcessOperationsWithModels#23833
mdemblani wants to merge 4 commits into
OpenAPITools:masterfrom
mdemblani:fix/kotlin-spring-default-response-code

Conversation

@mdemblani
Copy link
Copy Markdown

@mdemblani mdemblani commented May 20, 2026

Description

Fixes #17445

When an OpenAPI spec defines a default response (a catch-all with no specific HTTP status code), the generator internally represents it with code = "0". The existing logic in KotlinSpringServerCodegen.postProcessOperationsWithModels unconditionally mapped "0""200", causing every generated @ApiResponse annotation for default responses to incorrectly use responseCode = "200" instead of responseCode = "default".

Root cause

// Before (line 1607)
if ("0".equals(resp.code)) {
    resp.code = "200";  // wrong for isDefault responses
}

Fix

Check resp.isDefault before mapping — only fall back to "200" for non-default zero-coded responses:

// After
if ("0".equals(resp.code)) {
    resp.code = resp.isDefault ? "default" : "200";
}

This mirrors the fix applied to SpringCodegen (Java) in #14399.

Generated code: before vs after

Before:

@ApiResponse(responseCode = "200", description = "Unexpected error", content = [Content(schema = Schema(implementation = Error::class))])

After:

@ApiResponse(responseCode = "default", description = "Unexpected error", content = [Content(schema = Schema(implementation = Error::class))])

Checklist

  • The fix is consistent with the approach taken in [Java][Spring] fix default response code #14399 for SpringCodegen
  • Change is minimal and surgical — one line in postProcessOperationsWithModels
  • Does not affect responses with explicit HTTP status codes

Summary by cubic

Preserves the OpenAPI default response in the Kotlin Spring generator without mutating response codes. @ApiResponse now renders responseCode = "default" while numeric codes are kept elsewhere to avoid crashes with useResponseEntity = false.

  • Bug Fixes
    • Keep resp.code numeric in KotlinSpringServerCodegen.postProcessOperationsWithModels; emit "default" in swagger2 annotations via {{#isDefault}} in api.mustache and apiInterface.mustache.
    • No changes needed to SpringHttpStatusLambda or swagger1 templates; they continue using numeric codes.
    • Add defaultResponseCodeRenderedAsDefault test to verify "default" is rendered, explicit 200 stays numeric, and "0" never appears.
    • Regenerated Kotlin Spring petstore samples.

Written for commit aabbc52. Summary will update on new commits. Review in cubic

…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 OpenAPITools#17445
Copilot AI review requested due to automatic review settings May 20, 2026 14:29
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 1 file

Re-trigger cubic

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to fix the Kotlin Spring server generator so OpenAPI default responses are emitted as @ApiResponse(responseCode = "default") instead of incorrectly being rewritten to "200" during postProcessOperationsWithModels.

Changes:

  • Update KotlinSpringServerCodegen.postProcessOperationsWithModels to map internal response code == "0" to "default" when resp.isDefault is true, otherwise keep the "200" fallback.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

mdemblani and others added 3 commits May 21, 2026 01:49
…e code fix

Updates 8 kotlin-spring sample files to reflect the fix for issue OpenAPITools#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 OpenAPITools#17445)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@wing328
Copy link
Copy Markdown
Member

wing328 commented May 23, 2026

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Kotlin][Spring] Response code 200 generated for default responses

3 participants