Abu Romaïssae
Abu Romaïssae

Reputation: 3901

File upload on Swagger Editor OpenAPI 3 not showing the file browser when trying it out

I'm using Swagger Editor with OpenAPI 3.0. I need to document a route which consists of uploading an image. When attempting to "Try it out", I don't get the file browser to choose the image to upload in the request body, all I get is a JSON string with the name and type of parameter.

File Upload "Try it out" screenshot

This is the YAML description of the route:

openapi: 3.0.0
...
paths:
  /media/upload/thumbnail:
    post:
      tags:
        - media
        - publications
      security:
        - bearerAuth: []
      summary: upload a kid's publication
      operationId: uploadPublication
      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                upload:
                  type: string
                  format: binary
      responses:
        '200':
          description: Successful operation
          content:
            application/json:
              schema:
                type: object
                properties:
                  id:
                    type: string
                    description: ObjectId of the uploaded file in db (original size)
                    example: 5a6048b3fad06019afe44f24
                  filename:
                    type: string
                    example: myPainting.png
                  thumbnail:
                    type: string
                    description: ObjectId of the uploaded file in db (thumbnai size)
                    example: 5a6048b3fad06019afe44f26
        '400':
          description: Authentication failed

What am I missing here?

Upvotes: 8

Views: 14353

Answers (3)

CodeWorld
CodeWorld

Reputation: 2307

For one of my same kind of concern !!!
I had to modify the input type to requested endpoint method.
Note- It might not address all type of requirements, but few can definitely be benefitted from this

Before:

/myEndpoint( @RequestBody RequestModelName requestModelName )

where RequestModelName has properties and getter/setter...

  1. private MultipartFile file
  2. private String otherProp

By this I could NOT see File option in swagger.

After:

/myEndpoint( @RequestParam MultipartFile file, @RequestParam String otherProp )

And then I could see File option in swagger...

Upvotes: 0

unknown_user
unknown_user

Reputation: 135

Try with

content:
          image/png:
            schema:
              properties:
                file:
                  type: string
                  format: binary

Upvotes: -2

Helen
Helen

Reputation: 97609

Your definition is correct.

Support for file upload for multipart/form-data requests in OpenAPI 3.0 definitions has been added in Swagger Editor 3.5.7 and Swagger UI 3.16.0. Make sure you are using a version that supports file upload.

Upvotes: 4

Related Questions