Repository navigation
feat(v3): an allOf that only annotates a $ref is that $ref's type - #2574
Merged
mromaszewicz merged 3 commits intoSep 24, 2026
Merged
mromaszewicz merged 3 commits into
mromaszewicz merged 3 commits into
Conversation
OpenAPI 3.0 allows nothing next to a $ref, so specs wrap one in an allOf
to annotate it: `allOf: [$ref Named, {description: ...}]`, or
`{nullable: true}`, `{readOnly: true}`,
`{x-go-type-skip-optional-pointer: true}`. v2 merges such an allOf into
a copy of Named, a new anonymous struct, or a new enum type with
constants of its own. schema-merging-behavior v3 generates it as Named:
- a component becomes `type Described = Named`;
- a property is `Named`, or `*Named` when a member makes it nullable;
- an enum is the same enum, with no duplicate constants.
This extends the rule v3 already applies to schemas in other documents
and to x-go-type. A member that restates the $ref's type, such as
`{type: object, description: ...}`, still only annotates it; one that
shapes the type (properties, required, an enum, a different type) makes
a new type, as before. x-go-type-name on an annotating member names the
alias, as it does for other members.
A component that would become an alias of itself, directly or through
other such compositions, isn't collapsed: Go rejects a cyclic alias. It
is merged as before, which gives v2's output.
The strict server's response envelope is a defined type over the
response's type, so it needs a delegating MarshalJSON when that type has
a generated one (oapi-codegen#2549). generatesMarshalJSON used to stop at the
alias; v3 now records the type an allOf is an alias of, and the check
follows it. This also fixes a single-member `allOf: [$ref X]`, which v2
still generates without the delegation.
The v3 matrix cells for the decorator shapes change accordingly
(allof_decorator, allof_nullable_decorator, allof_readonly,
allof_default, allof_unique_items, allof_array and the three
allof_nested_own_properties_* shapes); every one still round-trips its
samples. No v1 or v2 cell changes. Regenerating every fixture under v3
finds the same types under a $ref's name, e.g.
`nullable.Nullable[DecoratableValue]` for `nullable.Nullable[string]`,
where DecoratableValue is an alias of string.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Contributor
|
This comment has been minimized.
This comment has been minimized.
Findings from a review of v3 so far and from Greptile on this PR.
Keywords next to allOf:
- The schema's own keywords next to allOf now join the merge as one
more member, as JSON Schema defines them: `{type: string, allOf: [A]}`
is `{allOf: [A, {type: string}]}`. Before, only its own properties,
required, additionalProperties and oneOf/anyOf did, and an enum,
format, items, type, const or discriminator next to allOf was silently
ignored, with no error when it contradicted a member. The common
`{type: object, allOf: [$ref X]}` stays X: a restated type only
annotates.
What only annotates a $ref:
- One rule, used for a $ref, an opaque member, and a composition nested
in another: a member annotates when it has nothing that shapes a Go
type, restates the target's type and format, or is the same $ref
again. Before, a restated type was only accepted at the top level and
next to a local $ref, and a repeated $ref made a copy.
- A restated type must name the same types: `type: integer` next to a
`number` $ref narrows it to int rather than aliasing it.
- A target with properties or additionalProperties but no type is an
object, so `type: object` restates it.
- A type restated next to a schema in another document is taken at its
word, since that schema isn't read. Without that,
`{type: object, allOf: [$ref ext#/X]}` would now be an error.
- x-enum-varnames on a member renames the values, which takes a new
enum type, instead of being dropped by the collapse. When two
members name the same value, the later one's name wins.
- An annotating member's description describes the alias.
Generated MarshalJSON behind an alias:
- x-go-type-name on an annotating member made an alias that
generatesMarshalJSON couldn't see through, so a strict envelope over
it lost additional properties and union data. The alias now records
its target, and the check follows it.
- The alias walks follow up to 100 aliases (they stopped at 16 and 32).
x-oapi-codegen-enum-merge:
- A nested composition always merges in its own mode first, the
default included, so a union around it can't add values it rules
out.
- The extension applies only to the schema it is on. When a property
that members declare separately has no value in common, the error now
says to put it on the member's property, not "on the composition".
Also: the merge no longer carries uniqueItems and exclusive bounds,
which nothing reads; x-go-type and keywords read better together in
errors; stale comments and a typo are fixed.
Six v3 matrix cells change only in doc comments, where the annotating
member's description now describes the alias. No v1 or v2 output
changes. Regenerating every fixture under v3 finds no new rejection.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
…'t a restatement
Review fixes for what counts as restating a $ref's type:
- A member that restates the type but also names enum values
(x-enum-varnames, x-enumNames) counted as a restatement, so the
composition became an alias of the enum and the names were lost. Such
a member now makes a new enum type with those names, as it does
without the restated type.
- A type restated next to a schema in another document was taken at its
word, so `{type: string}` next to an external object was silently
dropped. That schema's type and format are now read to check the
restatement; its body still never is. A different type or format is
the usual error. When the schema doesn't say what it is at the top, as
an allOf of its own doesn't, a restated type is still taken at its
word.
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
mromaszewicz
added a commit
that referenced
this pull request
Sep 24, 2026
* fix: keep a strict response's MarshalJSON behind allOf: [$ref X] In v2, `allOf: [$ref X]` generates as an alias of X. When X has a generated MarshalJSON, for additionalProperties or a union, the strict server's response envelope, a defined type over the alias, needs a delegating MarshalJSON (#2549). generatesMarshalJSON, which decides that, couldn't see through the alias, so the envelope had none, and X's additional properties or union data were silently dropped from the response. generateAllOfV2 now records the type such a composition is an alias of, as generateAllOfV3 already does (#2574), and the check follows it. This only adds a MarshalJSON method to the envelopes of such responses; no generated fixture in the repository changes. v1 is unaffected: it generates a struct that embeds X. A new matrix shape, allof_single_ref_custom_json, covers it under v1, v2 and v3. Its v2 cell lost the additional properties at every body position before this fix. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> * test: round-trip a union behind allOf: [$ref X]; skip v1's Go-dependent cells CI failed on the v1 cell of allof_single_ref_custom_json, which passes on Go 1.27 but not on the 1.25 and 1.26 that CI runs. v1 generates `struct { Open }`, embedding the referenced type in an unnamed struct. Before Go 1.27, encoding/json doesn't call Open's UnmarshalJSON for list and map elements of such a struct, so their additional properties were lost. This predates the fix and v1 doesn't use it; the cell is marked broken for v1, with that reason, so its generated code stays pinned while its round trips are skipped. Review asked for a union round trip too: allof_single_ref_union does the same for `allOf: [$ref Either]` over a oneOf, under v1, v2 and v3, with v1 marked broken for the same reason. Without the fix, its v2 cell loses the union's data at every body position. The new cells pass on Go 1.25, 1.26 and 1.27. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> * test: skip only the matrix positions a version can't round-trip A shape's `broken:` entry skipped every round trip of a version, so marking v1 broken for the two allOf: [$ref X] shapes also dropped the positions v1 handles, although only holder-inline fails: the list and map elements of v1's unnamed `struct { X }`, before Go 1.27. A `broken:` entry can now map positions to reasons, and only those positions are skipped: broken: v1: holder-inline: v1 embeds Open in an unnamed struct. ... The existing form, a reason for every position, is unchanged, and so is every cell that uses it. The two shapes now run v1's other eight positions, on Go 1.25, 1.26 and 1.27. 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 3 of the
schema-merging-behavior: v3work, after #2564, #2568 and #2572. It changes only v3, which is still opt-in and undocumented. v1 and v2 output doesn't change.What changes
OpenAPI 3.0 allows nothing next to a
$ref, so specs wrap one in an allOf to annotate it. Framework-generated specs do this constantly:v2 merges such an allOf like any other. The result is a copy of
Named: a new struct, or, for an enum, a new enum type with a duplicate set of constants. v3 now generates it asNameditself:allOf: [$ref Named, {description: …}]type Described struct { … }(a copy of Named)type Described = NamedallOf: [$ref Named, {nullable: true}]*struct { … }*NamedallOf: [$ref Named, {x-go-type-skip-optional-pointer: true}]struct { … }NamedallOf: [$ref Color, {description: …}], where Color is an enumtype Paint string, with constantsPaintRed,PaintGreentype Paint = ColorallOf: [$ref Named, {type: 'null'}]*struct { … }*Namedv3 already did this for
$refs into other documents and forx-go-type(#2568). This PR applies the same rule to local$refs.Which members count as annotations
$refwhen it has no keyword that shapes a Go type. Documentation,nullableand a 3.1"null"type,readOnly/writeOnly,default, validation constraints, and extensions other thanx-go-typeall qualify.$ref's own type, such as{type: object, description: …}next to an object, still counts as an annotation. v3 can read the referenced schema, so it can check the types match.properties,required, anenum, or a differenttype.$refstands for that$ref, soallOf: [{allOf: [$ref Named, {nullable: true}]}, {description: …}]isNamed.x-go-type-skip-optional-pointeron an annotating member applies to the result.x-go-type-namenames an alias:type Renamed = TheName,type TheName = Named.Edge cases
Aliases of themselves. Go rejects a type that is an alias of itself. A component such as
Self: allOf [$ref Self, {description: …}], or two components that each annotate the other, would produce one. Those compositions aren't collapsed: they merge as before, which gives v2's output. A property is never affected, because a field of the enclosing type, such asNext *Node, is not an alias definition.Generated
MarshalJSONbehind an alias. The strict server wraps a response in a defined type over the response's Go type, so it needs a delegatingMarshalJSONwhen that type has a generated one: without it, additional properties and union data are silently lost (#2549). The check for that stopped at the alias, so collapsing a$refto a type with additional properties or a union broke those responses.Schemanow records the type an allOf is an alias of (aliasOf, set only by v3), andgeneratesMarshalJSONfollows it. This also covers a single-memberallOf: [$ref X], where v2 has the same gap. That is a pre-existing v2 bug, to be fixed separately.Tests
pkg/codegen/merge_schemas_decorator_v3_test.gocovers the collapse for components, properties, enums, restated types, nested wrappers,x-go-type-nameandx-go-type-skip-optional-pointer. It also covers the cases that still merge, 3.1, alias chains, self-referencing and mutually referencing components, and the strict envelope's delegation.allof_decorator,allof_nullable_decorator,allof_readonly,allof_default,allof_unique_items,allof_arrayand the threeallof_nested_own_properties_*shapes. That's about 1,850 lines fewer, because the copies are gone. Every cell still round-trips its samples through the client and strict server, and all 42 v3 cells pass. No v1 or v2 cell changes.Fixtures
make generatechanges no committed file. Regenerating every fixture under v3, the only differences are a$ref's name where the same type used to be spelled out. For example,internal/test/schemas/nullablegetsnullable.Nullable[DecoratableValue]instead ofnullable.Nullable[string], whereDecoratableValueis an alias ofstring.🤖 Generated with Claude Code