Repository navigation
feat(v3): a child that is an allOf of its parent isn't a union of its siblings - #2577
Merged
mromaszewicz merged 1 commit intoSep 25, 2026
Conversation
… siblings
Some generators, springdoc and NSwag among them, write inheritance with
the parent listing its children in a oneOf and each child an allOf of
the parent:
Pet: {discriminator: {propertyName: petType}, oneOf: [$ref Cat, $ref Dog]}
Cat: {allOf: [$ref Pet, {properties: {meow: ...}}]}
Merging Pet into Cat copies Pet's oneOf, so in v2 every child is a
union of all the children: Cat has union storage, Cat.AsDog(),
Cat.FromDog() and Cat.ValueByDiscriminator().
In schema-merging-behavior v3, a member's oneOf or anyOf that lists the
composition being generated, or a schema the merge is flattening into
it, is left out: a Cat already is the Cat branch, so the list says
nothing more about it. Cat is a plain struct, and Pet stays the union of
Cat and Dog. This holds through deeper chains (Kitten, an allOf of Cat)
and for anyOf. A child that is only `allOf: [$ref Pet]`, or that with
members that only annotate, such as a description, is likewise a struct
of Pet's fields rather than an alias of Pet's union, which would make
Pet.AsCat() return a Pet. A schema the parent doesn't list, such as an
inline `allOf: [$ref Pet, {...}]` or a property of Cat that is one, is
still one of the parent's branches and keeps the union. The members'
order doesn't matter: `allOf: [$ref Pet, $ref Cat, ...]` is a Cat too,
as is an allOf with Cat further down. The parent's discriminator goes
with the list it tells apart, so it doesn't land on a union the child
has of its own.
The matrix gains oneof_parent_lists_children, with new v1 and v2
cells; no existing cell changes, and regenerating every fixture under
v3 changes nothing beyond the previous commit.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Contributor
|
mromaszewicz
added a commit
that referenced
this pull request
Sep 25, 2026
…2578) * feat(v3): an allOf of unions is one of each union's variants at once `allOf: [$ref Payment, $ref Delivery]`, where Payment is a card or a transfer and Delivery a courier or a pickup, describes a value that is both: a card and a courier, say. v2 concatenates the members' oneOfs into one union of four variants, so FromCourier replaces the card that FromCard set. schema-merging-behavior v3 keeps each oneOf and anyOf an allOf brings as a union of its own. The type still holds one JSON value, and each variant's As* parses it as before. From* for a variant now replaces only the keys its own union owns, those only its union's variants declare, and keeps the rest: FromCard then FromCourier holds both, and FromTransfer afterwards swaps the card for the IBAN and keeps the courier's address. A key several unions' variants declare, such as a shared id, belongs to all of them and is kept unless the new variant sets it: a variant in another union too may be that union's data. A variant from another document or with an x-go-type declares no keys, since its fields aren't ours to read. A variant that marshals to null clears its union's keys and keeps the rest; when the variant or the held value is anything else that isn't a JSON object, From* replaces the value whole, as it did. Additional properties the caller set are kept. Merge* is unchanged. A member that is a $ref to a union type also gets As* and From* for that type, so `AsPayment()` returns a Payment with its own methods, and `FromPayment` sets it, keeping the delivery. AsPayment leaves out the keys only Delivery's variants declare, so setting the Payment back later doesn't bring back an old delivery; `FromPayment(Payment{})` clears the payment. An allOf that brings a single union, however many times, generates as before, and so does a schema with a single oneOf or anyOf. A schema with both an anyOf and a oneOf is two unions this way. A union brought twice is one, with the $ref and discriminator either copy has, and one of only 3.1 null branches says nullable rather than being a union. A value has one discriminator, which belongs to one of the unions: the one that declares it, or, for one declared next to the allOf or in a member of its own, the union whose variants its mapping names or, without a mapping, the one whose variants all have the property. It is an error for it to fit several unions or none, or to be declared on two (the merge already rejects two different ones). It stamps and dispatches only its own union's variants, and the other unions' variants no longer fail its mapping check. Its property is one of its union's keys, even when the variants leave it to the stamp. A discriminator next to an anyOf keeps working through the allOf too. An inline variant of one of several unions is named <path>OneOf<n><index> or <path>AnyOf<n><index> (<n> only when there are several of a kind). A oneOf whose branches only restate a type another member declares, such as `{type: object, required: [email]}` next to `$ref Contact`, only adds constraints (see #2576) and makes no union here either. The matrix gains allof_of_unions and allof_of_unions_additional (with additionalProperties), with v3 tests of the above and v1 and v2 cells (v1 is broken: it embeds both unions, whose MarshalJSON and UnmarshalJSON then cancel out). v3's anyof_oneof cell changes: its anyOf and oneOf variants now keep each other's data. Generating the committed fixtures with v3 instead of v2 changes one more type beyond #2577, internal/test/aggregates/oneof's OneOfObject12, an allOf of two oneOfs. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> * fix(v3): union keys by their wire names, for every union a variant is in Review fixes for allOf of unions: - A variant in two of the unions, such as Card in both `oneOf: [Card, Transfer]` and `oneOf: [Card, Courier]`, only got the keys the first union owns, so FromCard after FromCourier kept the courier's address and the value matched both of the second union's variants. Setting such a variant now replaces what each of its unions owns. Setting another variant still never removes the shared variant's keys. - Owned keys used the spec's property names, but a json tag in x-oapi-codegen-extra-tags renames a field on the wire and x-go-json-ignore takes it off, so FromTransfer could leave a renamed card field behind. Keys are now the names the fields marshal as. - A variant's keys stopped at eight levels of allOf; they now come from every level, with a visited set for cycles. - From* for a variant of several unions, and From* for a $ref union, return a marshaling error before touching the union data, instead of clearing it. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> --------- Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This was referenced Sep 25, 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.
Step 4 of the
schema-merging-behavior: v3work, second of four PRs after #2576. It changes only v3, which is still opt-in and undocumented. v1 and v2 output doesn't change.a oneOf or anyOf that only adds constraints makes no union(feat(v3): a oneOf or anyOf that only adds constraints makes no union #2576)What changes
Some generators, springdoc and NSwag among them, write inheritance with the parent listing its children in a oneOf and each child an allOf of the parent:
Merging Pet into Cat copies Pet's oneOf, so in v2 every child is a union of all the children:
Catgets union storage,Cat.AsDog(),Cat.FromDog()andCat.ValueByDiscriminator(). A Cat already is the Cat branch, so in v3 the parent's list says nothing more about it and is left out. Pet stays the union of its children.Cat: allOf [$ref Pet, {properties: …}]As/From/Mergefor every child,Discriminator(),ValueByDiscriminator()type Cat struct { Meow *string; PetType string }Kitten: allOf [$ref Cat, {properties: …}]Bird: allOf [$ref Pet, {description: …}]Dog: allOf [$ref Pet]type Dog = Pet, soPet.AsDog()returns a PetFish: allOf [$ref Pet, {oneOf: [$ref Fresh, $ref Salt]}]FromFreshstampspetType = "Fresh"allOf [$ref Pet, {properties: …}], which Pet doesn't listThe rule
allOf: [$ref Pet, $ref Cat, …]is dropped for[$ref Cat, $ref Pet, …]too, and for an allOf with Cat further down.allOf: [$ref Pet, …]is a Pet, one of the parent's branches, and keeps the union.allOf: [$ref Pet], or that plus members that only annotate (feat(v3): an allOf that only annotates a $ref is that $ref's type #2574), would otherwise be an alias of Pet's union. It is merged instead, which gives a struct of Pet's fields.Tests
pkg/codegen/merge_schemas_v3_test.go:TestParentListsChildrenV3,TestParentListsChildOnlyV3andTestParentListsMemberV3cover children, grandchildren, anyOf parents, schemas the parent doesn't list, properties inside a child, children that only reference or annotate the parent, member order, nested allOfs, a union that lists another member of the same allOf, and a child's own union next to the parent's discriminator.oneof_parent_lists_children, with v1, v2 and v3 cells that round-trip their samples at every position, also on Go 1.25. A hand-written v3 test pinsCatas a plain struct and checks thatValueByDiscriminatoron the parent still returns aCat. No existing cell changes.Fixtures
make generatechanges no committed file. Regenerating every fixture under v3 changes nothing beyond #2576.🤖 Generated with Claude Code