Improve OneOf handling with new normalizer REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING#23543
Conversation
|
Thanks a lot for this quick fix! I have verified it resolves the issue for our use-case. |
This bug has bother me for months. I've initially tried to fix it in DefaultCodeGen, but the handling of composition vs inheritance is very complex. I'll work on the PR. In the meantime you can create a custom normalizer: |
…REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING
There was a problem hiding this comment.
5 issues found across 18 files
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="docs/customization.md">
<violation number="1" location="docs/customization.md:654">
P3: Malformed Markdown adds a stray backtick to the documented normalizer name, making the docs show/copy the option incorrectly.</violation>
</file>
<file name="pom.xml">
<violation number="1" location="pom.xml:27">
P1: Root-level module declarations duplicate modules already listed in the default-active openapi-generator profile, which can make Maven fail with duplicated reactor projects.</violation>
</file>
<file name="modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java:1608">
P2: `schema.getOneOf()` is iterated without a null guard after `processSimplifyOneOf()` can clear `oneOf`, causing a potential NPE in discriminator replacement.</violation>
<violation number="2" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java:1619">
P1: Unconditionally removing `oneOf` can drop subtype metadata when the discriminator mapping is partial or not rebuilt.</violation>
<violation number="3" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java:1630">
P2: `hasParent()` returns false immediately when it encounters a visited `allOf` branch, so later siblings are never checked and an existing parent ref can be added again.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
| @@ -23,6 +23,15 @@ | |||
| <developerConnection>scm:git:git@github.com:openapitools/openapi-generator.git</developerConnection> | |||
There was a problem hiding this comment.
P1: Root-level module declarations duplicate modules already listed in the default-active openapi-generator profile, which can make Maven fail with duplicated reactor projects.
Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At pom.xml, line 27:
<comment>Root-level module declarations duplicate modules already listed in the default-active openapi-generator profile, which can make Maven fail with duplicated reactor projects.</comment>
<file context>
@@ -23,6 +23,15 @@
<url>https://github.com/openapitools/openapi-generator</url>
</scm>
+ <modules>
+ <module>modules/openapi-generator-core</module>
+ <module>modules/openapi-generator</module>
+ <module>modules/openapi-generator-cli</module>
</file context>
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/OpenAPINormalizer.java">
<violation number="1" location="modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java:1780">
P1: Regression in `hasParent()`: the new visited check short-circuits recursive traversal, so existing parent refs nested under `allOf` are missed and duplicate parent inheritance refs can be added.</violation>
</file>
Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.
fixes #19261, #912, #23527, #22013, #23276, #14769, #16391, #19194, #19261, #23577 (and probably others) when using the new REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING normalizer option.
The goal is to use inheritance when composition or
useOneOfInterfacesdon't work.The normalizer converts oneOf with discriminators.
It ensures that:
x-discriminator-valueif present or single enum value).For the java generators, it means that inheritance with a base class class is generated instead of an interface.
Warning: the normalizer does not support inline oneOf (as seen in #23276 and in #15 using composed-oneof.yaml). A warning is outputted
Inline oneOf schema not supported by REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING normalization. No transformation is done.It might be nice to refactor the inline oneOfs in the OpenAPINormalizer, similar to REFACTOR_ALLOF_INLINE_SCHEMAS done in the InlineModelResolver.
ps: it can't be done in InlineModelResolver because it runs after the normalizer.
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.
master(upcoming7.x.0minor release - breaking changes with fallbacks),8.0.x(breaking changes without fallbacks)"fixes #123"present in the PR description)Summary by cubic
Adds a normalizer that converts
oneOfintodiscriminator.mapping, removesoneOf, and sets upallOfinheritance so Java andspringgenerators emit proper class hierarchies. Also hardens parent detection to avoid missed inheritance in nestedallOfgraphs.New Features
REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING=true: buildsdiscriminator.mappingfromoneOf$refs (keeps existing mappings), removesoneOf, adds anallOfparent$refto children and moves child properties under thatallOf; removes the discriminator property from children when the base defines it; skips inlineoneOfwith a warning.x-discriminator-value, else a single enum on the discriminator property, else the schema name. Docs updated with aspringCLI example; new samples/tests (issues 912, 14769, 19261, 22013, 23276, 23527, 23577) validateJsonTypeInfo/JsonSubTypesannotations and children extending the base.Bug Fixes
hasParentrecursion guard to prevent early termination or infinite loops and reliably detect existing parents without duplicatingallOflinks.Written for commit c1da8d4. Summary will update on new commits.