fix(avro): order "null" first in union when default is null#24135
Open
mwikberg-virta wants to merge 1 commit into
Open
fix(avro): order "null" first in union when default is null#24135mwikberg-virta wants to merge 1 commit into
mwikberg-virta wants to merge 1 commit into
Conversation
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.
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The avro-schema generator emitted an invalid union when a property combined a non-null type with an explicit
default: null(commonly produced bynullable: true+allOfcomposition), 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 defaultnullis 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: nullas a JacksonNullNode(a non-null Java object) rather than a Javanull, sotoDefaultValuereturned 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
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.
Summary by cubic
Fixes Avro union ordering when a field has
default: nullby placing "null" first, preventing invalid schemas and runtime crashes during schema evolution.org.openapitools.codegen.languages.AvroSchemaCodegen, treat explicitdefault: null(JacksonNullNode) as no concrete default intoDefaultValue, so we emit["null", <type>]with"default": nullper Avro spec.issue6268.yamlwith a nullable scalar and anallOf-composed model usingdefault: null; regenerated samples (AvroDefaultNullReference.avsc, updatedSampleModelToTestAvroDefaultValues.avsc).Written for commit a210b47. Summary will update on new commits.