Repository navigation
Description
Found while reviewing #2514; pre-existing on main and independent of that PR.
Problem
When generated code refers to a type that belongs to an import-mapped document, the generator qualifies it by putting the package name in front of the whole Go type (ensureExternalRefsInSchema in pkg/codegen/externalref.go). That is only right when the type is a single model name (Thing → externalRef0.Thing). Every other spelling breaks:
Body externalRef0.any // undefined
Limit *externalRef0.int // undefined
Body externalRef0.*string // gofmt: expected 'IDENT', found '*'
type OpJSONRequestBody = externalRef0.[]Thing // gofmt: expected 'IDENT', found '['
Body externalRef0.time.Time // gofmt: expected ';', found '.'Struct literals are skipped instead of qualified, so a model named inside one stays a bare local name: undefined: Thing, or worse, if the importing package declares its own Thing, it silently refers to that type and decodes into the wrong shape. Response headers of a reused path item have the same problem.
Two positions are affected:
- A reusable response reached from a non-fixed status code (
default,2XX), or reused by a second status code, when it comes from another document. The operation declares its ownBody <type>field, and that type is mis-qualified. - Every inline schema of an operation in a path item reused by
$ref(/things: $ref: ./common.yaml#/paths/~1things): its parameters, its request body, and its responses. In the same position, response headers aren't qualified at all, and neither are the client's typed responses (JSON200 *Thing→undefined: Thing).
In practice, any primitive, array, map, pointer, format: date-time/uuid or x-go-type schema in those positions fails. Only a $ref to a model, or a plain inline object without model fields, works today.
Reproduction
common.yaml:
openapi: "3.0.3"
info: {title: common, version: "1"}
paths:
/things:
get:
operationId: listThings
parameters:
- {name: limit, in: query, schema: {type: integer}}
responses:
default:
description: error
content:
application/json:
schema: {}
components:
responses:
Untyped:
description: untyped
content:
application/json:
schema: {}api.yaml:
openapi: "3.0.3"
info: {title: api, version: "1"}
paths:
/things:
$ref: "./common.yaml#/paths/~1things"
/other:
get:
operationId: getOther
responses:
default:
$ref: "./common.yaml#/components/responses/Untyped"Configs: common.yaml with generate: {models: true, std-http-server: true, strict-server: true}; api.yaml with the same plus import-mapping: {./common.yaml: <common package>}. The api package does not compile:
api.gen.go:21:9: undefined: externalRef0 // Limit *externalRef0.int
api.gen.go:224:13: undefined: externalRef0 // Body externalRef0.any (getOther)
api.gen.go:249:13: undefined: externalRef0 // Body externalRef0.any (listThings default)
(The import is missing because nothing else refers to the package.) Adding a reusable response whose schema is x-go-type: "*string", or an array such as type: array, items: {$ref: '#/components/schemas/Thing'}, referenced from a default status, makes generation itself fail:
error generating code: error formatting Go code at line 360: api.go:360:39: expected 'IDENT', found '*' (and 2 more errors)
Notes
- Since none of this output compiles, the fix is free to choose the right shape: qualify only the model names inside the type (
[]externalRef0.Thing), and leave builtins (any,*string) and already-qualified names (time.Time,openapi_types.UUID) alone. - A single model name, and a struct literal that the schema declares as a named type, already work and must keep their current spelling. For example, under
old-aliasingthe imported package declares a request-body type as a defined type rather than an alias, so switching which name is referenced would change type identity. - fix: handle untyped strict response bodies #2514 fixes a separate problem that also shows up here: under strict-server, an inline
schema: {}response with a fixed status code doesn't compile in either package. With both fixes, reused path items with untyped responses compile.
Activity