vertx-web icon indicating copy to clipboard operation
vertx-web copied to clipboard

openapi gives 400 http status code, when uploading file with multipart/form-data

Open ebadta81 opened this issue 4 years ago • 6 comments
trafficstars

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

ebadta81 avatar Sep 16 '21 10:09 ebadta81

Is there any workaround, I can apply for this bug?

ebadta81 avatar Sep 23 '21 15:09 ebadta81

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/jpeg but Vert.x Web expects application/octet-stream
  • when the file content type is set to application/octet-stream, FormBodyProcessorImpl builds a JsonObject which does not contain the file property because it is not returned by request.formAttributes(); then the validator rejects the request for missing property

I can't think about any workaround.

tsegismont avatar Oct 12 '21 09:10 tsegismont

@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.

tsegismont avatar Oct 12 '21 10:10 tsegismont

@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.

tsegismont avatar Oct 12 '21 10:10 tsegismont

The PR shall also update the doc to indicate content type can be specified in the spec file

tsegismont avatar Oct 12 '21 13:10 tsegismont

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.

tsegismont avatar Oct 13 '21 15:10 tsegismont