oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Empty schema in response

Open ShouheiNishi opened this issue 2 years ago • 4 comments

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
$

ShouheiNishi avatar Oct 18 '23 01:10 ShouheiNishi

Is there a workaround?

renom avatar Jan 28 '25 20:01 renom

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
}

jpeach avatar Jun 03 '25 06:06 jpeach

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 :(

jpeach avatar Jun 05 '25 01:06 jpeach

This seems to generate the right code:

paths:
  /test:
    get:
      operationId: test
      responses:
        200: {}

jpeach avatar Jun 05 '25 01:06 jpeach