Skip to content

[Kotlin-client] fix kotlin code client generator for oneOf with allOf#24163

Open
bykakashka wants to merge 3 commits into
OpenAPITools:masterfrom
bykakashka:fix/kotlin_client_allOf-oneOf
Open

[Kotlin-client] fix kotlin code client generator for oneOf with allOf#24163
bykakashka wants to merge 3 commits into
OpenAPITools:masterfrom
bykakashka:fix/kotlin_client_allOf-oneOf

Conversation

@bykakashka

@bykakashka bykakashka commented Jun 30, 2026

Copy link
Copy Markdown

Removed children params from parent model in case of oneOf + allOf usage. Fixes #24164

PR checklist

  • Read the contribution guidelines.
  • 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.
  • 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

Fixes Kotlin client codegen for polymorphic oneOf + allOf with discriminators by stopping child properties from leaking into the parent model. Works consistently with jackson, moshi, gson, and kotlinx_serialization.

  • Bug Fixes
    • Set skipOneOfPropertyMergeInParent = true in KotlinClientCodegen (constructor) for all serializers; moved the flag from AbstractKotlinCodegen and decoupled it from supportsInheritance.
    • Simplified DefaultCodegen so oneOf/anyOf are treated as alternatives when the flag is on (no child-property merge into parents regardless of discriminator or supportsInheritance); added parameterized tests across all serializers covering: plain oneOf wrapper, oneOf with discriminator (no leak), plain allOf (inheritance preserved), and allOf with discriminator (no leak).

Written for commit 646e880. 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.

3 issues found across 4 files

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

Re-trigger cubic

…Inheritance

- Remove supportsInheritance gate from the non-merge branch condition
  to avoid silently re-enabling child-property merging for generators
  using alternative polymorphism strategies
- Move skipOneOfPropertyMergeInParent flag from AbstractKotlinCodegen
  to KotlinClientCodegen (more specific scope)
- Fix test assertions to check correct model paths

@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 4 files (changes from recent commits).

Prompt for AI agents (unresolved 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="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java:311">
P2: Setting `skipOneOfPropertyMergeInParent = true` unconditionally in the KotlinClientCodegen constructor affects all oneOf/anyOf schemas, not just the oneOf+allOf case described in the PR. In `DefaultCodegen.addProperties()`, this flag prevents merging oneOf AND anyOf child properties into parent models globally. Since KotlinClientCodegen only supports oneOf/anyOf wrappers for `jvm-retrofit2` with `gson`/`kotlinx_serialization`, and the default is `jvm-okhttp4` with `moshi`, plain oneOf/anyOf schemas under default or unsupported configurations may lose child properties with no wrapper fallback. Consider scoping this flag to the specific oneOf+allOf/discriminator case or to library/serialization combinations that provide wrapper support.</violation>
</file>

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

Re-trigger cubic

cliOptions.add(CliOption.newBoolean(USE_JACKSON_3,
"Use Jackson 3 dependencies (tools.jackson package). Not yet supported for kotlin-client; reserved for future use."));

skipOneOfPropertyMergeInParent = true;

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.

P2: Setting skipOneOfPropertyMergeInParent = true unconditionally in the KotlinClientCodegen constructor affects all oneOf/anyOf schemas, not just the oneOf+allOf case described in the PR. In DefaultCodegen.addProperties(), this flag prevents merging oneOf AND anyOf child properties into parent models globally. Since KotlinClientCodegen only supports oneOf/anyOf wrappers for jvm-retrofit2 with gson/kotlinx_serialization, and the default is jvm-okhttp4 with moshi, plain oneOf/anyOf schemas under default or unsupported configurations may lose child properties with no wrapper fallback. Consider scoping this flag to the specific oneOf+allOf/discriminator case or to library/serialization combinations that provide wrapper support.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java, line 311:

<comment>Setting `skipOneOfPropertyMergeInParent = true` unconditionally in the KotlinClientCodegen constructor affects all oneOf/anyOf schemas, not just the oneOf+allOf case described in the PR. In `DefaultCodegen.addProperties()`, this flag prevents merging oneOf AND anyOf child properties into parent models globally. Since KotlinClientCodegen only supports oneOf/anyOf wrappers for `jvm-retrofit2` with `gson`/`kotlinx_serialization`, and the default is `jvm-okhttp4` with `moshi`, plain oneOf/anyOf schemas under default or unsupported configurations may lose child properties with no wrapper fallback. Consider scoping this flag to the specific oneOf+allOf/discriminator case or to library/serialization combinations that provide wrapper support.</comment>

<file context>
@@ -307,6 +307,8 @@ public KotlinClientCodegen() {
         cliOptions.add(CliOption.newBoolean(USE_JACKSON_3,
             "Use Jackson 3 dependencies (tools.jackson package). Not yet supported for kotlin-client; reserved for future use."));
+
+        skipOneOfPropertyMergeInParent = true;
     }
 
</file context>

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

The bug affects all serialization libraries equally. Without the fix, moshi, gson, and kotlinx_serialization all produce identical broken output — fat parent interfaces with leaked child properties and sibling contamination.

Plain oneOf without discriminator is NOT affected. These schemas generate a data class wrapper (not an interface), and addProperties() merging is correct — the wrapper IS supposed to contain all variant properties as a union type. Tested and confirmed.

Plain allOf without discriminator is NOT affected. Standard inheritance works identically with and without the fix.

Added 16 new parameterized tests covering all 4 patterns (plain oneOf, oneOf+discriminator, plain allOf, allOf+discriminator) × all 4 serialization libraries (jackson, moshi, gson, kotlinx_serialization) — all pass. The flag is safe to set unconditionally at the KotlinClientCodegen level.

@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 1 file (changes from recent commits).

Prompt for AI agents (unresolved 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="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java">

<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/KotlinClientCodegen.java:311">
P2: Setting `skipOneOfPropertyMergeInParent = true` unconditionally in the KotlinClientCodegen constructor affects all oneOf/anyOf schemas, not just the oneOf+allOf case described in the PR. In `DefaultCodegen.addProperties()`, this flag prevents merging oneOf AND anyOf child properties into parent models globally. Since KotlinClientCodegen only supports oneOf/anyOf wrappers for `jvm-retrofit2` with `gson`/`kotlinx_serialization`, and the default is `jvm-okhttp4` with `moshi`, plain oneOf/anyOf schemas under default or unsupported configurations may lose child properties with no wrapper fallback. Consider scoping this flag to the specific oneOf+allOf/discriminator case or to library/serialization combinations that provide wrapper support.</violation>
</file>

Tip: Review your code locally with the cubic CLI to iterate faster.

Re-trigger cubic

Parameterize existing polymorphism tests to run against all 4 Kotlin
serialization libraries (jackson, moshi, gson, kotlinx_serialization),
covering:
- Plain oneOf without discriminator (wrapper model)
- oneOf with discriminator (no child property leaking)
- Plain allOf without discriminator (inheritance preserved)
- allOf with discriminator (no child property leaking)

Simplify DefaultCodegen guards: remove discriminator-scoping from
addProperties() since the flag is set at the KotlinClientCodegen level
for all serialization libraries. Move flag from Jackson-only processOpts
back to constructor — the bug affects all serializers equally and tests
confirm the fix is safe across all configurations.
@bykakashka bykakashka force-pushed the fix/kotlin_client_allOf-oneOf branch from 91c4667 to 646e880 Compare June 30, 2026 13:08
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.

[BUG] [Kotlin] Client generator produces 'fat' classes in case of oneOf with allOf

1 participant