Skip to content

Incorrect array response generation with multiple refs #1509

Closed

Description

@robertschonfeld

Version

v2.1.0

Issue

When referencing a response component multiple times, the generated code incorrectly uses struct embedding. In this example the generated server.go contains

type MyObjectResponseJSONResponse []MyObject
type MyAnotherQuery200JSONResponse struct{ MyObjectResponseJSONResponse }
type MyQuery200JSONResponse struct{ MyObjectResponseJSONResponse }

resulting in the response being nested inside the field MyObjectResponseJSONResponse like

{
  "MyObjectResponseJSONResponse": [
    {"myField": "value"}
  ]
}

The code I would expect is something like

type MyObjectResponseJSONResponse []MyObject
type MyAnotherQuery200JSONResponse MyObjectResponseJSONResponse
type MyQuery200JSONResponse MyObjectResponseJSONResponse

A workaround is not not use the response component and instead define the response under each path. Thanks in advance for response and this awesome library 🙏 .

To reproduce

go run github.com/deepmap/oapi-codegen/v2/cmd/[email protected] --config=server.cfg.yaml openapi.yaml > server.go

server.cfg.yaml

package: gen
generate:
  echo-server: true
  strict-server: true
  embedded-spec: true

openapi.yaml

openapi: 3.1.0
info:
  version: 1.0.0
  title: bug?
paths:
  /myobject/query:
    get:
      operationId: MyQuery
      parameters: []
      responses:
        "200":
          $ref: '#/components/responses/MyObjectResponse'
      x-visible: true
  /my-object/another-query:
    get:
      operationId: MyAnotherQuery
      parameters: []
      responses:
        "200":
          $ref: '#/components/responses/MyObjectResponse'
      x-visible: true
components:
  schemas:
    MyObject:
      type: object
      properties:
        myField:
          type: string
  responses:
    MyObjectResponse:
      description: Successful response
      content:
        application/json:
          schema:
            type: array
            items:
              $ref: '#/components/schemas/MyObject'

Activity

  1. added
    area:external-refs
    on Mar 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:external-refs

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions