oapi-codegen
oapi-codegen copied to clipboard
Empty schema in response
When schema in content for response is empty, generated strict server codes are broken.
$
$ cat issue.yaml
openapi: "3.0.1"
paths:
/test:
get:
operationId: test
responses:
200:
description: good
content:
application/json:
schema:
$ go run cmd/oapi-codegen/oapi-codegen.go -generate types,server,strict-server -package issue -o issue.go issue.yaml && go test issue.go
# command-line-arguments
./issue.go:78:7: invalid receiver type Test200JSONResponse (pointer or interface type)
FAIL
$
Is there a workaround?
FWIW I worked around this with
responses:
"201":
content:
application/json:
schema:
x-go-type: std.EmptyResponse
x-go-type-import:
path: example.com/std
Then EmptyResponse looks like:
type EmptyResponse struct{}
func (e EmptyResponse) MarshalJSON() ([]byte, error) {
return []byte{}, nil
}
FWIW I worked around this with
Serves me right for posting before testing; the JSON marshaller returns an error if you return empty bytes; so this isn't actually a workaround :(
This seems to generate the right code:
paths:
/test:
get:
operationId: test
responses:
200: {}