vertx-web
vertx-web copied to clipboard
openapi gives 400 http status code, when uploading file with multipart/form-data
Version
Which version(s) did you encounter this bug ? 4.1.3
Context
Vertx openapi gives 400 http status code, when uploading file with multipart/form-data
Do you have a reproducer?
https://github.com/ebadta81/test
Steps to reproduce
After starting the test project, load the http://localhost:8443/web/index.html#/upload/upload-image url in your browser, and send a request with swagger ui.
You will get 400 | Error: Bad Request
Extra
MacOs 11.5.2 openjdk 11.0.10 2021-01-19
Is there any workaround, I can apply for this bug?
There are two issues:
- the content type of the file sent by Swagger UI: if you choose an image, the content type is, for example,
image/jpegbut Vert.x Web expectsapplication/octet-stream - when the file content type is set to
application/octet-stream,FormBodyProcessorImplbuilds aJsonObjectwhich does not contain thefileproperty because it is not returned byrequest.formAttributes(); then the validator rejects the request for missing property
I can't think about any workaround.
@ebadta81 the first issue can be solved by updating your schema:
content:
"multipart/form-data":
schema:
$ref: "#/components/schemas/image-upload-query"
encoding:
file:
contentType: 'image/*'
With this, any file having a content type starting with image/ will be accepted.
@ebadta81 the second issue can be worked around by avoiding a schema reference:
paths:
/upload/image:
post:
summary: image upload
operationId: upload-image
tags:
- upload
requestBody:
required: true
content:
"multipart/form-data":
schema:
description: user image upload
type: object
required:
- ugyletid
- file
properties:
ugyletid:
type: integer
token:
type: string
file:
type: string
format: binary
encoding:
file:
contentType: 'image/*'
responses:
200:
description: Expected response to a valid request
400:
description: format error
I will provide a PR so that validation works when the object definition is provided via a schema reference.
The PR shall also update the doc to indicate content type can be specified in the spec file
Tracked down the difference to be in this method:
https://github.com/vert-x3/vertx-web/blob/135879f8400d0d69a82100c0e63b5def9a03bfed/vertx-web-openapi/src/main/java/io/vertx/ext/web/openapi/impl/MultipartFormBodyProcessorGenerator.java#L73-L85
When the multipart form schema is a reference, this method is not useful because the schema has been already loaded and the validators created.