Skip to content

refactor: remove duplication in the allOf and union code - #2584

Merged
mromaszewicz merged 9 commits into
oapi-codegen:mainfrom
mromaszewicz:refactor/v3-dedupe
Sep 26, 2026
Merged

mromaszewicz merged 9 commits into
oapi-codegen:mainfrom
mromaszewicz:refactor/v3-dedupe

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

A cleanup of the allOf and union code after the schema-merging-behavior work (#2562 to #2580). v3 began as a copy of v2 (#2564), and each later PR changed one part of it. So some code stayed duplicated between the versions, and some helpers were left behind. This removes that duplication. Generated code doesn't change for any version.

What changes

  • Shared between v2 and v3, in merge_schemas.go and a new union.go:
  • Removed:
    • a write-only map in both union generators;
    • parameters that always got the same value;
    • MergeSchemas and Discriminator.PropertyName(), which nothing in the repo uses;
    • a struct field that passed a parameter to allOfMerge.add.
  • Renamed: helpers that exist in one version only lose their V2/V3 suffix. Functions with a same-named counterpart in the other version keep it.
  • Deduplicated:
    • union.tmpl's two copies of the discriminator stamp become one union.stamp template;
    • v3's hand-written allOf-tree walks use one iterator, allOfTree, and alias chains use Schema.aliasChain;
    • a small appendUnique helper replaces a repeated idiom.
  • Comments: several that described earlier versions of the code are brought up to date.

Verification

The code for every version generated byte-identical output before and after:

  • all 307 go:generate targets under internal/test, formatted and with skip-fmt, compared with a harness;
  • separately, all 360 fixtures with skip-fmt, compared by running the command-line binaries built from main and from this branch.

The only differences were in the order of import lines, which varied between runs of the same binary until #2582.

make generate changes nothing. make test and make lint pass, and so does the aggregate matrix on Go 1.25.

🤖 Generated with Claude Code

mromaszewicz and others added 9 commits September 25, 2026 22:09
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]>
…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 oapi-codegen#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]>
…on.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]>
…nion 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]>
A V2 or V3 suffix marks a function with a same-named counterpart in
another schema-merging-behavior version. oapi-codegen#2563 suffixed every function
of the v2 files and oapi-codegen#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]>
…ator 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]>
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]>
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]>
- 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]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner September 26, 2026 05:12
@greptile-apps

greptile-apps Bot commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Refactors code generation for schema merging and unions.

The PR should not merge until the removed exported codegen entry point is addressed.

Findings

  1. P1 Exported merge function removed ▶
Fix with agent prompt
### Issue 1
pkg/codegen/merge_schemas.go:19
If downstream code calls `codegen.MergeSchemas(allOf, path)`, removing this exported function makes that code fail to compile against the new version. There is no exported replacement with the same signature. Please retain a compatibility wrapper if this API is meant to remain available.

---

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

Summary

This PR consolidates shared allOf and union-generation code, extracts a discriminator-stamping template, and removes unused helpers.

  • The refactor largely preserves the examined generation paths.
  • Removing an exported codegen function breaks its downstream callers; the new template name also risks colliding with user overrides.

Reviews (1) · Last reviewed commit: "docs: bring the allOf and union comments..."

Comment thread pkg/codegen/merge_schemas.go Resolved
@mromaszewicz
mromaszewicz merged commit c51209b into oapi-codegen:main Sep 26, 2026
15 checks passed
@mromaszewicz
mromaszewicz deleted the refactor/v3-dedupe branch September 26, 2026 05:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant