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

Resolve strict-server mode failure when handling multiple binary request bodies (e.g., jpeg and png)

Open neilli-sable opened this issue 2 years ago • 2 comments

I am using the following OpenAPI definition:

openapi: 3.0.3
info:
  title: my api
  version: 1.0.0
paths:
  /image:
    put:
      summary: put image
      operationId: PutImage
      requestBody:
        content:
          image/jpeg:
            schema:
              type: string
              format: binary
          image/png:
            schema:
              type: string
              format: binary
      responses:
        200:
          description: succeed
          content:
            application/json:
              schema:
                type: object
                properties:
                  result:
                    type: string
                    description: result

This OpenAPI definition seems to be syntactically correct, and there should be no issues with code generation using the default settings.

However, when I enable strict-server mode, the code generated is as follows:

type PutImageRequestObject struct {
	Body io.Reader
	Body io.Reader
}

Since the same field name "Body" is used twice, this is invalid syntax in Go.

To address this, I have created a fix. In the contentType switch, when determining Body as Binary, I store only the first occurrence in the bodyDefinitions to resolve this issue.

neilli-sable avatar Jul 30 '23 14:07 neilli-sable