Skip to content

Commit dfa9d88

Browse files
committed
Support ordered multipart including streaming
This adds support for all `multipart` media types that do not have named parts, including support for streaming such media types. Note that `multipart/mixed` defines the basic processing rules for all `multipart` types, and implementations that encounter unrecognized `multipart` subtypes are required to process them as `multipart/mixed`. Therefore support for `multipart/mixed` addresses all other subtypes to some degree. This builds on the recent support for sequential media types: * `multipart/mixed` and similar meet the definition for a sequential media type, requiring it to be modeled as an array. This does use an expansive definition of "repeating the same structure", where the structure is literally any content with a media type. * As a sequential media type, it also supports `itemSchema` * Adding a parallel `itemEncoding` is the obvious solution to `multipart/mixed` streams requiring an Encoding Object * We have regularly received requests to support truly mixed `multipart/mixed` payloads, and previously claimed such support from 3.0.0 onwards, without actually supporting it. Adding `prefixEncoding` along with `itemEncoding` supports this use case with a clear parallel to `prefixItems`, which is the schema construct needed to support this case. * There is no need for a `prefixSchema` field because the streaming use case requires a repetition of the same schema for each item. Therefore all mixed use cases can use `schema` and `prefixItems`
1 parent f855d47 commit dfa9d88

File tree

2 files changed

+141
-15
lines changed

2 files changed

+141
-15
lines changed

src/oas.md

Lines changed: 129 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,17 @@ Some examples of sequential media types (including some that are not IANA-regist
101101
application/json-seq
102102
application/geo+json-seq
103103
text/event-stream
104+
multipart/mixed
104105
```
105106

106107
In the first three above, the repeating structure is any [JSON value](https://tools.ietf.org/html/rfc8259#section-3).
107-
The fourth repeats `application/geo+json`-structured values, while the last repeats a custom text format related to Server-Sent Events.
108+
The fourth repeats `application/geo+json`-structured values, while `text/event-stream` repeats a custom text format related to Server-Sent Events.
109+
The final media type listed above, `multipart/mixed`, provides an ordered list of documents of any media type, and is sometimes streamed.
108110

109111
Implementations MUST support mapping sequential media types into the JSON Schema data model by treating them as if the values were in an array in the same order.
110112

111113
See [Complete vs Streaming Content](#complete-vs-streaming-content) for more information on handling sequential media type in a streaming context, including special considerations for `text/event-stream` content.
114+
For `multipart` types, see also [Encoding By Position](#encoding-by-position).
112115

113116
#### Media Type Registry
114117

@@ -1150,7 +1153,9 @@ See [Working With Examples](#working-with-examples) for further guidance regardi
11501153
| <a name="media-type-item-schema"></a>itemSchema | [Schema Object](#schema-object) | A schema describing each item within a [sequential media type](#sequential-media-types). |
11511154
| <a name="media-type-example"></a>example | Any | Example of the media type; see [Working With Examples](#working-with-examples). |
11521155
| <a name="media-type-examples"></a>examples | Map[ `string`, [Example Object](#example-object) \| [Reference Object](#reference-object)] | Examples of the media type; see [Working With Examples](#working-with-examples). |
1153-
| <a name="media-type-encoding"></a>encoding | Map[`string`, [Encoding Object](#encoding-object)] | A map between a property name and its encoding information, as defined under [Encoding Usage and Restrictions](#encoding-usage-and-restrictions). The `encoding` field SHALL only apply when the media type is `multipart` or `application/x-www-form-urlencoded`. If no Encoding Object is provided for a property, the behavior is determined by the default values documented for the Encoding Object. |
1156+
| <a name="media-type-encoding"></a>encoding | Map[`string`, [Encoding Object](#encoding-object)] | A map between a property name and its encoding information, as defined under [Encoding By Name](#encoding-by-name). The `encoding` field SHALL only apply when the media type is `multipart` or `application/x-www-form-urlencoded`. If no Encoding Object is provided for a property, the behavior is determined by the default values documented for the Encoding Object. This field MUST NOT be present if `prefixEncoding` or `itemEncoding` are present. |
1157+
| <a name="media-type-prefix-encoding"></a>prefixEncoding | [[Encoding Object](#encoding-object)] | An array of positional encoding information, as defined under [Encoding By Position](#encoding-by-position). The `prefixEncoding` field SHALL only apply when the media type is `multipart`. If no Encoding Object is provided for a property, the behavior is determined by the default values documented for the Encoding Object. This field MUST NOT be present if `encoding` is present. |
1158+
| <a name="media-type-item-encoding"></a>itemEncoding | [Encoding Object](#encoding-object) | A single Encoding Object that provides encoding information for multiple array items, as defined under [Encoding By Position](#encoding-by-position). The `itemEncoding` field SHALL only apply when the media type is `multipart`. If no Encoding Object is provided for a property, the behavior is determined by the default values documented for the Encoding Object. This field MUST NOT be present if `encoding` is present. |
11541159

11551160
This object MAY be extended with [Specification Extensions](#specification-extensions).
11561161

@@ -1170,7 +1175,8 @@ For this use case, `maxLength` MAY be implemented outside of regular JSON Schema
11701175

11711176
###### Streaming Sequential Media Types
11721177

1173-
The `itemSchema` field is provided to support streaming use cases for sequential media types.
1178+
The `itemSchema` field is provided to support streaming use cases for sequential media types, with `itemEncoding` as a corresponding encoding mechanism for streaming [positional `multipart` media types](#encoding-by-position).
1179+
11741180
Unlike `schema`, which is applied to the complete content (treated as an array as described in the [sequential media types](#sequential-media-types) section), `itemSchema` MUST be applied to each item in the stream independently, which supports processing each item as it is read from the stream.
11751181

11761182
Both `schema` and `itemSchema` MAY be used in the same Media Type Object.
@@ -1206,13 +1212,16 @@ properties:
12061212

12071213
##### Encoding Usage and Restrictions
12081214

1209-
The `encoding` field defines how to map each [Encoding Object](#encoding-object) to a specific value in the data.
1215+
The three encoding fields define how to map each [Encoding Object](#encoding object) to a specific value in the data.
1216+
Each field has its own set of media types with which it can be used; for all other media types all three fields SHALL be ignored.
12101217

1211-
To use the `encoding` field, a `schema` MUST exist, and the `encoding` field's keys MUST exist in the schema as properties.
1212-
Array properties MUST be handled by applying the given Encoding Object to one part per array item, each with the same `name`, as is recommended by [[?RFC7578]] [Section 4.3](https://www.rfc-editor.org/rfc/rfc7578.html#section-4.3) for supplying multiple values per form field.
1213-
For all other value types for both top-level non-array properties and for values, including array values, within a top-level array, the Encoding Object MUST be applied to the entire value.
1218+
###### Encoding By Name
12141219

12151220
The behavior of the `encoding` field is designed to support web forms, and is therefore only defined for media types structured as name-value pairs that allow repeat values, most notably `application/x-www-form-urlencoded` and `multipart/form-data`.
1221+
1222+
To use the `encoding` field, each key under the field MUST exist in the `schema` as a property.
1223+
Array properties MUST be handled by applying the given Encoding Object to produce one encoded value per array item, each with the same `name`, as is recommended by [[?RFC7578]] [Section 4.3](https://www.rfc-editor.org/rfc/rfc7578.html#section-4.3) for supplying multiple values per form field.
1224+
For all other value types for both top-level non-array properties and for values, including array values, within a top-level array, the Encoding Object MUST be applied to the entire value.
12161225
The order of these name-value pairs in the target media type is implementation-defined.
12171226

12181227
For `application/x-www-form-urlencoded`, the encoding keys MUST map to parameter names, with the values produced according to the rules of the [Encoding Object](#encoding-object).
@@ -1221,15 +1230,29 @@ See [Encoding the `x-www-form-urlencoded` Media Type](#encoding-the-x-www-form-u
12211230
For `multipart`, the encoding keys MUST map to the [`name` parameter](https://www.rfc-editor.org/rfc/rfc7578#section-4.2) of the `Content-Disposition: form-data` header of each part, as is defined for `multipart/form-data` in [[?RFC7578]].
12221231
See [[?RFC7578]] [Section 5](https://www.rfc-editor.org/rfc/rfc7578.html#section-5) for guidance regarding non-ASCII part names.
12231232

1224-
Other `multipart` media types are not directly supported as they do not define a mechanism for part names.
1225-
However, the usage of a `name` [`Content-Disposition` parameter](https://www.iana.org/assignments/cont-disp/cont-disp.xhtml#cont-disp-2) is defined for the `form-data` [`Content-Disposition` value](https://www.iana.org/assignments/cont-disp/cont-disp.xhtml#cont-disp-1), which is not restricted to `multipart/form-data`.
1226-
Implementations MAY choose to support the a `Conent-Disposition` of `form-data` with a `name` parameter in other `multipart` media types in order to use the `encoding` field with them, but this usage is unlikely to be supported by generic `multipart` implementations.
1227-
12281233
See [Encoding `multipart` Media Types](#encoding-multipart-media-types) for further guidance and examples, both with and without the `encoding` field.
12291234

1235+
###### Encoding By Position
1236+
1237+
Most `multipart` media types, including `multipart/mixed` which defines the underlying rules for parsing all `multipart` types, do not have named parts.
1238+
Data for these media types are modeled as an array, with one item per part, in order.
1239+
1240+
To use the `prefixEncoding` and/or `itemEncoding` fields, either an array `schema` or `itemSchema` MUST be present.
1241+
These fields are analogous to the `prefixItems` and `items` JSON Schema keywords, with `prefixEncoding` (if present) providing an array of Encoding Objects that are each applied to the value at the same position in the data array, and `itemEncoding` applying its single Encoding Object to all remaining items in the array.
1242+
1243+
The `itemEncoding` field can also be used with `itemSchema` to support streaming `multipart` content.
1244+
1245+
###### Additional Encoding Approaches
1246+
1247+
The `prefixEncoding` field can be used with any `multipart` content to require a fixed part order.
1248+
This includes `multipart/form-data`, for which the Encoding Object's `headers` field MUST be used to provide the `Content-Disposition` and part name, as no property names exist to provide the names automatically.
1249+
1250+
Prior versions of this specifications advised using the `name` [`Content-Disposition` parameter](https://www.iana.org/assignments/cont-disp/cont-disp.xhtml#cont-disp-2) of the `form-data` [`Content-Disposition` value](https://www.iana.org/assignments/cont-disp/cont-disp.xhtml#cont-disp-1) with `multipart` media types other than `multipart/form-data` in order to work around the limitations of the `encoding` field.
1251+
Implementations MAY choose to support this workaround, but as this usage is not common, implementations of non-`form-data` `multipart` media types are unlikely to support it.
1252+
12301253
##### Media Type Examples
12311254

1232-
For form-related media type examples, see the [Encoding Object](#encoding-object).
1255+
For form-related and `multipart` media type examples, see the [Encoding Object](#encoding-object).
12331256

12341257
###### JSON
12351258

@@ -1542,8 +1565,9 @@ These fields MAY be used either with or without the RFC6570-style serialization
15421565
This object MAY be extended with [Specification Extensions](#specification-extensions).
15431566

15441567
The default values for `contentType` are as follows, where an _n/a_ in the `contentEncoding` column means that the presence or value of `contentEncoding` is irrelevant.
1545-
This table is based on the value to which the Encoding Object is being applied, which as defined under [Encoding Usage and Restrictions](#encoding-usage-and-restrictions) is the array item for properties of type `"array"`, and the entire value for all other types.
1546-
Therefore the `array` row in this table applies only to array values inside of a top-level array.
1568+
This table is based on the value to which the Encoding Object is being applied as defined under [Encoding Usage and Restrictions](#encoding-usage-and-restrictions).
1569+
Note that in the case of [Encoding By Name](#encoding-by-name), this value is the array item for properties of type `"array"`, and the entire value for all other types.
1570+
Therefore the `array` row in this table applies only to array values inside of a top-level array when encoding by name.
15471571

15481572
| `type` | `contentEncoding` | Default `contentType` |
15491573
| ---- | ---- | ---- |
@@ -1766,6 +1790,97 @@ requestBody:
17661790

17671791
As seen in the [Encoding Object's `contentType` field documentation](#encoding-content-type), the empty schema for `items` indicates a media type of `application/octet-stream`.
17681792

1793+
###### Example: Ordered, Unnamed Multipart
1794+
1795+
A `multipart/mixed` payload consisting of a JSON metadata document followed by an image which the metadata describes:
1796+
1797+
```yaml
1798+
multipart/mixed:
1799+
schema:
1800+
prefixItems:
1801+
- # default content type for objects
1802+
# is `application/json`type: object
1803+
properties:
1804+
author:
1805+
type: string
1806+
created:
1807+
type: string
1808+
format: datetime
1809+
copyright:
1810+
type: string
1811+
license:
1812+
type: string
1813+
- # default content type for a schema without `type`
1814+
# is `application/octet-stream`, which we need
1815+
# to override.
1816+
{}
1817+
prefixEncoding:
1818+
- # Encoding Object defaults are correct for JSON
1819+
{}
1820+
- contentType: image/*
1821+
```
1822+
1823+
###### Example: Ordered Multipart With Required Header
1824+
1825+
As described in [[?RFC2557]], a set of HTML pages can be sent in a `multipart/related` payload, preserving links among themselves by defining a `Content-Location` header for each page.
1826+
1827+
See [Appendix D](appendix-d-serializing-headers-and-cookies) for an explanation of why `content: {text/plain: {...}}` is used to describe the header value.
1828+
1829+
```yaml
1830+
multipart/related:
1831+
schema:
1832+
items:
1833+
type: string
1834+
itemEncoding:
1835+
contentType: text/html
1836+
headers:
1837+
Content-Location:
1838+
required: true
1839+
content:
1840+
text/plain:
1841+
schema:
1842+
type: string
1843+
format: uri
1844+
```
1845+
1846+
While the above example could have used `itemSchema` instead, if the payload is expected to be processed all at once, using `schema` ensures that tools will wait until the complete response is available before processing.
1847+
1848+
###### Example: Streaming Multipart
1849+
1850+
This example assumes a device that takes large sets of pictures and streams them to the caller.
1851+
Unlike the previous example, we use `itemSchema` here because the expectation is that each image is processed as it arrives (or in small batches), since we know that buffering the entire stream will take too much memory.
1852+
1853+
```yaml
1854+
multipart/mixed:
1855+
itemSchema:
1856+
$comment: A single data image from the device
1857+
itemEncoding:
1858+
contentType: image/jpg
1859+
```
1860+
1861+
###### Example: Streaming Byte Ranges
1862+
1863+
For `multipart/byteranges` [[RFC9110]] [Section 14.6](https://www.rfc-editor.org/rfc/rfc9110.html#section-14.6), a `Content-Range` header is required:
1864+
1865+
See [Appendix D](appendix-d-serializing-headers-and-cookies) for an explanation of why `content: {text/plain: {...}}` is used to describe the header value.
1866+
1867+
```yaml
1868+
multipart/byteranges:
1869+
itemSchema:
1870+
$comment: A single range of bytes from a video
1871+
itemEncoding:
1872+
contentType: video/mp4
1873+
headers:
1874+
Content-Range:
1875+
required: true
1876+
content:
1877+
text/plain:
1878+
schema:
1879+
# A suitable "pattern" constraint for this
1880+
# header is left as an exercise for the reader
1881+
type: string
1882+
```
1883+
17691884
#### Responses Object
17701885

17711886
A container for the expected responses of an operation.

src/schemas/validation/schema.yaml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -511,9 +511,20 @@ $defs:
511511
type: object
512512
additionalProperties:
513513
$ref: '#/$defs/encoding'
514+
prefixEncoding:
515+
type: array
516+
items:
517+
$ref: '#/$defs/encoding'
518+
itemEncoding:
519+
$ref: '#/$defs/encoding'
514520
allOf:
515-
- $ref: '#/$defs/specification-extensions'
516521
- $ref: '#/$defs/examples'
522+
- $ref: '#/$defs/specification-extensions'
523+
- dependentSchema:
524+
encoding:
525+
properties:
526+
prefixEncoding: false
527+
itemEncoding: false
517528
unevaluatedProperties: false
518529

519530
encoding:

0 commit comments

Comments
 (0)