2

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.

aware
  • 118
  • 1
  • 9
  • Your schema is correct. If some tool handles `bar` as a required field, it's a bug in that tool. – Helen Jul 18 '21 at 18:16

1 Answers1

1

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