Repository navigation
refactor: share the Go-type and lowered-type walks the aggregate code grew - #2592
Merged
mromaszewicz merged 4 commits intoSep 29, 2026
Merged
mromaszewicz merged 4 commits into
mromaszewicz merged 4 commits into
Conversation
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]>
Contributor
|
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.
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:
go/parserand read it their own way:goTypeNeedsBodyWrapper(operations.go) andv1GoTypeKind(merge_schemas_v1.go) look at the outermost node, andmentionsTypeName(schema.go) andqualifyExternalTypeNames(externalref.go) walk the identifiers in type position, one withast.Inspectand one with a hand-written switch over node kinds. Only one of them saw through parentheses.$refmember a composition was lowered to, and the typex-go-type-namedeclares beside the schema.GenStructFromAllOfwas 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 ofmergeSchemasV1's classification loop.This is a refactor with no issue behind it; generated output is unchanged.
What changed.
gotype.goholdsparseGoType,unparen,typeIdentsandisPredeclaredType; the four readers use them.lowered.goholdsloweredToMemberandSchema.namedTypeDef, andsubschemaLists. 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$refmember by the type it generates as, like the response classification, instead of by name; they differ only for a member whose$refcarries anx-go-type, which v1 keeps embedded either way.GenStructFromAllOfis gone.mergeOpenapiSchemasis 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:
v1GoTypeKindsees through parentheses around a type, asgoTypeNeedsBodyWrapperalready did; andtypeIdentsreaches the types in an interface's type set (~int | Foo), which the hand-written walk did not, and leaves an array's length alone, whichast.Inspectdid not.How it's tested.
make generatechanges no committed file. A binary built frommainand one from this branch were run over every//go:generatetarget underinternal/testandexamples, forcingschema-merging-behavior: v1where 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.gopinstypeIdents(source order; field and method names,pkg.Nameand array lengths skipped; early stop) andparseGoType/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 testandmake lintpass;pkg/codegenpasses under Go 1.25. The 200-link v1 chain still generates in under half a second.🤖 Generated with Claude Code