Skip to content

feat(v3): allOf can only annotate external and x-go-type schemas - #2568

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

mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
mromaszewicz:aggregates/phase3

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

schema-merging-behavior v3 no longer reads the schema of an allOf member that is in another document, or that x-go-type replaces with a Go type. Another generator run owns an external schema's type, and an x-go-type's fields are unknown, so neither can be merged into a new struct.

  • Such a member plus members that only annotate it (description, nullable, readOnly, validation constraints, extensions other than x-go-type) is that member's type: allOf: [$ref ext#/User, {nullable: true}] is *externalRef0.User rather than a copy of User's body.
  • Combined with anything that shapes a Go type (properties, required, type, another $ref, the parent's own properties), it is an error that names both members. For an external schema it says that a reference to another document can't be merged with other allOf members, and to define the schema in this document; for x-go-type, to give the composition an x-go-type of its own. Neither suggests v2: v2 generates code for these shapes, but not correct code. In particular, v2 drops an x-go-type member's type without a word (Generating Struct with Embedded Imported Type #1622); v1 embeds it, if that's what the spec author wants.
  • A local component that is only a $ref to another document, directly or through other such components, counts as external: kin-openapi hands us the other document's schema for it, and copying it produced references to types this package doesn't have. So does a local schema that is an annotated external or x-go-type schema.
  • Every member read goes through one function that refuses these schemas, so nested allOfs and array items can't copy one either.
  • External refs inside a member (properties, items, additionalProperties) and external oneOf/anyOf variants are just type names and keep working.
  • v3 drops the remote-ref rewriting that flattening external schemas needed (allOf with cross-file does not qualify nested type references from source schema #2288, Stack overflow in propagateRemoteRefs when a self-recursive external schema is composed via allOf more than once #2557).
  • Errors from a nested allOf merge now keep their cause.

The aggregate matrix gains a rejects: shape key: a version that rejects a shape gets a cell with the spec and config but no generated code, and a test that expects the error. Two new x-go-type shapes use it. They also add new v2 cells; no existing v1 or v2 cell changes.

Three fixtures test shapes v3 rejects, so their configs now pin schema-merging-behavior: v2: #2288 (multipackage/packageA), #2470 (multipackage/discriminated_union_allof) and the #2335 overlay fixture (overlays/allof_merge_extensions). v2 is the default, so their generated code is unchanged. Regenerating every fixture under v3 found no other rejection. The only other difference is in extensions/x_go_type, where an annotated $ref to an x-go-type schema becomes the component's alias, which is the same Go type.

Closes: #1622
Refs: #2288
Refs: #2335
Refs: #2470
Refs: #2557

schema-merging-behavior v3 no longer reads the schema of an allOf member
that is in another document, or that x-go-type replaces with a Go type.
Another generator run owns an external schema's type, and an x-go-type's
fields are unknown, so neither can be merged into a new struct.

- Such a member plus members that only annotate it (description,
  nullable, readOnly, validation constraints, extensions other than
  x-go-type) is that member's type: `allOf: [$ref ext#/User,
  {nullable: true}]` is `*externalRef0.User` rather than a copy of
  User's body.
- Combined with anything that shapes a Go type (properties, required,
  type, another $ref, the parent's own properties), it is an error that
  names both members. For an external schema it says that a reference
  to another document can't be merged with other allOf members, and to
  define the schema in this document; for x-go-type, to give the
  composition an x-go-type of its own. Neither suggests v2: v2 generates
  code for these shapes, but not correct code. In particular, v2 drops
  an x-go-type member's type without a word (oapi-codegen#1622); v1 embeds it, if
  that's what the spec author wants.
- A local component that is only a $ref to another document, directly
  or through other such components, counts as external: kin-openapi
  hands us the other document's schema for it, and copying it produced
  references to types this package doesn't have. So does a local schema
  that is an annotated external or x-go-type schema.
- Every member read goes through one function that refuses these
  schemas, so nested allOfs and array items can't copy one either.
- External refs inside a member (properties, items,
  additionalProperties) and external oneOf/anyOf variants are just type
  names and keep working.
- v3 drops the remote-ref rewriting that flattening external schemas
  needed (oapi-codegen#2288, oapi-codegen#2557).
- Errors from a nested allOf merge now keep their cause.

The aggregate matrix gains a `rejects:` shape key: a version that
rejects a shape gets a cell with the spec and config but no generated
code, and a test that expects the error. Two new x-go-type shapes use
it. They also add new v2 cells; no existing v1 or v2 cell changes.

Three fixtures test shapes v3 rejects, so their configs now pin
schema-merging-behavior: v2: oapi-codegen#2288 (multipackage/packageA), oapi-codegen#2470
(multipackage/discriminated_union_allof) and the oapi-codegen#2335 overlay fixture
(overlays/allof_merge_extensions). v2 is the default, so their
generated code is unchanged. Regenerating every fixture under v3 found
no other rejection. The only other difference is in
extensions/x_go_type, where an annotated $ref to an x-go-type schema
becomes the component's alias, which is the same Go type.

Closes: oapi-codegen#1622
Refs: oapi-codegen#2288
Refs: oapi-codegen#2335
Refs: oapi-codegen#2470
Refs: oapi-codegen#2557
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 14:55
@mromaszewicz mromaszewicz added the notable changes Used for release notes to highlight these more highly label 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 based on the issues assessed in this review.

Summary

The PR makes v3 treat external and x-go-type allOf members as types that may be annotated but not structurally merged. It also adds rejection cases to the aggregate matrix and pins affected existing fixtures to v2.

Reviews (2) · Last reviewed commit: "fix(v3): address review of the opaque al..."

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

This comment has been minimized.

- A $ref to a whole document (`./user.yaml`, no fragment) is no longer
  treated as opaque. oapi-codegen has no Go type for such a schema and
  inlines it wherever it's used, so v3 merges it like an inline schema.
  Before, v3 rejected merging one and still copied it when annotated.
- Array items that merge an opaque schema with an annotating one keep
  the annotations: `items: {nullable: true}` next to `items: $ref
  ext#/User` now gives `[]*externalRef0.User`, not
  `[]externalRef0.User`.
- x-go-type-name on a member that annotates an opaque one names the
  composition's type, as it does anywhere else (`type MyUser =
  externalRef0.User`), instead of being ignored.
- Errors name the member that brings an opaque schema in: a member
  whose own allOf includes one ("#/components/schemas/Inner (whose
  allOf includes ./common.yaml#/...)"), where the error used to name
  only the external schema. They call the schema's own properties
  "the schema's own properties" rather than "an inline schema", show an
  inline member's x-go-type, and for OpenAPI 3.1 specs spell nullable
  as `type: "null"`.
- v3 rejects old-allof-sibling-merging, so generateAllOfV3 no longer
  checks it, and the merge no longer deletes an x-go-type that can't
  reach it.
- New tests for 3.1 specs, whole-document refs, nested members,
  nullable array items, x-go-type-name, and x-go-type items,
  x-go-type-skip-optional-pointer and $ref siblings.

v1 and v2 are unchanged: regenerating the repository changes no file.
Regenerating every fixture under v3 gives the same result as before.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mromaszewicz
mromaszewicz merged commit a1f7077 into oapi-codegen:main Sep 24, 2026
15 checks passed
@mromaszewicz
mromaszewicz deleted the aggregates/phase3 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

notable changes Used for release notes to highlight these more highly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Generating Struct with Embedded Imported Type

1 participant