Skip to content

feat: add compatibility.schema-merging-behavior and version the aggre… - #2563

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:aggregates/phase2
Sep 24, 2026
Merged

mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:aggregates/phase2

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

…gate code

Adds compatibility.schema-merging-behavior, which selects how allOf is merged into Go types and how anyOf/oneOf become Go unions, in sequential versions:

  • v1: $ref allOf members are embedded in the Go struct, and the fields of inline members are inlined. This is what old-merge-schemas: true does; that option is now a deprecated alias for v1 that prints a warning, and combining it with v2 is an error.
  • v2: allOf members are merged keyword by keyword into one Go type, and anyOf/oneOf become a union type with As*/From*/Merge* methods. Still the default.

Unknown values are an error. Validate and Generate reject the same settings through one function, CompatibilityOptions.schemaMergingVersion.

The code behind each version now lives in files and functions named for it: merge_schemas_v1.go, merge_schemas_v2.go and union_v2.go. These take bug fixes but must not regress; new behavior goes into a new version. generateGoSchema reaches them through generateAllOf, which hands generateAllOfV2 the version's merge function, and generateUnions.

Generated output does not change for any spec or configuration.

Tests:

  • The aggregate test matrix moves from matrix// to matrix///, each package's config pinned to its version. The v2 packages are the previous matrix, byte-identical apart from package names. The hand-written tests moved with them, changed only in their package names and a header comment. The allOf shapes also run under v1. Nine of them can't round-trip under v1: it drops additionalProperties and unions, and turns an allOf over an array or a string into a struct. Their code is still generated and compiled, and the round trips are skipped with the reason recorded in the shape file.
  • The tests that call the v2 merge functions directly move to merge_schemas_v2_test.go, with the calls renamed; their expectations are unchanged.
  • New tests cover validation, version resolution, Generate rejecting invalid settings, and v1 generating what old-merge-schemas does.
  • .greptile/rules.md asks reviewers to check that the tests covering v1 and v2 are never updated to accept a breaking change.

…gate code

Adds `compatibility.schema-merging-behavior`, which selects how allOf is
merged into Go types and how anyOf/oneOf become Go unions, in sequential
versions:

- v1: $ref allOf members are embedded in the Go struct, and the fields of
  inline members are inlined. This is what `old-merge-schemas: true`
  does; that option is now a deprecated alias for v1 that prints a
  warning, and combining it with v2 is an error.
- v2: allOf members are merged keyword by keyword into one Go type, and
  anyOf/oneOf become a union type with As*/From*/Merge* methods. Still the
  default.

Unknown values are an error. Validate and Generate reject the same
settings through one function, CompatibilityOptions.schemaMergingVersion.

The code behind each version now lives in files and functions named for
it: merge_schemas_v1.go, merge_schemas_v2.go and union_v2.go. These take
bug fixes but must not regress; new behavior goes into a new version.
generateGoSchema reaches them through generateAllOf, which hands
generateAllOfV2 the version's merge function, and generateUnions.

Generated output does not change for any spec or configuration.

Tests:
- The aggregate test matrix moves from matrix/<shape>/ to
  matrix/<version>/<shape>/, each package's config pinned to its version.
  The v2 packages are the previous matrix, byte-identical apart from
  package names. The hand-written tests moved with them, changed only in
  their package names and a header comment. The allOf shapes also run
  under v1. Nine of them can't round-trip under v1: it drops
  additionalProperties and unions, and turns an allOf over an array or a
  string into a struct. Their code is still generated and compiled, and
  the round trips are skipped with the reason recorded in the shape file.
- The tests that call the v2 merge functions directly move to
  merge_schemas_v2_test.go, with the calls renamed; their expectations
  are unchanged.
- New tests cover validation, version resolution, Generate rejecting
  invalid settings, and v1 generating what old-merge-schemas does.
- .greptile/rules.md asks reviewers to check that the tests covering v1
  and v2 are never updated to accept a breaking change.

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 06:20
@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; no actionable regression was established.

Summary

The PR adds a versioned schema-merging setting while retaining v2 as the default and treating old-merge-schemas as a deprecated v1 alias.

  • Moves allOf and union generation into version-named implementations.
  • Pins the aggregate test matrix to v1 or v2 and adds validation and compatibility tests.

Reviews (1) · Last reviewed commit: "feat: add compatibility.schema-merging-b..."

@mromaszewicz
mromaszewicz merged commit 653a28d into oapi-codegen:main Sep 24, 2026
16 checks passed
@mromaszewicz
mromaszewicz deleted the aggregates/phase2 branch September 24, 2026 06:24
mromaszewicz added a commit that referenced this pull request Sep 26, 2026
* refactor: one sameSchema for schema-merging-behavior v2 and v3

sameSchemaV2 and sameSchemaV3 were byte-identical: the one function of
the v2 copy that v3 never changed. It is now sameSchema, in
merge_schemas.go with the code every version shares. Generated output is
unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* refactor: share the allOf recursion and alias bookkeeping between v2 and v3

generateAllOfV2 and generateAllOfV3 began and ended with the same 69
lines: the reference to a composition an enclosing frame is already
generating (issue #2542), and, after the merge, the alias target for
generatesMarshalJSON, the description, x-go-type-skip-optional-pointer
and the naming of a recursive composition. They are now
genContext.inProgressAllOf and finishAllOf in merge_schemas.go, whose
file comment says that the versions move in step for the code there.
Each version keeps only its own middle. Generated output is unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* refactor: share the nullable-union collapse between v2 and v3, in union.go

generateUnionV2 and generateUnionV3 had the same 54 lines to collapse a
oneOf or anyOf whose only branch besides a 3.1 null one is that branch.
They are now effectiveBranches and collapseNullableUnion in a new
union.go, the anyOf/oneOf counterpart of merge_schemas.go. generateUnions
and the union helpers every version reads (unionTextKinds,
discriminatorValueType, findProperty, propertyNames, scalarType, and the
Schema methods for the union template's text binding) move there from
schema.go unchanged. Generated output is unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* refactor: drop dead code and constant parameters from the allOf and union code

- refToGoTypeMap in generateUnionV2 and generateUnionV3 was written and
  never read.
- generateAllOfV3 was always handed mergeSchemasV3; it calls it directly.
- opaqueSchemaFor was always called with a nil seen set.
- allOfMerge.member was set by every caller just before add and cleared
  at the top of add: a parameter passed through a field. It is add's
  first parameter now.
- The label of a nested allOf member was computed once in add and once
  in allOfMemberLabel; allOfMemberLabel takes the parent's label.
- MergeSchemas and Discriminator.PropertyName had no caller in the repo,
  templates included.

Generated output and error messages are unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* refactor: drop the V2/V3 suffix from one-version allOf and union helpers

A V2 or V3 suffix marks a function with a same-named counterpart in
another schema-merging-behavior version. #2563 suffixed every function
of the v2 files and #2564 copied them all to v3, but v3 has since
replaced its copies with code of other names, which left these without
a counterpart: isExtensionOnlySchema, valueWithPropagatedRef,
propagateRemoteRefs, qualifyRemoteRef, mergeAllOf, mergeOpenapiSchemas,
sameTypeSet, mergeTypes, mergeItems and addNestedOwnProperties in v2,
and withoutConstraintOnlyUnions and generateUnionComponents in v3. The
suffix stays on the twins: mergeSchemas, generateAllOf, generateUnions,
generateUnion, hasStructuralSiblings and isConstraintOnlyUnion.
Generated output is unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* refactor: one union.stamp template for the From* and Merge* discriminator stamp

union.tmpl had the same eight lines twice, assigning the stamped
discriminator value to the union struct's own field in From* and in
Merge*. They are now a union.stamp template, called from both. The
template keeps the indentation and trim markers of the inline blocks, so
generated code is unchanged with and without skip-fmt.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* refactor(v3): walk allOf trees and alias chains with one iterator each

v3 walked the tree of a schema and its allOf members, all the way down,
in five places, each with its own nil checks, seen set and recursion:
opaqueSchemaWithin, allOfMerge.markFlattening, declaredVariantKeys,
pinnedDiscriminatorValue and discriminatorPropertyTyped. They now range
over allOfTree, which yields the schemas in the same order and stops a
cycle the same way. standsFor and declaredTypes keep their own walks:
one judges the other members of each composition, the other folds the
types.

aliasGeneratesMarshalJSON and v3's variantKeys followed a schema's
aliases with the same loop; it is Schema.aliasChain now, and each keeps
its own check per step.

Generated output is unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* refactor: appendUnique for the append-unless-present idiom

Ten places in the allOf and union code appended a value to a slice
unless slices.Contains found it there, three lines each; one wrapped the
idiom in a closure. They call appendUnique now. The order of every slice
is as before, so generated output is unchanged.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* docs: bring the allOf and union comments up to date with the code

- gencontext.go said the allOf code lives in merge_schemas_v2.go and
  that generateAllOfV2 registers, names and re-checks a recursive
  composition; both versions do, through inProgressAllOf and
  finishAllOf.
- rootPosition was described as a diagnostic for a case believed
  impossible. v3 reads it to keep a component from being defined as an
  alias of itself (annotatedRefMember), which TestAnnotatedRefV3Aliases
  covers.
- schema.go and externalref.go named generateUnionV2 for what every
  version's union generator does, and aliasOf's setters had moved.
- A comment in mergeSchemasV3 ended with an empty line.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
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.

1 participant