How to specify a multipart form field is optional in OpenAPI/Swagger v3?

2k Views Asked by At

I'd like to outline a multipart form request body, with some fields which are required, and other fields which are optional. Generally, in OAS Schema Objects, all properties not explicitly marked with required: true are defaulted to optional. However, when outlining a requestBody with multipart/form-data content, this seems to go by the wayside, and all fields are required.

I have tried multiple ways of designating the fields required vs optional. I receive compilation errors when trying to explicitly designate a field as optional with required: false.

OAS3 spec:

requestBody:
  required: true
  content:
    multipart/form-data:
      schema:
        type: object
        required:
          - foo
        properties:
          foo:
            type: string
            format: binary
          bar:
            type: string
            format: binary

Expected: Detail a multipart/form-data requestBody with some fields required and some optional.

In the code example above, foo should be a required file, while bar should be an optional file.

1

There are 1 best solutions below

0
On

You can add an array of required fields, for example, if you'd like do make just foo required, should be like that:

requestBody:
  content:
    multipart/form-data:
      schema:
        type: object
        required: [foo]
        properties:
          foo:
            type: string
            format: binary
          bar:
            type: string
            format: binary

If you'd like to add more than one, just add the name on the array, such as [foo,bar].