OAI / OpenAPI-Specification

The OpenAPI Specification Repository
https://openapis.org
Apache License 2.0
28.82k stars 9.07k forks source link

Can we get clarification on how to post multipart/form-data containing an array of items of type X? #2840

Closed spacether closed 3 months ago

spacether commented 2 years ago

Can we get clarification on how to post multipart form data with an array of items of type X? Here is an example requestBody:

      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                files:
                  type: array
                  items: {}
                bools:
                  type: array
                  items:
                    type: boolean
                strings:
                  type: array
                  items:
                    type: string

1 What if x is a boolean?

  1. Are the rfc accepted string serializations for booleans or should we use json?
  2. What if X is multipart/octet-stream binary data, should the array be exploded? If so should the name be files or files[]?
  3. If x is type string, should the array serialized with explode = True or False?
  4. When should explode be true and when should it be false for multipart form data?
  5. What count as primitive types aside from strings? int? float? bool? null?
  6. Should empty arrays be sent with json content-type because explode would not send their data? Any actual examples including the data sent in each multipart section would be a great help.

It looks like this PR is related: https://github.com/OAI/OpenAPI-Specification/pull/2066

spacether commented 2 years ago

Form here in the spec:

To upload multiple files, a multipart media type MUST be used:

requestBody:
  content:
    multipart/form-data:
      schema:
        properties:
          # The property name 'file' will be used for all files.
          file:
            type: array
            items: {}

It looks like explode=True for multipart form arrays of files.

MHamzaRajput commented 1 year ago

explode=True is not working for multipart/form-data request

handrews commented 1 year ago

The style field in the Encoding Object is only valid for application/x-www-form-urlencoded, so it cannot be used with multipart media types.

I'm fairly certain that means that the cases asked about in this issue are all invalid.

spacether commented 1 year ago

style is defined as Describes how a specific property value will be serialized depending on its type. See [Parameter Object](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameterObject) for details on the [style](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#parameterStyle) property. The behavior follows the same values as query parameters, including default values. This property SHALL be ignored if the request body media type is not application/x-www-form-urlencoded or multipart/form-data. If a value is explicitly defined, then the value of [contentType](https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#encodingContentType) (implicit or explicit) SHALL be ignored. So isn't multipart/form-data explicitly covered by style?

Also this issue asks for clarification when style is unset. If the answer is to point to an RFC that is fine, but the RFCs that I looked at lacked these examples.

handrews commented 1 year ago

@spacether I see what's going on here. In OAS 3.0, style and explode apply only to application/x-www-form-urlencode, but in OAS 3.1 they apply to both application/x-www-form-urlencoded and multipart/form-data.

So if @MHamzaRajput is using OAS 3.0, that would explain why explode: true is not working for them.

Regarding your questions (in the context of OAS 3.1):

handrews commented 1 year ago

At minimum, a bit of clarification on the expected string form of boolean and null values is probably worth including in a patch release, so I'm labeling this appropriately. Even if that clarification ends up being "do whatever works for your environment." For example, Perl does not have a boolean type, so it's not always possible to round-trip JSON-like booleans.

handrews commented 3 months ago

PR merged for 3.0.4 and ported to 3.1.1 via PR #3921! This is addressed in the new Appendix B.