Repository navigation
fix: qualify only the model names in types from an imported document - #2589
Merged
mromaszewicz merged 1 commit intoSep 27, 2026
Merged
mromaszewicz merged 1 commit into
mromaszewicz merged 1 commit into
Conversation
A type generated in the context of an import-mapped document was
qualified by prefixing its whole Go spelling with the package, which is
only right when it is a single model name. Every other spelling broke:
Body externalRef0.any // undefined: externalRef0.any
Body externalRef0.*string // gofmt: expected 'IDENT', found '*'
Q *externalRef0.string // undefined
type OpJSONRequestBody = externalRef0.[]Thing
Body externalRef0.time.Time
This hit a reusable response reached from a non-fixed status code (or
reused by a second status code), and every inline schema of an
operation in a path item reused by $ref: its parameters, request body
and responses. A struct literal was skipped instead, so the models it
named stayed unqualified. The response headers of such a path item were
not qualified at all, nor were the typed responses of its client.
Qualify only the unqualified, non-builtin type names in the expression,
using go/ast to find them and leaving everything else as written:
"[]Thing" becomes "[]externalRef0.Thing", "*string" and "time.Time"
stay as they are. A plain model name, and a struct literal the schema
declares as a named type, are qualified exactly as before. Qualify the
response headers of a reused path item, except a header that is itself
a $ref into another document, and the client's typed responses for it.
One case that compiled before now generates differently. A struct
literal or response header of a reused path item that names a model
used to compile only when the importing package happened to declare a
type of the same name, and then it referred to that local type rather
than the imported model the spec names, so it decoded into the wrong
shape. It now refers to the imported model.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
|
mromaszewicz
added a commit
to mromaszewicz/oapi-codegen
that referenced
this pull request
Sep 27, 2026
Brings in oapi-codegen#2587 (a reusable form response's envelope is marshalled, not its wrapper) and oapi-codegen#2589 (types from an imported document qualify only their model names). Co-Authored-By: Claude Opus 5.5 <[email protected]>
mromaszewicz
added a commit
to mromaszewicz/oapi-codegen
that referenced
this pull request
Sep 27, 2026
The fixture oapi-codegen#2589 added has a reusable response whose schema is `x-go-type: "*string"`, reached only from `default`. Its envelope now carries a Body field, as a pointer envelope does, so that a fixed-status $ref to it would compile. Co-Authored-By: Claude Opus 5.5 <[email protected]>
mromaszewicz
added a commit
that referenced
this pull request
Sep 28, 2026
* fix: wrap receiver-incompatible strict JSON responses * test: cover strict JSON response receiver compatibility * test: cover strict JSON response wire values * test: cover OpenAPI 3.1 strict JSON responses * fix: keep strict response shapes that already compile Wrapping every receiver-incompatible body changed response types that compiled before, breaking code written against them: - A components/responses envelope is never a method receiver. Each operation that reaches it through $ref embeds it in a struct, and an embedded field may be an interface, so an untyped or interface envelope compiled as a direct type. Only a pointer, which cannot be embedded, needs a Body field there. - Under schema-merging-behavior v1, an allOf of a single $ref becomes a struct that embeds the member, which is a valid receiver, whether it is inline or a component of its own. The single-member allOf shortcut assumed the member's type is always used directly. Classify a composition by a referenced member only when the generator actually emitted that member's type. That also covers a nullable union collapsed to an untyped member, and an allOf of an untyped $ref with sibling keywords under v3. Check the declared type of x-go-type-name as well. One envelope that compiled before still changes: a pointer envelope that no operation reaches through a fixed-status $ref, so that it is never embedded (only reached through default, a status range, or not at all), goes from `type FooJSONResponse *T` to a struct with a Body field. The envelope has to match what a fixed-status $ref needs, and no generated operation uses the envelope in that case. servers/strict/responsebody_v1 compiles and serves the v1 shapes. Co-Authored-By: Claude Opus 5.5 <[email protected]> * fix: wrap untyped text and form strict response bodies A text/plain or application/x-www-form-urlencoded response without a schema, or with one that lowers to any, generated a method on an interface type, the same compile error as the JSON case: invalid receiver type GetText200TextResponse (pointer or interface type) The strict templates already write text and form bodies from response.Body whenever the response has a Body field, so classify these content types as well. An operation that reaches a reusable text response through $ref redeclares its envelope as the receiver type, unlike a JSON or form envelope, which it embeds. So a text envelope is classified as a receiver. That changes one type that compiled before: a reusable text envelope whose body lowers to any or a pointer, and that no operation reaches through a fixed-status $ref (only through default, a status range, or not at all), goes from `type FooTextResponse any` to a struct with a Body field. A fixed-status $ref to it needs that shape to compile, and no generated operation uses the envelope otherwise. Co-Authored-By: Claude Opus 5.5 <[email protected]> * refactor: drop NeedsLocalResponseHeaders NeedsLocalResponseHeaders only restated `not .IsRef`, and the strict templates used it for two of their three header branches; the branch for a response without content still reads $isRef. It changes no output, so go back to $isRef throughout and keep the templates' diff to what the fix needs. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test: describe what the response_cast fixture now covers The spec headers still described only the #2010 cast. Name the untyped, pointer and header-bearing responses the fixture now covers, and cite #2525 alongside #1328. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test: pin the v1 wire bodies, and spell the nullable fixtures with anyOf Review follow-ups: - responsebody_v1 only checked that the direct responses were valid JSON, so a visitor that dropped the value would still pass. Assert the body. v1 embeds the untyped member, and encoding/json encodes an embedded interface as a field named after its type, so the value has always gone out as {"Untyped": ...}. The test records that. - Untyped ({}) already admits null, so oneOf of it and a null branch rejects null, the value the fixtures send. Use anyOf, which the generator lowers identically, in both fixtures. The unit test keeps the oneOf spelling that specs commonly use, and adds the anyOf one. Co-Authored-By: Claude Opus 5.5 <[email protected]> * test: regenerate external_types for reusable pointer envelopes The fixture #2589 added has a reusable response whose schema is `x-go-type: "*string"`, reached only from `default`. Its envelope now carries a Body field, as a pointer envelope does, so that a fixed-status $ref to it would compile. Co-Authored-By: Claude Opus 5.5 <[email protected]> --------- Co-authored-by: Marcin Romaszewicz <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
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 type that belongs to an import-mapped document was qualified by putting the package in front of its whole Go spelling (
ensureExternalRefsInSchema). That's only right for a single model name.any,*string,[]Thing,map[string]Thing,time.Time, and every primitive becameexternalRef0.any,externalRef0.*string,externalRef0.[]Thingand so on. These either failed gofmt, aborting generation, or didn't compile. Struct literals were skipped instead, so a model named inside one stayed unqualified. Two positions were affected:$ref.In the same position, response headers weren't qualified at all, and neither were the client's typed responses (
JSON200 *Thing).What changed.
qualifyExternalTypeNamesparses the type expression withgo/astand prefixes only the unqualified, non-builtin type names, leaving the rest of the text (field names, tags, comments) as written. So[]Thingbecomes[]externalRef0.Thing, a struct literal'sF *ThingbecomesF *externalRef0.Thing, andany,*string,time.TimeandexternalRef1.Barstay as they are.ensureExternalRefsInSchemakeeps its current result wherever that result compiled: a plain model name is still referred to asexternalRef0.<Name>(even when the schema already carries a RefType), and a struct literal that the schema declares as a named type is left alone. Everything else is qualified with the new helper.ensureExternalRefsInResponseDefinitionsnow qualifies response headers too, except a header that is itself a$refinto another document, which is already qualified with that document's package.One case that compiled before generates differently, on purpose. A struct literal or response header of a reused path item that names a model (
Thing *Thing,XThing *Tag) used to compile only when the importing package happened to declare its own type of the same name. It then referred to that local type instead of the imported model the spec names, silently decoding into the wrong shape. It now refers toexternalRef0.Thing. That output was broken, so it isn't preserved behind acompatibilityoption. Handler code that built those structs from the local type needs to use the imported model.How it's tested.
pkg/codegen/externalref_test.go:ensureExternalRefsInSchema's rules, including the preserved ones and the header guard;main, where generation aborts.internal/test/references/multipackage/external_typesgenerates an imported package and an API that reuses its reusable responses fromdefaultand one of its path items. A client ⇄ strict-server round trip coversany,*string,[]Thing, a path parameter, a query parameter, a request body, a response header typed by a model, and the client's typed responses.mainfails to generate this fixture (expected 'IDENT', found '[').mainagainst one from this branch. The matrix is 143 cross-package cases: the three positions above plus response headers, header$refs into the same and a third document, and path parameters. It covers builtins,date-time/uuid, pointers,x-go-type(local and qualified), arrays and maps of models and builtins, inline objects with and without model fields, enums, unions,additionalProperties,allOf, and$refs to the same and a third document. I ran it as-is, without the client, and withold-aliasing,schema-merging-behavior: v1/v3andnullable-type. In all six runs, no output that compiles onmainchanges (the matrix has no same-name collisions; that case is described above). Between 88 and 93 failing cases per run now compile. The only failures left are strict-server receivers for an inline untyped or pointer response with a fixed status code, which fail in the imported package too and are fixed by fix: handle untyped strict response bodies #2514. With fix: handle untyped strict response bodies #2514 merged in, they compile.make generatechanges no committed file.make testpasses,make lintgives 0 issues, and the new fixture and tests pass under Go 1.25.Fixes #2588
🤖 Generated with Claude Code