When a components/responses entry has more than one content type (e.g. application/json + application/xml) where both reference the same $ref schema, v2.7.0 generates response struct fields whose types (*ErrorResponseApplicationJSON, *ErrorResponseApplicationXML) are never defined, causing a compile error.
To Reproduce
Minimal spec:
components:
schemas:
errorResponse:
type: object
properties:
code:
type: integer
format: int32
message:
type: string
responses:
error:
description: Error response
x-go-name: ErrorResponse
content:
application/json:
schema:
$ref: '#/components/schemas/errorResponse'
application/xml:
schema:
$ref: '#/components/schemas/errorResponse'
With this response $ref'd from an operation (e.g. $ref: '#/components/responses/error'), running oapi-codegen v2.7.0 generates something like:
// In the response struct:
JSON500 *ErrorResponseApplicationJSON
XML500 *ErrorResponseApplicationXML
But neither ErrorResponseApplicationJSON nor ErrorResponseApplicationXML is ever defined. The generated file does not compile:
undefined: ErrorResponseApplicationJSON
undefined: ErrorResponseApplicationXML
Expected behavior
Either:
- Both fields use the single underlying type (
*ErrorResponse), as v2.6.x did; or
- The generator also emits type aliases:
type ErrorResponseApplicationJSON = ErrorResponse / type ErrorResponseApplicationXML = ErrorResponse
Root cause
gatherComponentResponses in pkg/codegen/gather.go skips non-JSON content types:
for _, mediaType := range SortedMapKeys(response.Content) {
if !util.IsMediaTypeJson(mediaType) {
continue // XML (and any other non-JSON type) is never gathered
}
// ...appends to result with GoNameOverride from x-go-name
}
Only the JSON content type schema is gathered and assigned a resolved Go name. However, the response struct templates iterate over all content types and apply tryContentTypeSuffix to disambiguate — producing ErrorResponseApplicationJSON and ErrorResponseApplicationXML as field types. Since only the JSON schema was gathered, only ErrorResponse (the base name) is ever defined. The *ApplicationJSON and *ApplicationXML variants are referenced but have no corresponding type declaration.
oapi-codegen version: v2.7.0
Additional context
This is a regression from v2.6.x, which generated JSON500 *ErrorResponse / XML500 *ErrorResponse (the same base type for all content types).
When a
components/responsesentry has more than one content type (e.g.application/json+application/xml) where both reference the same$refschema, v2.7.0 generates response struct fields whose types (*ErrorResponseApplicationJSON,*ErrorResponseApplicationXML) are never defined, causing a compile error.To Reproduce
Minimal spec:
With this response
$ref'd from an operation (e.g.$ref: '#/components/responses/error'), runningoapi-codegenv2.7.0 generates something like:But neither
ErrorResponseApplicationJSONnorErrorResponseApplicationXMLis ever defined. The generated file does not compile:Expected behavior
Either:
*ErrorResponse), as v2.6.x did; ortype ErrorResponseApplicationJSON = ErrorResponse/type ErrorResponseApplicationXML = ErrorResponseRoot cause
gatherComponentResponsesinpkg/codegen/gather.goskips non-JSON content types:Only the JSON content type schema is gathered and assigned a resolved Go name. However, the response struct templates iterate over all content types and apply
tryContentTypeSuffixto disambiguate — producingErrorResponseApplicationJSONandErrorResponseApplicationXMLas field types. Since only the JSON schema was gathered, onlyErrorResponse(the base name) is ever defined. The*ApplicationJSONand*ApplicationXMLvariants are referenced but have no correspondingtypedeclaration.oapi-codegen version: v2.7.0
Additional context
This is a regression from v2.6.x, which generated
JSON500 *ErrorResponse/XML500 *ErrorResponse(the same base type for all content types).