oapi-codegen
oapi-codegen copied to clipboard
Incorrect array response generation with multiple refs
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'