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

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

Copy link
Copy Markdown

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, fixing polymorphism and compile errors. Discriminators are declared on the interface only when subtypes override them, and oneOf members now implement the interface even without a discriminator.

  • Bug Fixes
    • Enable useOneOfInterfaces = true in KotlinServerCodegen; add oneof_interface.mustache and route via model.mustache and libraries/jaxrs-spec/model.mustache.
    • Render sealed interfaces with Jackson annotations and declare the discriminator only when fixJacksonJsonTypeInfoInheritance makes subtypes override it.
    • Ensure oneOf members implement the sealed interface via x-implements, including discriminator-less oneOf.
    • Only emit parent constructor args for sealed classes; skip x-parent-ctor-args for interfaces in data_class.mustache.
    • In libraries/jaxrs-spec var templates, emit override for inherited properties; update tests and samples to expect sealed interfaces (no typealias).

Written for commit 9056b41. Summary will update on new commits.

Review in cubic

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

Copy link
Copy Markdown
Contributor

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

@mcuk-globalid
mcuk-globalid force-pushed the fix/kotlin-server-oneOf-interface branch from f2d564f to 875f200 Compare May 26, 2026 10:10
@wing328 wing328 added this to the 7.23.0 milestone Jun 3, 2026
@wing328

wing328 commented Jun 3, 2026

Copy link
Copy Markdown
Member

thanks for the pr

cc @karismann (2019/03) @Zomzog (2019/04) @andrewemery (2019/10) @4brunu (2019/11) @yutaka0m (2020/03) @stefankoppier (2022/06) @e5l (2024/10) @dennisameling (2026/02)

@wing328

wing328 commented Jun 3, 2026

Copy link
Copy Markdown
Member

please review the build failure when you've time

@wing328 wing328 removed this from the 7.23.0 milestone Jun 8, 2026
Matjaz Cuk added 2 commits July 30, 2026 10:07
The kotlin-server generator produces `typealias Foo = Any` for oneOf schemas
with discriminators instead of a proper type hierarchy. This causes compile
errors when subtypes try to extend the parent type.

Root cause: KotlinServerCodegen declares SchemaSupportFeature.oneOf but does
not set useOneOfInterfaces = true, so the codegen pipeline never produces the
x-is-one-of-interface vendor extension.

Fix (mirrors OpenAPITools#23574 for kotlin-spring):
- Set useOneOfInterfaces = true in KotlinServerCodegen constructor
- Add oneof_interface.mustache template for sealed interface with Jackson annotations
- Update model.mustache to route x-is-one-of-interface models to the new template
- Update jaxrs-spec model.mustache for the same routing
- Fix data_class.mustache: only emit constructor args when x-parent-ctor-args is present
- Skip x-parent-ctor-args when useOneOfInterfaces is true (interfaces have no constructors)
- Update existing tests to expect sealed interface instead of sealed class for oneOf
- Regenerate affected samples
Only declare the discriminator on the oneOf sealed interface when the
subtypes are actually rendered with 'override' (i.e. when
fixJacksonJsonTypeInfoInheritance is enabled). Otherwise the subtypes
failed to compile with "'petType' hides member of supertype 'Pet' and
needs an 'override' modifier".

Emit 'override' for inherited properties in the jaxrs-spec library
templates, which hit the same error.

Let oneOf members declare the sealed interface as a supertype via
x-implements, so a discriminator-less oneOf no longer produces an
empty interface that nothing implements.
@mcuk-globalid
mcuk-globalid force-pushed the fix/kotlin-server-oneOf-interface branch from 875f200 to 9056b41 Compare July 30, 2026 08:51
@mcuk-globalid

Copy link
Copy Markdown
Author

@wing328 pushed update

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.

2 participants