Skip to content

refactor: share the Go-type and lowered-type walks the aggregate code grew - #2592

Merged
mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
mromaszewicz:refactor/aggregate-shared-helpers
Sep 29, 2026
Merged

mromaszewicz merged 4 commits into
oapi-codegen:mainfrom
mromaszewicz:refactor/aggregate-shared-helpers

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

What was wrong. A review of the allOf/anyOf/oneOf code after #2514, #2589 and #2591 found the same two walks written several times over:

  • Four functions in four files parsed a Go type expression with go/parser and read it their own way: goTypeNeedsBodyWrapper (operations.go) and v1GoTypeKind (merge_schemas_v1.go) look at the outermost node, and mentionsTypeName (schema.go) and qualifyExternalTypeNames (externalref.go) walk the identifiers in type position, one with ast.Inspect and one with a hand-written switch over node kinds. Only one of them saw through parentheses.
  • The strict-server response classification and v1's allOf member classification each walked, in their own words, from a schema to the one its Go type came from: the $ref member a composition was lowered to, and the type x-go-type-name declares beside the schema.
  • GenStructFromAllOf was exported with no caller in the repository, and fix: lower a v1 allOf of a non-object $ref to the member's type #2591 gave it a copy of mergeSchemasV1's classification loop.
  • The idiom for "a schema's allOf, anyOf and oneOf" was spelled out in four places, and two v2 comments were stale or narrated hypotheticals.

This is a refactor with no issue behind it; generated output is unchanged.

What changed.

  • gotype.go holds parseGoType, unparen, typeIdents and isPredeclaredType; the four readers use them.
  • lowered.go holds loweredToMember and Schema.namedTypeDef, and subschemaLists. The two classifications keep only what differs between them: how they generate a schema (a fresh context for a response body; an isolated context with a memo for v1, so the 60-link chain stays linear), and what they ask of the type they reach. v1 now matches a composition's $ref member by the type it generates as, like the response classification, instead of by name; they differ only for a member whose $ref carries an x-go-type, which v1 keeps embedded either way.
  • GenStructFromAllOf is gone.
  • Comments: the nullability-union rationale in mergeOpenapiSchemas is stated once; the defensive-clone comment says what the clone is for; generateAllOfV2's note that "v1's merge has no such path" reflects fix: lower a v1 allOf of a non-object $ref to the member's type #2591.

Two inputs no spec has are read differently, both noted in the commit: v1GoTypeKind sees through parentheses around a type, as goTypeNeedsBodyWrapper already did; and typeIdents reaches the types in an interface's type set (~int | Foo), which the hand-written walk did not, and leaves an array's length alone, which ast.Inspect did not.

How it's tested.

  • Byte-identical output is the bar. make generate changes no committed file. A binary built from main and one from this branch were run over every //go:generate target under internal/test and examples, forcing schema-merging-behavior: v1 where the config doesn't pin a version: 382 targets, 220 forced to v1, all 382 identical, no target changing between generating and failing.
  • gotype_test.go pins typeIdents (source order; field and method names, pkg.Name and array lengths skipped; early stop) and parseGoType/unparen. The resolver is covered by the existing generation tests of both callers: TestStrictServer*Compatibility, TestMergeSchemasV1ValueMember* (including the 60-link chain and the isolation test), and the aggregate matrix.
  • make test and make lint pass; pkg/codegen passes under Go 1.25. The 200-link v1 chain still generates in under half a second.

🤖 Generated with Claude Code

mromaszewicz and others added 4 commits September 28, 2026 20:56
Four functions parsed a Go type expression with go/parser, each in its
own file and its own way: goTypeNeedsBodyWrapper and v1GoTypeKind look
at the outermost node, and mentionsTypeName and
qualifyExternalTypeNames walk the identifiers in type position, one
with ast.Inspect and one with a hand-written switch over node kinds.

gotype.go now holds parseGoType, unparen, typeIdents and
isPredeclaredType, and the four use them. Generated output is the same
for every committed spec. Two inputs no spec has are read differently:
v1GoTypeKind sees through parentheses around a type, as
goTypeNeedsBodyWrapper already did; and typeIdents reaches the types in
an interface's type set (~int | Foo), which the hand-written walk did
not, and leaves an array's length alone, which ast.Inspect did not.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The strict-server response classification (operations.go) and v1's allOf
member classification (merge_schemas_v1.go) each walked, in their own
words, from a schema to the one its Go type came from: the $ref member a
composition was lowered to, and the type x-go-type-name declares beside
the schema. lowered.go now holds that walk, as loweredToMember and
Schema.namedTypeDef, and the two classifications keep only what differs
between them: how they generate a schema (a fresh context, and an
isolated one with a memo), and what they ask of the type they reach.

v1 used to match a composition's $ref member by the member's name, and
now matches it by the type the member generates as, as the response
classification does. The two differ only for a member whose $ref carries
an x-go-type, which v1 keeps embedded either way.

subschemaLists spells out a schema's allOf, anyOf and oneOf for the four
places that walked all three.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
The exported wrapper had no caller in the repository, and oapi-codegen#2591 gave
it a copy of mergeSchemasV1's classification of the members. Only
genStructFromAllOf remains, which mergeSchemasV1 hands the context and
the classification it already has.

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

The rationale for unioning "null" across allOf members was written out
twice in mergeOpenapiSchemas, at the type comparison and at the
nullability merge; the type comparison now refers to the latter. The
comment on the defensive clone of the merged extensions narrated
hypothetical future changes; it now says what the clone is for. The
comment on generateAllOfV2's pure-wrapper case said v1 never returns the
referenced type unchanged; since oapi-codegen#2591 it does so for a member that
isn't an object.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner September 29, 2026 04:10
@greptile-apps

greptile-apps Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerConfidence Score: 4/5

[Medium risk] Refactors code generation internals to share type-parsing logic.

The PR should preserve the exported allOf function before merging, because its removal breaks downstream callers on upgrade.

Findings

  1. P1 Exported function removed ▶
Fix with agent prompt
### Issue 1
pkg/codegen/merge_schemas_v1.go:99-102
If downstream programs call `codegen.GenStructFromAllOf`, upgrading to this version will make them fail to compile because the exported function has been removed. The remaining `genStructFromAllOf` is unexported and has a different signature, so it cannot replace those calls. Please retain a compatible wrapper.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

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

Summary

The PR consolidates Go-type parsing and schema-to-member classification, removes a duplicated allOf wrapper, and updates related comments.

  • The shared walks largely preserve the examined caller behavior, but removing an exported function breaks callers of that API.
  • Focused tests are missing for the walker's two intentional behavior changes.

Reviews (1) · Last reviewed commit: "docs: say once why v2 unions nullability..."

Comment thread pkg/codegen/merge_schemas_v1.go Resolved
@mromaszewicz
mromaszewicz merged commit 83e2818 into oapi-codegen:main Sep 29, 2026
16 checks passed
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