Skip to content

Commit f913074

Browse files
committed
Reorganize multipart/form-data guidance (3.1.1 port of 3923 6/6, 3929)
This organizes and streamlines the guidance on multipart that was incorporated from the Media Type Object. Lots of duplication has been removed, and the examples reworked to show distinct use cases.
1 parent f6c2164 commit f913074

File tree

1 file changed

+34
-47
lines changed

1 file changed

+34
-47
lines changed

versions/3.1.1.md

Lines changed: 34 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1805,19 +1805,7 @@ However, this is not guaranteed, so it may be more interoperable to keep the pad
18051805

18061806
##### Encoding `multipart` Media Types
18071807

1808-
It is common to use `multipart/form-data` as a `Content-Type` when transferring request bodies to operations. In contrast to 2.0, a `schema` is REQUIRED to define the input parameters to the operation when using `multipart` content. This supports complex structures as well as supporting mechanisms for multiple file uploads.
1809-
1810-
In a `multipart/form-data` request body, each schema property, or each element of a schema array property, takes a section in the payload with an internal header as defined by [RFC7578](https://tools.ietf.org/html/rfc7578). The serialization strategy for each property of a `multipart/form-data` request body can be specified in an associated [`Encoding Object`](#encodingObject).
1811-
1812-
When passing in `multipart` types, boundaries MAY be used to separate sections of the content being transferred – thus, the following default `Content-Type`s are defined for `multipart`:
1813-
1814-
* If the property is a primitive, or an array of primitive values, the default Content-Type is `text/plain`
1815-
* If the property is complex, or an array of complex values, the default Content-Type is `application/json`
1816-
* If the property is a `type: string` with a `contentEncoding`, the default Content-Type is `application/octet-stream`
1817-
1818-
Per the JSON Schema specification, `contentMediaType` without `contentEncoding` present is treated as if `contentEncoding: identity` were present. While useful for embedding text documents such as `text/html` into JSON strings, it is not useful for a `multipart/form-data` part, as it just causes the document to be treated as `text/plain` instead of its actual media type. Use the Encoding Object without `contentMediaType` if no `contentEncoding` is required.
1819-
1820-
Note that only `multipart/*` media types with named parts can be described as shown here. Note also that while `multipart/form-data` originally defined a per-part `Content-Transfer-Encoding` header that could indicate base64 encoding (`contentEncoding: base64`), it has been deprecated for use with HTTP as of [RFC7578](https://www.rfc-editor.org/rfc/rfc7578#section-4.7).
1808+
It is common to use `multipart/form-data` as a `Content-Type` when transferring forms as request bodies. In contrast to 2.0, a `schema` is REQUIRED to define the input parameters to the operation when using `multipart` content. This supports complex structures as well as supporting mechanisms for multiple file uploads.
18211809

18221810
The `form-data` disposition and its `name` parameter are mandatory for `multipart/form-data` ([RFC7578 §4.2](https://www.rfc-editor.org/rfc/rfc7578.html#section-4.2)).
18231811
Array properties are handled by applying the same `name` to multiple parts, as is recommended by [RFC7578 §4.3](https://www.rfc-editor.org/rfc/rfc7578.html#section-4.3) for supplying multiple values per form field.
@@ -1829,14 +1817,18 @@ It is not currently possible to correlate schema properties with unnamed, ordere
18291817
Note that there are significant restrictions on what headers can be used with `multipart` media types in general ([RFC2046 §5.1](https://www.rfc-editor.org/rfc/rfc2046.html#section-5.1)) and `multi-part/form-data` in particular ([RFC7578 §4.8](https://www.rfc-editor.org/rfc/rfc7578.html#section-4.8)).
18301818

18311819
Note also that `Content-Transfer-Encoding` is deprecated for `multipart/form-data` ([RFC7578 §4.7](https://www.rfc-editor.org/rfc/rfc7578.html#section-4.7)) where binary data is supported, as it is in HTTP.
1832-
Using `contentEncoding` is equivalent to setting `Content-Transfer-Encoding` to the same value.
1833-
If `contentEncoding` is used along with setting a different `Content-Transfer-Encoding` value with the `headers` field, the result is undefined.
1820+
1821+
+Using `contentEncoding` for a multipart field is equivalent to specifying an [Encoding Object](#encodingObject) with a `headers` field containing `Content-Transfer-Encoding` with a schema that requires the value used in `contentEncoding`.
1822+
+If `contentEncoding` is used for a multipart field that has an Encoding Object with a `headers` field containing `Content-Transfer-Encoding` with a schema that disallows the value from `contentEncoding`, the result is undefined for serialization and parsing.
1823+
1824+
Note that as stated in [Working with Binary Data](#binaryData), if the Encoding Object's `contentType`, whether set explicitly or implicitly through its default value rules, disagrees with the `contentMediaType` in a Schema Object, the `contentMediaType` SHALL be ignored.
1825+
Because of this, and because the Encoding Object's `contentType` defaulting rules do not take the Schema Object's` contentMediaType` into account, the use of `contentMediaType` with an Encoding Object is NOT RECOMMENDED.
18341826

18351827
See [Appendix E](#percentEncodingAndFormMediaTypes) for a detailed examination of percent-encoding concerns for form media types.
18361828

18371829
###### Example: Basic Multipart Form
18381830

1839-
Examples:
1831+
When the `encoding` attribute is _not_ used, the encoding is determined by the Encoding Object's defaults:
18401832

18411833
```yaml
18421834
requestBody:
@@ -1846,27 +1838,16 @@ requestBody:
18461838
type: object
18471839
properties:
18481840
id:
1841+
# default for primitives without a special format is text/plain
18491842
type: string
18501843
format: uuid
1851-
address:
1852-
# default Content-Type for objects is `application/json`
1853-
type: object
1854-
properties: {}
18551844
profileImage:
1856-
# default Content-Type for properties with type string and a contentEncoding
1857-
# is `application/octet-stream`, so `image/png` must be set using contentMediaType
1845+
# default for string with binary format is `application/octet-stream`
18581846
type: string
1859-
contentMediaType: image/png
1860-
contentEncoding: base64
1861-
children:
1862-
# default Content-Type for arrays is based on the items subschema type, which
1863-
# is a string, producing a default of `text/plain`
1864-
type: array
1865-
items:
1866-
type: string
1847+
format: binary
18671848
addresses:
1868-
# default Content-Type for arrays is based on the items subschema type, which
1869-
# is an object, producing a default of `application/json`
1849+
# default for arrays is based on the type in the `items`
1850+
# subschema, which is an object, so `application/json`
18701851
type: array
18711852
items:
18721853
type: object
@@ -1875,7 +1856,8 @@ requestBody:
18751856
18761857
###### Example: Multipart Form with Encoding Objects
18771858
1878-
`multipart/form-data` allows for binary parts:
1859+
Using `encoding`, we can set more specific types for binary data, or non-JSON formats for complex values.
1860+
We can also describe headers for each part:
18791861

18801862
```yaml
18811863
requestBody:
@@ -1885,25 +1867,30 @@ requestBody:
18851867
type: object
18861868
properties:
18871869
id:
1888-
# default is text/plain
1870+
# default is `text/plain`
18891871
type: string
18901872
format: uuid
1891-
address:
1892-
# default is application/json
1893-
type: object
1894-
properties: {}
1895-
historyMetadata:
1896-
# need to declare XML format!
1897-
description: metadata in XML format
1898-
type: object
1899-
properties: {}
1900-
profileImage: {}
1873+
addresses:
1874+
# default based on the `items` subschema would be
1875+
# `application/json`, but we want these address objects
1876+
# serialized as `application/xml` instead
1877+
description: addresses in XML format
1878+
type: array
1879+
items:
1880+
$ref: '#/components/schemas/Address'
1881+
profileImage:
1882+
# default is application/octet-stream, but we can declare
1883+
# a more specific image type or types
1884+
type: string
1885+
format: binary
19011886
encoding:
1902-
historyMetadata:
1887+
addresses:
19031888
# require XML Content-Type in utf-8 encoding
1889+
# This is applied to each address part corresponding
1890+
# to each address in he array
19041891
contentType: application/xml; charset=utf-8
19051892
profileImage:
1906-
# only accept png/jpeg
1893+
# only accept png or jpeg
19071894
contentType: image/png, image/jpeg
19081895
headers:
19091896
X-Rate-Limit-Limit:
@@ -1914,7 +1901,7 @@ requestBody:
19141901
19151902
###### Example: Multipart Form with Multiple Files
19161903
1917-
To upload multiple files, a `multipart` media type MUST be used:
1904+
In accordance with [RFC7578 §4.3](https://www.rfc-editor.org/rfc/rfc7578.html#section-4.3), multiple files for a single form field are uploaded using the same name (`file` in this example) for each file's part:
19181905

19191906
```yaml
19201907
requestBody:

0 commit comments

Comments
 (0)