Skip to content

feat(v3): merge allOf members by what allOf means - #2572

Merged
mromaszewicz merged 5 commits into
oapi-codegen:mainfrom
mromaszewicz:aggregates/v3-projection-merge
Sep 24, 2026
Merged

mromaszewicz merged 5 commits into
oapi-codegen:mainfrom
mromaszewicz:aggregates/v3-projection-merge

Conversation

@mromaszewicz

@mromaszewicz mromaszewicz commented Sep 24, 2026 •

Copy link
Copy Markdown
Member

Step 2 of the schema-merging-behavior: v3 work, after #2564 and #2568. It changes only v3, which is still opt-in and undocumented. v1 and v2 output doesn't change.

What changes

v2 merges allOf members two at a time, keyword by keyword, with rules that grew one issue at a time: the last member to declare a property replaces it, enums are concatenated, integer and number conflict, and a member's own keywords next to a nested allOf are dropped. v3 replaces that with one merge, allOfMerge in merge_schemas_v3.go. It keeps the keywords that shape a Go type, each merged by what allOf means (a value must satisfy every member). A disagreement no value can satisfy is an error that names both members.

Keyword v2 v3
type error unless the same intersection; integer narrows number, and [integer, number] is number
enum union intersection, keeping x-enum-varnames; x-oapi-codegen-enum-merge: union opts back into the union
const dropped must be the same in every member, and allowed by the enum
discriminator any two are an error the same one may repeat
required concatenated union, each once
properties the last member's wins merged (#2107)
items, additionalProperties items merged on their own; two different additionalProperties schemas are an error merged like properties
a nested member's own keywords dropped (partly restored by #2571) kept

Documentation, validation constraints and nullability never conflict. Nullability is set when any member sets it, as before.

Examples

A member refines a property (#2107). The JSON Merge Patch pattern: reuse a resource's schema and make a field nullable.

Patch:
  allOf:
    - $ref: '#/components/schemas/Base'   # name: {type: string}, required
    - properties:
        name: {nullable: true}

v2 generates Name any, because the second member's {nullable: true} replaces Base's property. v3 generates Name *string: Base's type, made nullable by the member. The merged property is an allOf of the members' property schemas and generates like any allOf, so nested objects merge their properties and conflicts are errors. It keeps the members' descriptions, examples, and field-level extensions such as x-go-name.

Types narrow. allOf: [{type: number}, {type: integer}] fails in v2 with can not merge incompatible types: [number], [integer]. v3 generates int.

Enums intersect (#1633).

Subject:
  allOf:
    - $ref: '#/components/schemas/Color'   # [red, green, blue]
    - enum: [green, blue, violet]

v2 generates SubjectRed, SubjectGreen, SubjectBlue and SubjectViolet; v3 generates SubjectGreen and SubjectBlue. That is what allOf means, and what kin-openapi's openapi3filter and libopenapi-validator accept.

A composition that uses allOf to add values can ask for the union:

ExtendedStatus:
  x-oapi-codegen-enum-merge: union
  allOf:
    - $ref: '#/components/schemas/BaseStatus'   # [active, inactive]
    - enum: [archived]

This generates active, inactive and archived. Without the extension no value is in both enums, and v3 reports that and suggests the extension. Validators still read the allOf literally, so a service that validates requests has to allow the extra values itself. The Go types follow what the spec's author asked for. The extension goes on the schema that has the allOf, or on a member's property that refines a property. v2 ignores it.

Conflicts are errors. Where v2 silently kept one member's schema, v3 fails and names both members, by $ref or by their position in the composition:

allOf can't merge #/components/schemas/Base/properties/age (type integer) with allOf/1/properties/age (type string): no value has both types

Recursion

The allOf built for a merged property is cached by the schemas it is built from. A property that refers back to its own composition therefore builds the same allOf again, which the recursion guard from #2542 recognizes, and it generates a recursive named type.

Tests

  • pkg/codegen/merge_schemas_v3_test.go covers each merge rule, with end-to-end cases for properties, items, additionalProperties, recursion, error messages and the enum extension.

  • The aggregate matrix gets new shapes. Each is generated under both v2 and v3, so the two versions' output can be compared directly:

    The allof_nested_own_properties shape from fix: keep the properties an allOf member declares next to its own allOf #2571 now runs under v3 too. The new v2 cells are additions; no existing cell changes.

Fixtures

make generate changes no committed file. Regenerating every fixture under v3 shows three differences:

  • internal/test/schemas/enums/components combines enums with allOf and relies on v2's union; one of its compositions has no value in common. Its config now pins schema-merging-behavior: v2.
  • internal/test/aggregates/hoisting keeps a description on a base property that v2 dropped along with the rest of the property.
  • examples/anyof-allof-oneof's IdentityWithDuplicateField, the README's example of last-wins, is now an error. A later docs change updates it before v3 becomes the default.

Next: a $ref plus members that only annotate it will generate as that $ref's type, instead of an anonymous copy.

Closes #1633
Closes #2107

🤖 Generated with Claude Code

@mromaszewicz
mromaszewicz requested a review from a team as a code owner September 24, 2026 20:20
@mromaszewicz mromaszewicz added notable changes Used for release notes to highlight these more highly enhancement New feature or request 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 outstanding finding or new actionable issue was established.

Summary

The PR adds an opt-in enum-union mode for v3 allOf merging and expands composition tests. Changes since the previous review also restore nested properties in a v2 merge case and add matrix coverage for it.

Reviews (3) · Last reviewed commit: "test(v3): cover a nested member's own pr..."

Comment thread pkg/codegen/merge_schemas_v3.go Resolved
Comment thread pkg/codegen/merge_schemas_v3.go Resolved
mromaszewicz and others added 5 commits September 24, 2026 13:37
schema-merging-behavior v3 replaces v2's pairwise keyword merge with one
that keeps the keywords that shape a Go type, each with a rule that
follows what allOf means (every member holds), and that reports a
conflict as an error naming both members rather than silently keeping
one side:

- type: the types every member allows. integer narrows number, so
  `allOf: [{type: number}, {type: integer}]` is an integer; v2 rejected
  it. "null" stays nullability, which any member can add.
- enum: the values every member's enum allows, keeping their
  x-enum-varnames names (oapi-codegen#1633). v2 took the union. A member without an
  enum can still rename the values.
- const: the same in every member that has one, and allowed by the enum.
- discriminator: members may repeat the same one; v2 rejected any two.
- required: every member's, once each.
- format: unchanged, one member's or the same in all.

A conflict reads, for example:

  allOf can't merge #/components/schemas/Base/allOf/0 (type object)
  with allOf/1 (type string): no value has both types

Members are named by their $ref, or by their place in the composition
("allOf/1", "allOf/1/allOf/0"). The schema's own keywords next to allOf
are "the schema itself".

A member that is an allOf itself now contributes its own keywords as
well as its members'. v2 dropped them; since oapi-codegen#2571 it adds back the
properties where that only adds fields, and v3 keeps them all.

The matrix gains three shapes: integer narrowing number (which v2
rejects), overlapping enums, and disjoint enums (which v3 rejects). They
add new v2 cells; no existing cell changes. The allof_nested_own_properties
shape from oapi-codegen#2571 now runs under v3 too.

internal/test/schemas/enums/components uses allOf to combine enums,
relying on v2's union, and one of its compositions has no value in
common, so its config now pins schema-merging-behavior: v2. Its
generated code is unchanged. Regenerating every fixture under v3 found
no other change.

Closes: oapi-codegen#1633
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
In v2 the last allOf member to declare a property decides its schema, so
a member that only refines a property replaces it: `allOf: [$ref Base,
{properties: {name: {nullable: true}}}]`, the way a JSON Merge Patch
body reuses a resource's schema, turns Base's `name string` into
`name any` (oapi-codegen#2107).

schema-merging-behavior v3 merges them instead. Different schemas that
members declare for one property become an allOf of those schemas,
which generates like any other allOf, so every allOf rule applies at the
property too: name above stays a string and becomes nullable, nested
objects merge their properties, and types that conflict are an error
naming both, such as

  allOf can't merge #/components/schemas/Base/properties/age
  (type integer) with allOf/1/properties/age (type string): no value
  has both types

The merged property keeps its members' descriptions, examples,
readOnly, writeOnly and deprecated, and field-level extensions such as
x-go-name, which the generator reads from the property itself. Array
items and additionalProperties merge the same way; v2 merged items
separately and rejected two different additionalProperties schemas.

The same schemas always make the same allOf, so a merged property that
refers back to the composition is recognised as recursion, by the guard
added for oapi-codegen#2542, instead of recursing forever.

The matrix gains two shapes: a member refining a property (v2 generates
`Name any`, v3 `Name *string`), and one declaring it with a conflicting
type (which v3 rejects). They add new v2 cells; no existing cell
changes.

Regenerating every fixture under v3 finds two more differences:
- internal/test/aggregates/hoisting keeps a base property's description
  that v2 dropped along with the rest of the property;
- examples/anyof-allof-oneof's IdentityWithDuplicateField, the README's
  example of last-wins, is now an error. The plan already has the docs
  step update that example and the README before v3 becomes the default.

Closes: oapi-codegen#2107
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
schema-merging-behavior v3 merges an allOf's enums into their
intersection, which is what allOf means and what validators such as
kin-openapi's openapi3filter and libopenapi-validator check. Some specs
use allOf to add values to an enum instead:

  ExtendedStatus:
    x-oapi-codegen-enum-merge: union
    allOf:
      - $ref: '#/components/schemas/BaseStatus'   # [active, inactive]
      - enum: [archived]

Read as allOf, no value is valid, so v3 rejects it unless the
composition sets x-oapi-codegen-enum-merge: union, which allows the
values of any member's enum: active, inactive and archived. Validators
still read the allOf literally, so a service that validates requests
has to be told about the extra values too; the Go types are what the
spec's author asked for. The other value, intersection, is the default.

- The extension goes on the schema that has the allOf. For a property
  that a member refines, it can go on the member's property, since the
  merged property carries its members' extensions.
- A member that is itself a union composition is merged its own way
  first, so decorating ExtendedStatus keeps its three values.
- Values keep their x-enum-varnames names in both modes. A value that
  no member names is named after itself, as it is when no member names
  any.
- The error for enums with no value in common now suggests the
  extension.
- An invalid value is an error. v2 ignores the extension; it always
  takes the union.

The matrix gains an allof_enum_union shape, with a new v2 cell.

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

Review fixes for the allOf merge:

- Two members that declare a property with the same $ref, but different
  extensions next to it, were treated as one schema, so the later
  member's extensions, such as x-go-name, were lost. The property stays
  that one $ref, so it keeps its type, and gets both members'
  extensions, the later member's winning.
- Merging `type: [integer, number]` with `type: number` gave
  [integer, number], which generates as `any`. number already allows
  every integer, so such a list is number, and the merge gives number.
- The allOf a merge makes for a position is now kept by the schemas it
  was made from rather than by the result, so the $ref above is made
  once too. Otherwise a recursive schema would make a new one each time,
  and the recursion guard would never see it again.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
oapi-codegen#2571 made v2 keep the properties an allOf member declares next to its
own allOf, but only where the merge already has properties: elsewhere
v2 generates a map or a bare union, and adding a field would change that
type. Three shapes cover those cases under both versions:

- next to typed additionalProperties: v2 `map[string]string`, v3 a
  struct with B and AdditionalProperties;
- next to an empty object: v2 `map[string]any`, v3 a struct with B;
- next to a oneOf: v2 a bare union, v3 the union with a B field.

Every cell round-trips its samples: v2's map and union keep b as data,
v3 gives it a field. The v2 cells are new; no existing cell changes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@mromaszewicz
mromaszewicz force-pushed the aggregates/v3-projection-merge branch from 40d8e40 to f8781ee Compare September 24, 2026 20:39
@mromaszewicz mromaszewicz changed the title feat(v3): let a composition merge its enums into their union feat(v3): merge allOf members by what allOf means Sep 24, 2026
@mromaszewicz
mromaszewicz merged commit 96133d3 into oapi-codegen:main Sep 24, 2026
15 checks passed
@mromaszewicz
mromaszewicz deleted the aggregates/v3-projection-merge 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.

Merge properties when combining schemas with allOf feat: make enum union/intersection generation more configurable

1 participant