Skip to content

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

Open
mwikberg-virta wants to merge 1 commit into
OpenAPITools:masterfrom
virta-ltd:fix/avro-null-default-union-ordering
Open

fix(avro): order "null" first in union when default is null#24135
mwikberg-virta wants to merge 1 commit into
OpenAPITools:masterfrom
virta-ltd:fix/avro-null-default-union-ordering

Conversation

@mwikberg-virta

@mwikberg-virta mwikberg-virta commented Jun 26, 2026

Copy link
Copy Markdown

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: #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.

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

Review in cubic

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.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

No issues found across 5 files

Re-trigger cubic

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][AVRO] Union with explicit default: null is ordered type-first, producing an invalid Avro schema

1 participant