Skip to content

feat(v3): an allOf that only annotates a $ref is that $ref's type - #2574

Merged
mromaszewicz merged 3 commits into
oapi-codegen:mainfrom
mromaszewicz:aggregates/v3-decorator-collapse
Sep 24, 2026
Merged

mromaszewicz merged 3 commits into
oapi-codegen:mainfrom
mromaszewicz:aggregates/v3-decorator-collapse

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Step 3 of the schema-merging-behavior: v3 work, 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:

Described:
  allOf:
    - $ref: '#/components/schemas/Named'
    - description: A described Named.

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 as Named itself:

Schema v2 v3
component allOf: [$ref Named, {description: …}] type Described struct { … } (a copy of Named) type Described = Named
property allOf: [$ref Named, {nullable: true}] *struct { … } *Named
property allOf: [$ref Named, {x-go-type-skip-optional-pointer: true}] struct { … } Named
component allOf: [$ref Color, {description: …}], where Color is an enum type Paint string, with constants PaintRed, PaintGreen type Paint = Color
3.1 property allOf: [$ref Named, {type: 'null'}] *struct { … } *Named

v3 already did this for $refs into other documents and for x-go-type (#2568). This PR applies the same rule to local $refs.

Which members count as annotations

  • A member annotates the $ref when it has no keyword that shapes a Go type. Documentation, nullable and a 3.1 "null" type, readOnly/writeOnly, default, validation constraints, and extensions other than x-go-type all qualify.
  • A member that restates the $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.
  • A member that shapes the type still produces a new type, merged as in feat(v3): merge allOf members by what allOf means #2572. That includes properties, required, an enum, or a different type.
  • An inline allOf that itself only annotates a $ref stands for that $ref, so allOf: [{allOf: [$ref Named, {nullable: true}]}, {description: …}] is Named.
  • x-go-type-skip-optional-pointer on an annotating member applies to the result. x-go-type-name names 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 as Next *Node, is not an alias definition.

Generated MarshalJSON behind an alias. The strict server wraps a response in a defined type over the response's Go type, so it needs a delegating MarshalJSON when 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 $ref to a type with additional properties or a union broke those responses. Schema now records the type an allOf is an alias of (aliasOf, set only by v3), and generatesMarshalJSON follows it. This also covers a single-member allOf: [$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.go covers the collapse for components, properties, enums, restated types, nested wrappers, x-go-type-name and x-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.
  • In the aggregate matrix, the v3 cells for the shapes this affects change accordingly: allof_decorator, allof_nullable_decorator, allof_readonly, allof_default, allof_unique_items, allof_array and the three allof_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 generate changes 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/nullable gets nullable.Nullable[DecoratableValue] instead of nullable.Nullable[string], where DecoratableValue is an alias of string.

🤖 Generated with Claude Code

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]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner September 24, 2026 21:18
@mromaszewicz mromaszewicz added enhancement New feature or request 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; no new actionable issue or outstanding previous finding was identified.

Summary

The PR makes v3 annotation-only allOf wrappers use their referenced Go type, preserves JSON delegation through those aliases, and updates v3 fixtures and tests. Since the previous review, it also prevents enum-name directives from being treated as annotations and checks restated types and formats against external references.

Reviews (3) · Last reviewed commit: "fix(v3): check a type restated next to a..."

Comment thread pkg/codegen/schema.go Resolved
Comment thread pkg/codegen/merge_schemas_v3.go Resolved
@greptile-apps

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]>
Comment thread pkg/codegen/merge_schemas_v3.go Resolved
Comment thread pkg/codegen/merge_schemas_v3.go Resolved
…'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
mromaszewicz merged commit ea4c18d into oapi-codegen:main Sep 24, 2026
15 checks passed
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]>
@mromaszewicz
mromaszewicz deleted the aggregates/v3-decorator-collapse branch September 26, 2026 00:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request notable changes Used for release notes to highlight these more highly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant