8000 Invalid generated code on reusing responses · Issue #2190 · oapi-codegen/oapi-codegen · GitHub
[go: up one dir, main page]

Skip to content

Invalid generated code on reusing responses #2190

@bravenut

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.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0