fix(avro): order "null" first in union when default is null

Merged
MW
5mo ago
+54-0

The avro-schema generator emitted an invalid union when a property combined a non-null type with an explicit default: null (commonly produced by nullable: true + allOf composition), e.g. ["model.Foo", "null"] with "default": null. Per the Avro specification, a union's default value must match the FIRST branch of the union, so the default null is only valid when "null" is the first branch. The invalid ordering is accepted by most schema parsers but fails at read time when the default is applied during schema evolution, crashing consumers.

The Swagger Parser represents an explicit default: null as a Jackson NullNode (a non-null Java object) rather than a Java null, so toDefaultValue returned the string "null" and the field was rendered through the concrete-default branch ([<type>, "null"]). Treat an explicit null default as "no default" so the field falls through to the existing nullable-union form (["null", <type>] with "default": null), which is valid. Real (non-null) defaults are unaffected.

Extends the issue6268 test spec with a nullable scalar and an allOf-composed model reference, both using default: null, and regenerates the sample.

Closes: https://github.com/OpenAPITools/openapi-generator/issues/24134

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 Avro union ordering when a field has default: null by placing "null" first, preventing invalid schemas and runtime crashes during schema evolution.

  • Bug Fixes
    • In org.openapitools.codegen.languages.AvroSchemaCodegen, treat explicit default: null (Jackson NullNode) as no concrete default in toDefaultValue, so we emit ["null", <type>] with "default": null per Avro spec.
    • Extended issue6268.yaml with a nullable scalar and an allOf-composed model using default: null; regenerated samples (AvroDefaultNullReference.avsc, updated SampleModelToTestAvroDefaultValues.avsc).
    • Real (non-null) defaults are unchanged.