Skip to content

Empty schema in response #1328

Closed
Closed

Description

@ShouheiNishi

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
$

Activity

  1. added
    bug
    on Nov 19, 2023
  2. renom commented on Jan 28, 2025

    @renom
    Contributor

    Is there a workaround?

  3. jpeach commented on Jun 3, 2025

    @jpeach

    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
    }
    
  4. jpeach commented on Jun 5, 2025

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

  5. jpeach commented on Jun 5, 2025

    @jpeach

    This seems to generate the right code:

    paths:
      /test:
        get:
          operationId: test
          responses:
            200: {}
    
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions