refactor: decouple schema naming from discriminator mapping introspection

Merged
MA
2mo ago
+59-33

Decouples the toModelName (e.g., adding a prefix or a suffix) Codegen functionality from the getAllOfDescendants and getOneOfAnyOfDescendants discriminator introspection methods. This is in preparation for these methods being moved to the DiscriminatorUtils class.

This is done so that the discriminator logic can later be clarified and optimized, since as of current there are several passes over the entire specification when searching for the discriminator mappings. A single walk along the path for each discriminator schema should be enough to gather all the necessary information for building the typing and the mapping for the discrimination.

There is also the rather crude implementation of the specification analysis, where it should be preferable to utilize the visitedSchemas approach (walk the path recursively and keep a log of what schemas that have been visited to terminate loops).

This PR also breaks out the building of a larger error message when failing to interpret a discriminator, and it also creates a helper for repeated vendor extension introspection.

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

Decouples schema naming from discriminator mapping introspection: descendant mappings now use raw schema names, and toModelName is applied once during discriminator creation. Centralizes x-discriminator-value lookups and discriminator error message building in DiscriminatorUtils; no behavior change expected.

  • Refactors
    • getAllOfDescendants/getOneOfAnyOfDescendants now return schema-name-based MappedModel; DefaultCodegen.adjustModelNames() applies toModelName during createDiscriminator.
    • Added DiscriminatorUtils.discriminatorVendorExtensionValue(...) and DiscriminatorUtils.getDiscriminatorSchemaError(...); replaced inline vendor extension access and warning assembly.