Improve OneOf handling with new normalizer REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING

Merged
JP
7mo ago
+1130-38

fixes #19261, #912, #23527, #22013, #23276, #14769, #16391, #19194, #19261, #23577, #22209 (and probably others) when using the new REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING normalizer option.

The goal is to use inheritance with base classes when composition or useOneOfInterfaces don't work.
For example:

  • oneOf + discriminator mapping generates classes implementing and extending a base class
  • oneOf + discriminator generates an interface or use composition
  • discriminator name using a unique enum value

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.

Edge cases don't do anything:

  • no discriminator property
  • oneOf with inline schema
  • non matching number of oneOf nodes and number of discriminator mappings (if present)

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.

@sreeshas (2017/08) @jfiala (2017/08) @lukoyanov (2017/09) @cbornet (2017/09) @jeff9finger (2018/01) @karismann (2019/03) @Zomzog (2019/04) @lwlee2608 (2019/10) @martin-mfg (2023/08)
| @cachescrubber (2022/02) @welshm (2022/02) @MelleD (2022/02) @atextor (2022/02) @manedev79 (2022/02) @javisst (2022/02) @borsch (2022/02) @banlevente (2022/02) @Zomzog (2022/09) @martin-mfg (2023/08)
@wing328

Please review


Summary by cubic

Adds a normalizer that replaces oneOf with discriminator.mapping and attaches allOf inheritance so Java and spring generators emit correct Jackson polymorphism and class hierarchies. Also refines composed/inline model handling and updates docs.

  • New Features

    • REPLACE_ONE_OF_BY_DISCRIMINATOR_MAPPING=true: build discriminator.mapping from oneOf $refs (merge existing), remove oneOf, add an allOf parent $ref to each child, and move child properties under that allOf.
    • Remove the discriminator property from children when the base defines it; skip inline or non-$ref oneOf with a warning.
    • Discriminator value resolution: x-discriminator-value > single enum on the discriminator property (resolving $ref) > schema name.
    • Ensure inheritance for existing discriminator mappings by adding missing allOf links to children; do not duplicate links.
    • Docs: added CLI example for spring.
  • Bug Fixes

    • Use isParentReferencedInChild to avoid duplicate allOf links and recursion; fix allOf construction and discriminator property lookup via ModelUtils.getReferencedSchema.
    • Warnings for missing discriminator property, mapping/oneOf size mismatch, and unsupported inline cases.
    • After moving child properties into allOf, clear leftover child metadata to prevent duplication; convert empty ComposedSchema to plain Schema.
    • InlineModelResolver now gathers inline models for composed schemas with properties (fixes 22209).