oapi-codegen
oapi-codegen copied to clipboard
Unable to load JSON file as multipart form data echo framework
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)
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