Fix all ofs with nullable properties in OpenAPI 3.1 specs

Merged
JU
2mo ago
+242-1

This PR fixes an error where using all ofs with nullable properties in an OpenAPI spec causes the nullability to get lost.
For an example spec see modules/openapi-generator/src/test/resources/3_1/allof-nullable-type-array.yaml

In this example, id should be nullable in both Base and Derived, but in the generated code (e.g. using the php-nextgen) generator, it is only nullable for Base. That happens because the normalizer turns the 3.1 spec's types: ['string', 'null'] into type: string and nullable: true, but AnnotationsUtils.clone parses the spec as 3.1 where that property doesn't exist and therefore skips it.

This should fix the following issues as well:

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

Fixes nullability being lost on properties merged into models via allOf in OpenAPI 3.1 specs.

cloneSchema now restores the nullable flag on the clone and at every nested level (properties, array items, additionalProperties, not, and composed sub-schemas), since the OAS 3.1 serializer drops it. The normalizer rewrites type: [<type>, "null"] into nullable: true, so the flag must survive cloning. Resolves related generator issues for python-flask, typescript-axios, and csharp.