Repository navigation
fix: allOf/anyOf/oneOf generation bugs, with an aggregate test matrix - #2562
Merged
Merged
Conversation
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]>
Contributor
|
…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]>
This was referenced Sep 26, 2026
This was referenced Nov 22, 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.
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:
default("merging two sets of defaults is undefined" is occured when allOf with default value #1379), and on mismatched uniqueItems, exclusiveMinimum/Maximum, allowEmptyValue, readOnly or writeOnly, which rejected decorators such asallOf: [$ref X, {readOnly: true}]. These keywords don't shape the Go type; flags are now ORed and one-sided values carry over.type: [object]and[object, "null"]failed to merge. "null" is now left out of the comparison and added back to the result.Generated code that did not compile:
Data lost or mangled on the wire:
Output changes for specs that already generated:
default:response that refers to such a component response has the component's model as its Body, instead of an anonymous struct.Closes #839
Closes #1147
Closes #1379
Closes #1509
Closes #1619
Closes #2539
Closes #2560