Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to describe a multipart response using OpenAPI (Swagger)?

I have a service that creates a multipart file containing:

  • a data byte array that represents an image buffer
  • a JSON that represents information about the image (coord, format, etc.)

Is it possible to model this custom response in an OpenAPI (Swagger) definition, using YAML?

like image 831
VisualPi Avatar asked Dec 01 '17 16:12

VisualPi


People also ask

How do you add a multipart form data in swagger?

You start with the requestBody/content keyword. Then, you specify the media type of request data. File uploads typically use the multipart/form-data media type, and mixed-data requests usually use multipart/mixed . Below the media type, put the schema keyword to indicate that you start describing the request payload.

What is multipart response?

a multipart request is a REST request containing several packed REST requests inside its entity. a multipart response is a REST response containing several packed REST responses inside its entity.


1 Answers

Multipart responses can be described using OpenAPI 3.0, but not OpenAPI 2.0 (fka Swagger 2.0).

openapi: 3.0.0
...
paths:
  /something:
    get:
      responses:
        '200':
          description: OK
          content:
            multipart/mixed: # <-- Content-Type of the response
              schema:
                type: object
                properties:
                  # Part 1 - application/octet-stream
                  file:  # <-- part name
                    type: string
                    format: binary
                  # Part 2 - application/json
                  metadata:  # <-- part name
                    type: object
                    properties:
                      foo:
                        type: string
                        example: bar
                    required:
                      - foo

              # Optional, see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#encoding-object
              encoding:
                file:
                  ...
                metadata:
                  ... 

The optional encoding key can be used to override the Content-Type for subparts or add headers for subparts (e.g. Content-Disposition).

like image 175
Helen Avatar answered Oct 29 '22 04:10

Helen