Skip to content

Comments

Type safe error handling in kotlin spring server with useSealedResponseInterfaces flag#23003

Merged
wing328 merged 3 commits intoOpenAPITools:masterfrom
louislepper:add-type-safe-returns
Feb 24, 2026
Merged

Type safe error handling in kotlin spring server with useSealedResponseInterfaces flag#23003
wing328 merged 3 commits intoOpenAPITools:masterfrom
louislepper:add-type-safe-returns

Conversation

@louislepper
Copy link
Contributor

@louislepper louislepper commented Feb 18, 2026

Summary

Adds useSealedResponseInterfaces option to the Kotlin Spring generator to provide type-safe response handling for operations with multiple response schemas.

What it does

When enabled, generates sealed interfaces for each operation's possible response types:

// Generated sealed interface
sealed interface GetUserByNameResponse

// Models implement the interface
data class User(...) : GetUserByNameResponse
data class UserNotFound(...) : GetUserByNameResponse

// API uses sealed interface for type safety
interface UserApi {
    fun getUserByName(username: String): ResponseEntity<GetUserByNameResponse>
}

Key benefits

  • Type safety: Compile-time guarantee that only valid response types can be returned
  • Interface-only mode: Ensures implementations match the OpenAPI specification
  • Multiple response schemas: Properly handles operations with different 2xx/4xx response types

Implementation notes

  • Sealed interfaces are generated in the model package (same package requirement for Kotlin)
  • Only generates sealed interfaces for operations with actual response content (skips void responses)
  • Handles duplicate prevention when same model appears in multiple status codes
  • Fallback to standard return types (Unit) for operations without response content

Resolves #23004, resolves #1096. Note that #1096 was closed after an option to switch to ResponseEntity<*> was added, but this is not type safe - any response can be returned from an endpoint with ResponseEntity<*> - they no longer need to match the API yaml.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • 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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • 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

Adds an opt-in feature for Kotlin Spring to generate sealed response interfaces per operation, letting controllers return type-safe responses. Enable useSealedResponseInterfaces to have models implement Response and API methods return that interface; operations without response bodies keep normal Unit/standard returns.

  • New Features

    • New option: useSealedResponseInterfaces (default false).
    • Generates SealedResponseInterfaces.kt in the model package with one sealed interface per operation that has response content.
    • Response models implement the right interfaces; API methods return them and import them (works with ResponseEntity).
    • Updated templates (apiInterface.mustache, dataClass.mustache, sealedResponseInterfaces.mustache), docs, and a petstore sample/config.
    • Added sealed-interfaces petstore sample to CI workflow to build/test with Kotlin server samples (JDK 17).
  • Bug Fixes

    • Skip sealed interfaces for operations with no response content; methods keep normal/Unit returns.
    • Prevent duplicate interface implementations and imports when a model appears in multiple responses.
    • API templates use the sealed interface only when present; otherwise use the standard return type.
    • Tests added for enabled/disabled behavior, duplicates, and void responses.

Written for commit b68eacf. Summary will update on new commits.

@louislepper louislepper force-pushed the add-type-safe-returns branch from 809b1df to c674e09 Compare February 18, 2026 06:18
@louislepper louislepper changed the title Add type safe returns Type safe error handling in kotlin spring server with useSealedResponseInterfaces flag Feb 22, 2026
@louislepper
Copy link
Contributor Author

@dennisameling / @stefankoppier / @e5l / @4brunu - could I grab a review from one or more of you?

Thanks

@louislepper
Copy link
Contributor Author

cc other technical committee members: @andrewemery / @Zomzog / @karismann

Copy link
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.

1 issue found across 32 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them. If appropriate, use sub-agents to investigate and fix each issue separately.


<file name="samples/server/petstore/kotlin-spring-sealed-interfaces/src/main/kotlin/org/openapitools/api/PetApi.kt">

<violation number="1" location="samples/server/petstore/kotlin-spring-sealed-interfaces/src/main/kotlin/org/openapitools/api/PetApi.kt:146">
P2: Optional @RequestPart is declared with a non-nullable MultipartFile. When the file part is omitted (allowed by required=false), Spring provides null, which conflicts with Kotlin non-nullable type and can cause binding/NPE errors. Make the parameter nullable or mark it required.</violation>
</file>

Since this is your first cubic review, here's how it works:

  • cubic automatically reviews your code and comments on bugs and improvements
  • Teach cubic by replying to its comments. cubic learns from your replies and gets better over time
  • Add one-off context when rerunning by tagging @cubic-dev-ai with guidance or docs links (including llms.txt)
  • Ask questions if you need clarification on any suggestion

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@wing328 wing328 merged commit b32faa0 into OpenAPITools:master Feb 24, 2026
63 checks passed
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] Add type-safe response handling with sealed interfaces for multiple status codes [Spring] can't handle multiple responses

2 participants