[Kotlin-client] fix kotlin code client generator for oneOf with allOf#24163
[Kotlin-client] fix kotlin code client generator for oneOf with allOf#24163bykakashka wants to merge 3 commits into
Conversation
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
91c4667 to
646e880
Compare
Removed children params from parent model in case of oneOf + allOf usage. Fixes #24164
PR checklist
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.
Summary by cubic
Fixes Kotlin client codegen for polymorphic
oneOf+allOfwith discriminators by stopping child properties from leaking into the parent model. Works consistently withjackson,moshi,gson, andkotlinx_serialization.skipOneOfPropertyMergeInParent = trueinKotlinClientCodegen(constructor) for all serializers; moved the flag fromAbstractKotlinCodegenand decoupled it fromsupportsInheritance.DefaultCodegensooneOf/anyOfare treated as alternatives when the flag is on (no child-property merge into parents regardless of discriminator orsupportsInheritance); added parameterized tests across all serializers covering: plainoneOfwrapper,oneOfwith discriminator (no leak), plainallOf(inheritance preserved), andallOfwith discriminator (no leak).Written for commit 646e880. Summary will update on new commits.