Skip to content

Response component with multiple content types generates undefined per-content-type type names #2389

@aravindhp

Description

@aravindhp

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:

  1. Both fields use the single underlying type (*ErrorResponse), as v2.6.x did; or
  2. 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).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions