Skip to content

fix: keep the properties an allOf member declares next to its own allOf - #2571

Merged
mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/v2-nested-allof-properties
Sep 24, 2026
Merged

mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/v2-nested-allof-properties

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

When an allOf member is itself a schema with an allOf, the v2 merge replaces the member with the merge of its allOf, which loses the properties it declares next to that allOf:

Mid:
type: object
properties:
b: {type: string}
allOf:
- $ref: '#/components/schemas/Named' Subject: allOf:
- $ref: '#/components/schemas/Mid' - properties: c: {type: string}

Subject had Name and C but no B, so b was dropped when decoding and encoding. The same happened to array items that v2 merges.

The fix is additive. After the merge, the properties such members declare are added, with whether they are required, but only where no other member declares the same property: that member's schema won before, and still does, so no existing field changes type. Nothing is added under old-allof-sibling-merging, which keeps the output that discards a schema's own properties next to its allOf.

This changes v2's output only by adding fields, and no generated fixture in the repository changes. A new matrix shape, allof_nested_own_properties, covers it, with a test in merge_schemas_v2_test.go that also checks an earlier member's property isn't replaced.

When an allOf member is itself a schema with an allOf, the v2 merge
replaces the member with the merge of its allOf, which loses the
properties it declares next to that allOf:

  Mid:
    type: object
    properties:
      b: {type: string}
    allOf:
      - $ref: '#/components/schemas/Named'
  Subject:
    allOf:
      - $ref: '#/components/schemas/Mid'
      - properties:
          c: {type: string}

Subject had Name and C but no B, so b was dropped when decoding and
encoding. The same happened to array items that v2 merges.

The fix is additive. After the merge, the properties such members
declare are added, with whether they are required, but only where no
other member declares the same property: that member's schema won
before, and still does, so no existing field changes type. Nothing is
added under old-allof-sibling-merging, which keeps the output that
discards a schema's own properties next to its allOf.

This changes v2's output only by adding fields, and no generated
fixture in the repository changes. A new matrix shape,
allof_nested_own_properties, covers it, with a test in
merge_schemas_v2_test.go that also checks an earlier member's property
isn't replaced.

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 20:14
@mromaszewicz mromaszewicz added the bug Something isn't working label Sep 24, 2026
@greptile-apps

greptile-apps Bot commented Sep 24, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

The PR does not yet appear safe to merge because a nested member’s sole named property still lacks a generated field.

Findings

  1. P1 Sole named property remains lost ▶
Fix with agent prompt
### Issue 1
pkg/codegen/merge_schemas_v2.go:572
When a nested `allOf` member is the only source of named properties-for example, `Mid` declares `b` beside an `allOf` containing an empty object-the initial merge has no properties, so this guard skips recovery. The generated type remains a generic map instead of exposing a typed `B` field. This valid form of the reported bug remains unfixed.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

This PR restores properties declared beside a nested allOf in v2 schema merging and adds a generated matrix fixture with round-trip coverage.

  • The latest changes adjust property precedence and requiredness, and avoid changing map-shaped outputs.
  • The empty-property guard also leaves one nested-property case unrecovered.

Reviews (2) · Last reviewed commit: "fix: only add nested allOf properties wh..."

Comment thread pkg/codegen/merge_schemas_v2.go Resolved
Comment thread pkg/codegen/merge_schemas_v2.go Resolved
Comment thread pkg/codegen/merge_schemas_v2.go Resolved
…ing else

Review of the previous commit found three ways it could change existing
output rather than only add to it:

- A merge without properties generates a map (from
  additionalProperties, or map[string]any for an empty object) or a
  bare union. Adding a property turned it into a struct, which breaks
  code that uses the map. Nothing is added to such a merge any more;
  its dropped properties stay dropped, as before.
- When several dropped declarations name the same property, the first
  one won. The later one wins now, as everywhere else in v2's merge,
  with a member's own properties after its allOf's.
- A property added from one declaration ignored a later declaration
  that requires it. An added property is now required when any dropped
  declaration requires it. Dropped required lists still don't touch
  properties that were already there.

Tests cover each case. No generated fixture changes.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
Comment thread pkg/codegen/merge_schemas_v2.go Resolved
@mromaszewicz
mromaszewicz merged commit 11315dd into oapi-codegen:main Sep 24, 2026
15 checks passed
mromaszewicz added a commit that referenced this pull request Sep 24, 2026
A review of #2571 asked why a nested allOf member that is the only
source of named properties still loses them. It is deliberate: v2
generates such a merge as a map (or a union), and adding a property
would turn it into a struct, breaking code that uses the map. The
comment on addNestedOwnPropertiesV2 and the test that covers the case
now say so, and that schema-merging-behavior v3 keeps them.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
mromaszewicz added a commit to mromaszewicz/oapi-codegen that referenced this pull request Sep 24, 2026
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]>
mromaszewicz added a commit to mromaszewicz/oapi-codegen that referenced this pull request Sep 24, 2026
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 added a commit that referenced this pull request Sep 24, 2026
* feat(v3): merge allOf keywords by their meaning, and name conflicts

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 (#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 #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 #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: #1633
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* feat(v3): merge a property that several allOf members declare

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` (#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 #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: #2107
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>

* feat(v3): let a composition merge its enums into their union

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]>

* fix(v3): keep a repeated $ref's extensions, and read [integer, number] 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]>

* test(v3): cover a nested member's own properties where v2 keeps a map

#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]>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
@mromaszewicz
mromaszewicz deleted the fix/v2-nested-allof-properties branch September 26, 2026 00:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant