Skip to content

[kotlin-server] Enable oneOf sealed interface generation#23846

Open
mcuk-globalid wants to merge 2 commits into
OpenAPITools:masterfrom
mcuk-globalid:fix/kotlin-server-oneOf-interface
Open

[kotlin-server] Enable oneOf sealed interface generation#23846
mcuk-globalid wants to merge 2 commits into
OpenAPITools:masterfrom
mcuk-globalid:fix/kotlin-server-oneOf-interface

Conversation

@mcuk-globalid
Copy link
Copy Markdown

@mcuk-globalid mcuk-globalid commented May 22, 2026

Summary

The kotlin-server generator produces typealias Foo = Any for oneOf schemas with discriminators instead of a sealed interface. This causes compile errors when subtypes (generated via allOf) try to extend the parent type.

Reproducer: any OpenAPI 3.1.x spec with oneOf + discriminator + allOf subtype pattern (e.g. src/test/resources/3_1/polymorphism-and-discriminator.yaml) generates:

typealias Pet = Any

instead of:

sealed interface Pet { ... }

Subtypes then fail to compile because Any has a constructor that cannot be delegated to.

Root cause: KotlinServerCodegen declares SchemaSupportFeature.oneOf and SchemaSupportFeature.Polymorphism but does not set useOneOfInterfaces = true, so the codegen pipeline never produces the x-is-one-of-interface vendor extension. The model.mustache template also lacks the routing to a oneof_interface template.

Fix

Mirrors #23574 (kotlin-spring):

  1. Set useOneOfInterfaces = true in KotlinServerCodegen constructor
  2. Add oneof_interface.mustache template for kotlin-server (sealed interface with Jackson @JsonTypeInfo / @JsonSubTypes annotations)
  3. Update model.mustache to route x-is-one-of-interface models to the new template

Related issues

🤖 Generated with Claude Code


Summary by cubic

Generate sealed interfaces for oneOf schemas in kotlin-server instead of typealias Any. Restores correct polymorphism and fixes compile errors for allOf subtypes.

  • Bug Fixes
    • Enable useOneOfInterfaces = true in KotlinServerCodegen.
    • Add oneof_interface.mustache to render a sealed interface with Jackson @JsonTypeInfo/@JsonSubTypes.
    • Route x-is-one-of-interface in both model.mustache and libraries/jaxrs-spec/model.mustache.
    • Emit parent constructor args only when needed (data_class.mustache); skip when generating interfaces.
    • Update tests to expect sealed interfaces and use TestUtils.parseSpec; regenerate affected samples.

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

The kotlin-server generator produces `typealias Foo = Any` for
`oneOf` schemas with discriminators instead of a sealed interface,
which causes compile errors when subtypes try to extend it.

The fix mirrors PR OpenAPITools#23574 (kotlin-spring): set `useOneOfInterfaces = true`
in the constructor so the codegen pipeline emits the correct model
metadata, and add an `oneof_interface.mustache` template that renders
a sealed interface with Jackson annotations.

Fixes the regression introduced in 7.20.0 where kotlin-server specs
using `oneOf` + `discriminator` + `allOf` subtype patterns stopped
compiling.
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 4 files

Re-trigger cubic

- Fix data_class.mustache: only emit constructor args when x-parent-ctor-args is present
  (interface parents don't have constructors)
- Fix KotlinServerCodegen: skip x-parent-ctor-args when useOneOfInterfaces is true
- Update jaxrs-spec model.mustache to route x-is-one-of-interface through oneof_interface
- Update existing tests to expect sealed interface instead of sealed class for oneOf
- Fix test to use TestUtils.parseSpec() instead of unavailable OpenAPIParser
- Regenerate affected samples
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.

1 participant