fix: honor sibling nullable:true on single-element allOf/oneOf/anyOf#465
Merged
Merged
Conversation
OpenAPI 3.0 expresses a nullable composed type as a `nullable: true` sibling
next to a single-element `allOf`/`oneOf`/`anyOf`, e.g.
`{nullable: true, allOf: [{$ref: ...}]}`. The parser's single-element xOf
branch returned the inner ref type verbatim, whose own `nullable` is false, and
the final nullability resolution short-circuited on that non-null `false` before
ever consulting the outer `nullable: true`. The field was generated as
non-nullable (e.g. `Foo` instead of `Foo?`).
The OpenAPI 3.1 form `{oneOf: [{$ref}, {type: null}]}` already worked because it
is a two-element xOf and hits the multi-element branch, which detects the null
member and calls makeNullable. Apply the same makeNullable in the single-element
branch when the outer map carries `nullable: true`, making the two encodings
consistent.
Adds an e2e regression test for the sibling-nullable single-element form and
updates the existing all_of.3.0 expected output, whose `getUserById` response
(`{nullable: true, allOf: [{$ref: UserDto}]}`) had captured the same bug and is
now correctly nullable.
Signed-off-by: Charles-Henri <dumalin.ch@gmail.com>
Carapacik
approved these changes
Jun 21, 2026
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.
Problem
OpenAPI 3.0 expresses a nullable composed type with a
nullable: truesibling next to a single-elementallOf/oneOf/anyOf:swagger_parser generates this field as non-nullable (
Fooinstead ofFoo?).The equivalent OpenAPI 3.1 form already works:
Root cause
In
_findType(open_api_parser.dart), theallOf/oneOf/anyOfbranch splits on element count:oneOf: [{$ref}, {type: null}]): the{type: null}member is detected andmakeNullableis applied. This branch also already honors a siblingnullable: true.allOf: [{$ref}]): it returns the inner ref type verbatim. The inner ref's ownnullableisfalse, and the final nullability resolution (ofType?.nullable ?? map[nullable] ?? …) short-circuits on that non-nullfalse, so the outernullable: trueis never consulted.Same intent, different element count, different code path — which is why the 3.1 two-element form works but the 3.0 single-element sibling form does not.
Fix
Apply
makeNullablein the single-element branch when the outer schema carriesnullable: true, mirroring what the multi-element branch already does.Tests
Adds an e2e regression test
xof/nullable_sibling_all_of.3.0covering both the 3.0 single-element sibling form and the 3.1oneOf+type: nullform. Also updates the existingall_of.3.0expected output, whosegetUserByIdresponse is{nullable: true, allOf: [{$ref: UserDto}]}and had silently captured the same bug — now correctlyFuture<UserDto?>. Full suite passes;dart formatanddart analyzeclean.