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

Unable to load JSON file as multipart form data echo framework

Open rat6666 opened this issue 1 year ago • 1 comments

Hi, Definition (Swagger 3.0.3)

      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                file:
                  type: string
                  format: binary

Tested on oapi-codegen v1.9.0, 2.0.0, 2.1.0 and echo

When using the Swagger UI to run a query, the following error is returned:

{
    "message": "request body has an error: doesn't match schema: Error at \"/file\": value must be a string"
}

This is from validation middleware.

func OapiRequestValidatorWithOptions(swagger *openapi3.T, options *Options) echo.MiddlewareFunc

This happens because Swagger detects the file extension and sets the Content-Type in the multipart data form to application/json.

curl -X 'POST' \
  'url here' \
  -H 'accept: application/json' \
  -H 'Content-Type: multipart/form-data' \
  -F '[email protected];type=application/json'

With type text/plain it works (but it's impossible to change in swagger UI)

rat6666 avatar Feb 26 '24 07:02 rat6666

Same issue here. Usually for parsing bodies as files I am using decoders like:

openapi3filter.RegisterBodyDecoder("text/xml", openapi3filter.FileBodyDecoder)

Of course if this would be done in combination with application/json, then normal JSON-ish requests would not work anymore. Any chance to get this working?

I want to use the definition like this:

requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              required:
                - "file"
              properties:
                file:
                  type: string
                  format: binary
            encoding:
              file:
                contentType: application/json

lukasbash avatar Jun 27 '24 18:06 lukasbash