Skip to content

Invalid generated code on reusing responses #2190

Closed

Description

@bravenut

Hi,

we just upgraded from oapi-codegen 2.3.0 to 2.5.1 and our go code generated from OpenAPI isn't valid anymore. I pinned down the issue to response components being referenced in endpoints and came up with this minimal reproducible spec:

openapi: 3.0.3
info:
  title: test
  version: 1.0.0
servers:
  - url: https://te.st
paths:
  /v1/test:
    get:
      operationId: GetTest
      responses:
        "200":
          $ref: "#/components/responses/Success"
        "401":
          $ref: "#/components/responses/Unauthorized"
components:
  responses:
    Success:
      description: Success
      content:
        application/json:
          schema:
            type: string
    Unauthorized:
      description: Unauthorized
      content:
        text/plain:
          schema:
            type: string
          example: "Unauthorized"

We execute oapi-codegen with this configuration:

package: test
output: api.gen.go
generate:
  std-http-server: true
  strict-server: true
  models: true
  client: true
output-options:
  nullable-type: true

The generated go code contains this:

type UnauthorizedTextResponse string

type GetTest401TextResponse struct{ UnauthorizedTextResponse }

func (response GetTest401TextResponse) VisitGetTestResponse(w http.ResponseWriter) error {
	w.Header().Set("Content-Type", "text/plain")
	w.WriteHeader(401)

	_, err := w.Write([]byte(response))
	return err
}

which is invalid as response is of type GetTest401TextResponse and cannot be converted to []byte.

We are using Go version 1.25.7.

I would appreciate if you could have a look, would also be happy to contribute.
Cheers

Activity

  1. added this to the v2.6.0 milestone on Feb 15, 2026
  2. added theissue type on Feb 15, 2026
  3. mromaszewicz commented on Feb 16, 2026

    @mromaszewicz
    Member

    Yep, confirmed. Totally a regression. Looking into it.

  4. self-assigned this
    on Feb 16, 2026
  5. mromaszewicz commented on Feb 16, 2026

    @mromaszewicz
    Member

    The cause of this issue is actually this revert:
    #1132

  6. jamietanna commented on Feb 16, 2026

    @jamietanna
    Member

    Did we not revert that in #1773 ?

    Edit: I see #2225 - thanks!

  7. bravenut commented on Feb 16, 2026

    @bravenut
    Author

    Thanks for fixing! Looking forward to the next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions