Skip to content

feat(v3): a child that is an allOf of its parent isn't a union of its siblings - #2577

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:aggregates/v3-unions-parent-lists
Sep 25, 2026
Merged

mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:aggregates/v3-unions-parent-lists

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Step 4 of the schema-merging-behavior: v3 work, second of four PRs after #2576. It changes only v3, which is still opt-in and undocumented. v1 and v2 output doesn't change.

  1. 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)
  2. a child that is an allOf of its parent isn't a union of its siblings (this PR)
  3. an allOf of unions is one of each union's variants at once
  4. an inline discriminated variant takes the value it pins

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:

Pet:
  type: object
  required: [petType]
  properties:
    petType: {type: string}
  discriminator:
    propertyName: petType
  oneOf:
    - $ref: '#/components/schemas/Cat'
    - $ref: '#/components/schemas/Dog'
Cat:
  allOf:
    - $ref: '#/components/schemas/Pet'
    - properties:
        meow: {type: string}

Merging Pet into Cat copies Pet's oneOf, so in v2 every child is a union of all the children: Cat gets union storage, Cat.AsDog(), Cat.FromDog() and Cat.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.

Schema, with Pet as above v2 v3
Cat: allOf [$ref Pet, {properties: …}] struct with union storage, As/From/Merge for every child, Discriminator(), ValueByDiscriminator() type Cat struct { Meow *string; PetType string }
Kitten: allOf [$ref Cat, {properties: …}] the same union of every child plain struct
Bird: allOf [$ref Pet, {description: …}] the same union of every child struct of Pet's fields
Dog: allOf [$ref Pet] type Dog = Pet, so Pet.AsDog() returns a Pet struct of Pet's fields
Fish: allOf [$ref Pet, {oneOf: [$ref Fresh, $ref Salt]}] one union of all the children and Fresh and Salt; FromFresh stamps petType = "Fresh" a union of Fresh and Salt, without Pet's discriminator
property allOf [$ref Pet, {properties: …}], which Pet doesn't list Pet's union Pet's union, unchanged

The rule

  • A member's oneOf or anyOf is left out when it lists the composition being generated, or a schema the merge flattens into it: another member, or a member of a nested allOf. So a union dropped for allOf: [$ref Pet, $ref Cat, …] is dropped for [$ref Cat, $ref Pet, …] too, and for an allOf with Cat further down.
  • Only the composition being merged counts as listed, not every allOf being generated further up. A property of Cat whose schema is allOf: [$ref Pet, …] is a Pet, one of the parent's branches, and keeps the union.
  • A child that is only 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.
  • A discriminator goes with the list it tells apart, the oneOf when there are both. When the parent's list is left out, so is its discriminator: it says which child a value is, not which branch of a union the child has of its own.
  • It works the same for anyOf.

Tests

  • pkg/codegen/merge_schemas_v3_test.go: TestParentListsChildrenV3, TestParentListsChildOnlyV3 and TestParentListsMemberV3 cover 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.
  • The aggregate matrix gains 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 pins Cat as a plain struct and checks that ValueByDiscriminator on the parent still returns a Cat. No existing cell changes.

Fixtures

make generate changes no committed file. Regenerating every fixture under v3 changes nothing beyond #2576.

🤖 Generated with Claude Code

… 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]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner September 25, 2026 02:41
@greptile-apps

greptile-apps Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

[Medium risk] Adds test cases for a schema merging edge case.

The PR appears safe to merge; no actionable issue was identified.

Summary

The PR changes opt-in v3 schema merging so a child composed from a parent that lists it does not inherit the parent’s sibling union.

  • Preserves the parent union and a child’s independently declared union.
  • Adds unit tests and v1/v2/v3 matrix fixtures for the inheritance shape.

Reviews (1) · Last reviewed commit: "feat(v3): a child that is an allOf of it..."

@mromaszewicz
mromaszewicz merged commit 217ca5d into oapi-codegen:main Sep 25, 2026
15 checks passed
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]>
@mromaszewicz
mromaszewicz deleted the aggregates/v3-unions-parent-lists branch September 26, 2026 00:46
@mromaszewicz mromaszewicz added the enhancement New feature or request label Sep 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant