Repository navigation
feat(v3): merge allOf members by what allOf means - #2572
Merged
mromaszewicz merged 5 commits intoSep 24, 2026
Merged
Conversation
Contributor
|
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
force-pushed
the
aggregates/v3-projection-merge
branch
from
September 24, 2026 20:39
40d8e40 to
f8781ee
Compare
This was referenced Sep 24, 2026
This was referenced Sep 26, 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 2 of the
schema-merging-behavior: v3work, 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,
integerandnumberconflict, and a member's own keywords next to a nested allOf are dropped. v3 replaces that with one merge,allOfMergeinmerge_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.typeintegernarrowsnumber, and[integer, number]isnumberenumx-enum-varnames;x-oapi-codegen-enum-merge: unionopts back into the unionconstdiscriminatorrequiredpropertiesitems,additionalPropertiesadditionalPropertiesschemas are an errorDocumentation, 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.
v2 generates
Name any, because the second member's{nullable: true}replaces Base's property. v3 generatesName *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 asx-go-name.Types narrow.
allOf: [{type: number}, {type: integer}]fails in v2 withcan not merge incompatible types: [number], [integer]. v3 generatesint.Enums intersect (#1633).
v2 generates
SubjectRed,SubjectGreen,SubjectBlueandSubjectViolet; v3 generatesSubjectGreenandSubjectBlue. That is what allOf means, and what kin-openapi'sopenapi3filterandlibopenapi-validatoraccept.A composition that uses allOf to add values can ask for the union:
This generates
active,inactiveandarchived. 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
$refor by their position in the composition: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.gocovers 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:
allof_integer_number(v2 rejects it);allof_enum_intersection,allof_enum_disjoint(v3 rejects it) andallof_enum_union;allof_property_refineandallof_property_conflict(v3 rejects it);allof_nested_own_properties_map,_emptyand_union, where v2 keeps a map or a bare union rather than add the property (fix: keep the properties an allOf member declares next to its own allOf #2571).The
allof_nested_own_propertiesshape 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 generatechanges no committed file. Regenerating every fixture under v3 shows three differences:internal/test/schemas/enums/componentscombines enums with allOf and relies on v2's union; one of its compositions has no value in common. Its config now pinsschema-merging-behavior: v2.internal/test/aggregates/hoistingkeeps a description on a base property that v2 dropped along with the rest of the property.examples/anyof-allof-oneof'sIdentityWithDuplicateField, the README's example of last-wins, is now an error. A later docs change updates it before v3 becomes the default.Next: a
$refplus 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