Skip to content

Improve OneOf handling with new normalizer REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING#23543

Open
jpfinne wants to merge 18 commits intoOpenAPITools:masterfrom
jpfinne:feature/normalizer_REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING
Open

Improve OneOf handling with new normalizer REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING#23543
jpfinne wants to merge 18 commits intoOpenAPITools:masterfrom
jpfinne:feature/normalizer_REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING

Conversation

@jpfinne
Copy link
Copy Markdown
Contributor

@jpfinne jpfinne commented Apr 14, 2026

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 useOneOfInterfaces don't work.

The normalizer converts oneOf with discriminators.
It ensures that:

  • discriminator mappings are created (using x-discriminator-value if present or single enum value).
  • It removes the oneOf.
  • It ensures that the referenced children correctly has an allOf to the parent.
  • it removes the single enum value in the referenced mappings if already present in the parent.

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

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • 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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • 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

Adds a normalizer that converts oneOf into discriminator.mapping, removes oneOf, and sets up allOf inheritance so Java and spring generators emit proper class hierarchies. Also hardens parent detection to avoid missed inheritance in nested allOf graphs.

  • New Features

    • REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING=true: builds discriminator.mapping from oneOf $refs (keeps existing mappings), removes oneOf, adds an allOf parent $ref to children and moves child properties under that allOf; removes the discriminator property from children when the base defines it; skips inline oneOf with a warning.
    • Discriminator values prefer x-discriminator-value, else a single enum on the discriminator property, else the schema name. Docs updated with a spring CLI example; new samples/tests (issues 912, 14769, 19261, 22013, 23276, 23527, 23577) validate JsonTypeInfo/JsonSubTypes annotations and children extending the base.
  • Bug Fixes

    • Fixes the hasParent recursion guard to prevent early termination or infinite loops and reliably detect existing parents without duplicating allOf links.

Written for commit c1da8d4. Summary will update on new commits.

@winklerm
Copy link
Copy Markdown

Thanks a lot for this quick fix!

I have verified it resolves the issue for our use-case.

@winklerm winklerm mentioned this pull request Apr 15, 2026
6 tasks
@jpfinne
Copy link
Copy Markdown
Contributor Author

jpfinne commented Apr 15, 2026

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:

NORMALIZER_CLASS: Set to full classname of a class extending the default org.openapitools.codegen.OpenAPINormalizer. It allows customization of the default normalizer

@jpfinne jpfinne changed the title normalizer REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING Improve OneOf handling with new normalizer REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING Apr 17, 2026
@jpfinne jpfinne marked this pull request as ready for review April 18, 2026 16:16
Copy link
Copy Markdown
Contributor

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

Choose a reason for hiding this comment

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

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.

Comment thread pom.xml
@@ -23,6 +23,15 @@
<developerConnection>scm:git:git@github.com:openapitools/openapi-generator.git</developerConnection>
Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai bot Apr 18, 2026

Choose a reason for hiding this comment

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

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>
Fix with Cubic

Comment thread docs/customization.md Outdated
Copy link
Copy Markdown
Contributor

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

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

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][DefaultCodegen] OneOf with discriminator for referenced object with allOf

2 participants