Skip to content

fix: allOf/anyOf/oneOf generation bugs, with an aggregate test matrix - #2562

Merged
mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
mromaszewicz:aggregates/phase1
Sep 24, 2026
Merged

mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
mromaszewicz:aggregates/phase1

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Adds internal/test/aggregates/matrix: every schema shape in shapes/ is placed at each position we generate code for (component, property, array items, additionalProperties value, request and response bodies by $ref and inline, component request bodies and responses, a default: response, a response with headers), and each sample goes through a generated client and strict server and must come back unchanged, on the wire and parsed. specgen writes one package per shape. Union parameters get their own fixture, internal/test/aggregates/params.

Specs that failed to generate:

Generated code that did not compile:

  • components/responses entries with an inline schema that needs its own type aliased a JSONResponseBody that was never declared (response.union undefined #2539). They are now named once, by the model pass, and everything that refers to them points at that name.
  • A union that declares a non-string discriminator property itself assigned it a quoted string.
  • An x-go-type-import of a package the generated file already imports, such as time, was emitted twice.

Data lost or mangled on the wire:

  • Non-string discriminator values were stamped as JSON strings, and Discriminator() could not read them (Make possible use values other than strings as discriminator #1619). Stamps are typed now, and Discriminator() returns the value's text.
  • From*/Merge* on a union with its own properties left those properties zero, and they then overwrote the variant's values when marshaling. A property that is still unset now takes the variant's value; a stale additionalProperties copy of a variant key is dropped.
  • A strict $ref response without headers embeds its component envelope, and a non-struct envelope (an array, map or primitive body) was encoded as {"XJSONResponse": ...} (strict server returns wrong JSON for array responses #1147, Incorrect array response generation with multiple refs #1509).
  • An inline request body with properties and additionalProperties lost the additional properties: its wrapper type had no JSON methods.
  • oneOf/anyOf parameters: an inline one was an anonymous struct that no caller outside the package could build, and no union parameter could be bound by a server (Unexported fields with anyOf in request parameters #2560). Inline union parameters are now named Params, and unions of scalars get UnmarshalText and Bind.

Output changes for specs that already generated:

  • allOf over an array keeps its items: []X instead of []any.
  • A oneOf/anyOf next to allOf is merged into the type instead of being dropped, unless its branches only add constraints.
  • A default: response that refers to such a component response has the component's model as its Body, instead of an anonymous struct.
  • Hoisted body types no longer say "defines parameters for".

Closes #839
Closes #1147
Closes #1379
Closes #1509
Closes #1619
Closes #2539
Closes #2560

Adds internal/test/aggregates/matrix: every schema shape in shapes/ is
placed at each position we generate code for (component, property,
array items, additionalProperties value, request and response bodies by
$ref and inline, component request bodies and responses, a `default:`
response, a response with headers), and each sample goes through a
generated client and strict server and must come back unchanged, on the
wire and parsed. specgen writes one package per shape. Union parameters
get their own fixture, internal/test/aggregates/params.

Specs that failed to generate:
- allOf failed whenever any member had a `default` (oapi-codegen#1379), and on
  mismatched uniqueItems, exclusiveMinimum/Maximum, allowEmptyValue,
  readOnly or writeOnly, which rejected decorators such as
  `allOf: [$ref X, {readOnly: true}]`. These keywords don't shape the Go
  type; flags are now ORed and one-sided values carry over.
- OpenAPI 3.1 `type: [object]` and `[object, "null"]` failed to merge.
  "null" is now left out of the comparison and added back to the result.
- Two identical additionalProperties schemas failed to merge.
- anyOf and oneOf on one schema named their inline members alike and
  failed with a duplicate type name (oapi-codegen#839). With both present, members
  are <Schema>AnyOf<i> and <Schema>OneOf<i>.

Generated code that did not compile:
- components/responses entries with an inline schema that needs its own
  type aliased a <Name>JSONResponseBody that was never declared (oapi-codegen#2539).
  They are now named once, by the model pass, and everything that refers
  to them points at that name.
- A union that declares a non-string discriminator property itself
  assigned it a quoted string.
- An x-go-type-import of a package the generated file already imports,
  such as time, was emitted twice.

Data lost or mangled on the wire:
- Non-string discriminator values were stamped as JSON strings, and
  Discriminator() could not read them (oapi-codegen#1619). Stamps are typed now, and
  Discriminator() returns the value's text.
- From*/Merge* on a union with its own properties left those properties
  zero, and they then overwrote the variant's values when marshaling. A
  property that is still unset now takes the variant's value; a stale
  additionalProperties copy of a variant key is dropped.
- A strict $ref response without headers embeds its component envelope,
  and a non-struct envelope (an array, map or primitive body) was
  encoded as {"XJSONResponse": ...} (oapi-codegen#1147, oapi-codegen#1509).
- An inline request body with properties and additionalProperties lost
  the additional properties: its wrapper type had no JSON methods.
- oneOf/anyOf parameters: an inline one was an anonymous struct that no
  caller outside the package could build, and no union parameter could
  be bound by a server (oapi-codegen#2560). Inline union parameters are now named
  <Op>Params<Name>, and unions of scalars get UnmarshalText and Bind.

Output changes for specs that already generated:
- allOf over an array keeps its items: []X instead of []any.
- A oneOf/anyOf next to allOf is merged into the type instead of being
  dropped, unless its branches only add constraints.
- A `default:` response that refers to such a component response has
  the component's model as its Body, instead of an anonymous struct.
- Hoisted body types no longer say "defines parameters for".

Closes oapi-codegen#839
Closes oapi-codegen#1147
Closes oapi-codegen#1379
Closes oapi-codegen#1509
Closes oapi-codegen#1619
Closes oapi-codegen#2539
Closes oapi-codegen#2560

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner September 24, 2026 01:15
@mromaszewicz mromaszewicz added bug Something isn't working notable changes Used for release notes to highlight these more highly labels Sep 24, 2026
@greptile-apps

greptile-apps Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 5/5

The PR appears safe to merge, with no accepted new findings or outstanding previous findings.

Summary

This PR substantially expands aggregate-schema coverage and fixes generation, naming, binding, response-envelope, discriminator, import, recursive-alias, and union serialization behavior.

  • Adds a generated matrix exercising aggregate shapes across components, properties, containers, request bodies, responses, and strict client/server round trips.
  • Improves allOf merging for decorators, nullable types, arrays, additional properties, and adjacent unions.
  • Names and binds union parameters, handles typed discriminators, and reconciles union-owned fields with variant payloads.
  • Corrects component response model reuse, inline request-body JSON delegation, import deduplication, and recursive array declarations.
  • Changes since the previous review add recursive and duplicate-union fixtures, improve shared parameter naming, and refine stale-field reconciliation.

Reviews (4) · Last reviewed commit: "fix: self-reference check, $ref constrai..."

Comment thread pkg/codegen/merge_schemas.go Resolved
Comment thread pkg/codegen/templates/union.tmpl Resolved
Comment thread pkg/codegen/templates/union.tmpl Resolved
mromaszewicz and others added 3 commits September 23, 2026 18:25
…y exact parameter text

adoptUnion only filled a union's own property while it was zero, so a
value it had adopted from one From* call, or that UnmarshalJSON had
filled in, stuck: the next From* left it in place, and MarshalJSON wrote
it over the new variant. A property now follows the union while it is
unset or still holds the value the previous union data had for it; a
value the caller changed is kept. Each value is decoded into a fresh
variable, so a pointer, map or slice the caller shares is never written
through.

UnmarshalText decided whether parameter text was a JSON boolean or
number with json.Decoder, whose More() reports false before a closing
bracket, so text such as `123}` bound as the number 123. The text must
now be exactly one JSON value, with no surrounding whitespace; anything
else is a string, or an error when the union has no string branch.

The allOf merge's union of 3.1 "null" types is kept as it is, and its
comment now says why: a member declares "null" to make the composed type
nullable, as with 3.0's nullable decorator idiom (oapi-codegen#1898).

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…rays

- adoptUnion panicked on a union property of interface type (an untyped
  schema), which holds nil before the first From*: reflect cannot ask a
  nil interface IsZero. Guarded.
- adoptUnion now fills only properties that MarshalJSON always writes.
  An optional pointer is written only when set, so it was never at risk
  of overwriting the variant, and adopting it made a previous variant's
  value stick after switching to one without the key. Such unions no
  longer need adoptUnion at all.
- From*/Merge* on a union with additionalProperties also drop the keys
  any variant declares. UnmarshalJSON copies them there, so after a round
  trip the previous variant's keys were written over the new variant.
- A union parameter shared by a path item has no operation name to be
  prefixed with, so its type got a bare name such as Id, which collides
  with a schema of that name. It is now <Name>Param; its members keep
  their names. A type an operation declares under a component type's
  name is now a generation error rather than code that does not compile.
- oneOf/anyOf parameters under components/parameters named their members
  from an empty path (N0, N1) and never declared them. They are now
  <Name>0, <Name>1 and declared.
- A self-referential array, such as the result of
  `allOf: [{type: array, items: $ref Self}, ...]`, was declared as an
  invalid recursive alias. Types whose definition refers to themselves are
  now declared as defined types.
- A type that is a member of both a union's anyOf and its oneOf got its
  accessors twice.
- The UnmarshalText doc comment of a union with only string branches was
  garbled.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…ions

- The check that declares a self-referential type as a defined type
  rather than an alias searched the whole type expression, struct bodies
  included, so an array whose inline items had a property named like the
  type, such as `Data: {type: array, items: {properties: {data: ...}}}`,
  stopped being an alias. The expression is now parsed, and only
  identifiers in type positions count: field names, struct tags and
  other packages' types do not.
- A oneOf next to allOf whose branches only add constraints is dropped,
  as before, but only inline branches were recognized. A `$ref` branch is
  now judged by the schema it refers to, so `oneOf: [$ref NeedEmail,
  $ref NeedPhone]` with `NeedEmail: {required: [email]}` keeps the schema
  an alias of its allOf member instead of a union of `any`.
- A union whose branch is itself a union of scalars, such as a `$ref` to
  one, gets UnmarshalText and Bind like any scalar union, so it binds as a
  parameter; before, a server silently dropped the value. A union that
  contains itself is left without them.
- The UnmarshalText doc comment joins the accepted types with "or".

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mromaszewicz
mromaszewicz merged commit 11cfb23 into oapi-codegen:main Sep 24, 2026
15 checks passed
@mromaszewicz
mromaszewicz deleted the aggregates/phase1 branch September 24, 2026 04:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working notable changes Used for release notes to highlight these more highly

Projects

None yet

1 participant