Skip to content

[kotlin-server] Add configurable inheritance modes#24090

Open
rm127 wants to merge 3 commits into
OpenAPITools:masterfrom
rm127:feat/kotlin-server-inheritance-mode
Open

[kotlin-server] Add configurable inheritance modes#24090
rm127 wants to merge 3 commits into
OpenAPITools:masterfrom
rm127:feat/kotlin-server-inheritance-mode

Conversation

@rm127

@rm127 rm127 commented Jun 22, 2026

Copy link
Copy Markdown

Summary

This change adds configurable inheritance handling to the kotlin-server generator, including jaxrs-spec.

Fixes

Added option

  • inheritanceMode
    • none
    • sealed
    • abstract
    • composition

Default behavior

  • ktor / javalin* default to sealed
  • jaxrs-spec defaults to abstract

Validation

  • Reject invalid inheritanceMode values
  • Reject jaxrs-spec + sealed

Implementation details

Codegen option plumbing

In KotlinServerCodegen:

  • added inheritanceMode state
  • registered the new CLI/additional-property option
  • added mode validation and library-specific defaults
  • exposed template flags:
    • x-inheritance-mode-none
    • x-inheritance-mode-sealed
    • x-inheritance-mode-abstract
    • x-inheritance-mode-composition

Model post-processing

Added mode-aware model shaping in postProcessAllModels:

  • none
    • flattens inheritance/discriminator metadata
    • removes parent linkage and inherited flags
  • composition
    • flattens inheritance first
    • replaces discriminator owners with a wrapper model containing:
      • value: kotlin.Any
  • existing inheritance processing remains active for sealed / abstract

Template updates

Shared kotlin-server model template

  • discriminator parents now render as:
    • sealed class in sealed mode
    • abstract class in abstract mode

jaxrs-spec model templates

  • discriminator parents can render as abstract class instead of interface
  • inherited properties now emit override
  • child classes forward inherited constructor args via x-parent-ctor-args

Tests

Added/updated coverage in KotlinServerCodegenTest for:

  • default sealed mode for ktor
  • default abstract mode for jaxrs-spec
  • invalid mode rejection
  • jaxrs-spec + sealed rejection
  • acceptance of composition
  • acceptance of none
  • javalin abstract-mode parent rendering
  • javalin flat-model rendering in none
  • javalin wrapper-parent generation in composition
  • jaxrs-spec abstract parent + override child rendering

Documentation

Updated docs/generators/kotlin-server.md with the new inheritanceMode option and mode descriptions.

Verification

Tested with:

./mvnw -pl modules/openapi-generator -am -Djacoco.skip=true -Dtest=org.openapitools.codegen.kotlin.KotlinServerCodegenTest -Dsurefire.failIfNoSpecifiedTests=false test

## Addresses
- https://github.com/OpenAPITools/openapi-generator/issues/11552


<!-- This is an auto-generated description by cubic. -->
---
## Summary by cubic
Adds configurable inheritance modes to `kotlin-server` (including `jaxrs-spec`) to control model generation: `none`, `sealed`, `abstract`, or `composition`. Sets sensible library defaults, updates templates, validates options, and adds tests.

- **New Features**
  - New option: `inheritanceMode` with values `none`, `sealed`, `abstract`, `composition`.
  - Defaults: `ktor`/`javalin5`/`javalin6``sealed`; `jaxrs-spec``abstract`. Rejects invalid values and `jaxrs-spec + sealed`.
  - Mode behavior:
    - `none`: flattens inheritance/discriminator metadata.
    - `composition`: flattens and generates wrapper parents with `value: kotlin.Any`.
    - `sealed`/`abstract`: preserves inheritance; discriminator parents render as `sealed class` or `abstract class`.
  - Templates:
    - Shared: discriminator parents switch between `sealed` and `abstract` by mode via `x-inheritance-mode-*`.
    - `jaxrs-spec`: parents can be `abstract class`; inherited properties emit `override`; children pass base ctor args via `x-parent-ctor-args`.
  - Exposes template flags: `x-inheritance-mode-none|sealed|abstract|composition`.
  - Docs updated; tests cover defaults, validation, and rendering across modes (including `javalin` none/abstract/composition and `jaxrs-spec` abstract with `override`).

- **Migration**
  - `jaxrs-spec` now defaults to abstract base classes for discriminator parents. Update implementations to extend the abstract class and `override` inherited properties. Use `inheritanceMode=none` for flat models or `inheritanceMode=composition` for wrapper-style polymorphism.

<sup>Written for commit 7facfa299e95bfd339d799e84d7df833a72b1e29. Summary will update on new commits.</sup>

<a href="https://cubic.dev/pr/OpenAPITools/openapi-generator/pull/24090?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="Review in cubic" src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>

<!-- End of auto-generated description by 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.

3 issues found across 7 files

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

Comment thread docs/generators/kotlin-server.md Outdated
Comment thread docs/generators/kotlin-server.md Outdated
@wing328

wing328 commented Jun 24, 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 24, 2026

Copy link
Copy Markdown
Member

please review the build failure and the feedback from cubic-dev-ai when you've time.

@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.

1 issue found across 3 files (changes from recent commits).

Reply with feedback, questions, or to request a fix.

Re-trigger cubic

@wing328

wing328 commented Jun 25, 2026

Copy link
Copy Markdown
Member

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

@rm127 rm127 force-pushed the feat/kotlin-server-inheritance-mode branch from 7561bc3 to 7facfa2 Compare June 25, 2026 11:54
@rm127

rm127 commented Jun 25, 2026

Copy link
Copy Markdown
Author

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

Thanks for the heads up! I've altered the history to reflect my GH user

@jpfinne

jpfinne commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

@rm127 I don't use Kotlin...
Can't INHERITANCE_MODE_NONE and INHERITANCE_MODE_COMPOSITION be done in the OpenApiNormalizer?

Then all languages can use the feature

Just my 2 cents

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.

3 participants