Skip to content

Commit afeedf8

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 a43f132 commit afeedf8

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

@@ -1253,7 +1256,9 @@ See [Working With Examples](#working-with-examples) for further guidance regardi
12531256
| <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). |
12541257
| <a name="media-type-example"></a>example | Any | Example of the media type; see [Working With Examples](#working-with-examples). |
12551258
| <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). |
1256-
| <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. |
1259+
| <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. |
1260+
| <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. |
1261+
| <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. |
12571262

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

@@ -1273,7 +1278,8 @@ For this use case, `maxLength` MAY be implemented outside of regular JSON Schema
12731278

12741279
###### Streaming Sequential Media Types
12751280

1276-
The `itemSchema` field is provided to support streaming use cases for sequential media types.
1281+
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).
1282+
12771283
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.
12781284

12791285
Both `schema` and `itemSchema` MAY be used in the same Media Type Object.
@@ -1309,13 +1315,16 @@ properties:
13091315

13101316
##### Encoding Usage and Restrictions
13111317

1312-
The `encoding` field defines how to map each [Encoding Object](#encoding-object) to a specific value in the data.
1318+
The three encoding fields define how to map each [Encoding Object](#encoding object) to a specific value in the data.
1319+
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.
13131320

1314-
To use the `encoding` field, a `schema` MUST exist, and the `encoding` field's keys MUST exist in the schema as properties.
1315-
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.
1316-
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.
1321+
###### Encoding By Name
13171322

13181323
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`.
1324+
1325+
To use the `encoding` field, each key under the field MUST exist in the `schema` as a property.
1326+
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.
1327+
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.
13191328
The order of these name-value pairs in the target media type is implementation-defined.
13201329

13211330
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).
@@ -1324,15 +1333,29 @@ See [Encoding the `x-www-form-urlencoded` Media Type](#encoding-the-x-www-form-u
13241333
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]].
13251334
See [[?RFC7578]] [Section 5](https://www.rfc-editor.org/rfc/rfc7578.html#section-5) for guidance regarding non-ASCII part names.
13261335

1327-
Other `multipart` media types are not directly supported as they do not define a mechanism for part names.
1328-
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`.
1329-
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.
1330-
13311336
See [Encoding `multipart` Media Types](#encoding-multipart-media-types) for further guidance and examples, both with and without the `encoding` field.
13321337

1338+
###### Encoding By Position
1339+
1340+
Most `multipart` media types, including `multipart/mixed` which defines the underlying rules for parsing all `multipart` types, do not have named parts.
1341+
Data for these media types are modeled as an array, with one item per part, in order.
1342+
1343+
To use the `prefixEncoding` and/or `itemEncoding` fields, either an array `schema` or `itemSchema` MUST be present.
1344+
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.
1345+
1346+
The `itemEncoding` field can also be used with `itemSchema` to support streaming `multipart` content.
1347+
1348+
###### Additional Encoding Approaches
1349+
1350+
The `prefixEncoding` field can be used with any `multipart` content to require a fixed part order.
1351+
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.
1352+
1353+
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.
1354+
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.
1355+
13331356
##### Media Type Examples
13341357

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

13371360
###### JSON
13381361

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

16471670
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.
1648-
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.
1649-
Therefore the `array` row in this table applies only to array values inside of a top-level array.
1671+
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).
1672+
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.
1673+
Therefore the `array` row in this table applies only to array values inside of a top-level array when encoding by name.
16501674

16511675
| `type` | `contentEncoding` | Default `contentType` |
16521676
| ---- | ---- | ---- |
@@ -1869,6 +1893,97 @@ requestBody:
18691893

18701894
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`.
18711895

1896+
###### Example: Ordered, Unnamed Multipart
1897+
1898+
A `multipart/mixed` payload consisting of a JSON metadata document followed by an image which the metadata describes:
1899+
1900+
```yaml
1901+
multipart/mixed:
1902+
schema:
1903+
prefixItems:
1904+
- # default content type for objects
1905+
# is `application/json`type: object
1906+
properties:
1907+
author:
1908+
type: string
1909+
created:
1910+
type: string
1911+
format: datetime
1912+
copyright:
1913+
type: string
1914+
license:
1915+
type: string
1916+
- # default content type for a schema without `type`
1917+
# is `application/octet-stream`, which we need
1918+
# to override.
1919+
{}
1920+
prefixEncoding:
1921+
- # Encoding Object defaults are correct for JSON
1922+
{}
1923+
- contentType: image/*
1924+
```
1925+
1926+
###### Example: Ordered Multipart With Required Header
1927+
1928+
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.
1929+
1930+
See [Appendix D](appendix-d-serializing-headers-and-cookies) for an explanation of why `content: {text/plain: {...}}` is used to describe the header value.
1931+
1932+
```yaml
1933+
multipart/related:
1934+
schema:
1935+
items:
1936+
type: string
1937+
itemEncoding:
1938+
contentType: text/html
1939+
headers:
1940+
Content-Location:
1941+
required: true
1942+
content:
1943+
text/plain:
1944+
schema:
1945+
type: string
1946+
format: uri
1947+
```
1948+
1949+
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.
1950+
1951+
###### Example: Streaming Multipart
1952+
1953+
This example assumes a device that takes large sets of pictures and streams them to the caller.
1954+
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.
1955+
1956+
```yaml
1957+
multipart/mixed:
1958+
itemSchema:
1959+
$comment: A single data image from the device
1960+
itemEncoding:
1961+
contentType: image/jpg
1962+
```
1963+
1964+
###### Example: Streaming Byte Ranges
1965+
1966+
For `multipart/byteranges` [[RFC9110]] [Section 14.6](https://www.rfc-editor.org/rfc/rfc9110.html#section-14.6), a `Content-Range` header is required:
1967+
1968+
See [Appendix D](appendix-d-serializing-headers-and-cookies) for an explanation of why `content: {text/plain: {...}}` is used to describe the header value.
1969+
1970+
```yaml
1971+
multipart/byteranges:
1972+
itemSchema:
1973+
$comment: A single range of bytes from a video
1974+
itemEncoding:
1975+
contentType: video/mp4
1976+
headers:
1977+
Content-Range:
1978+
required: true
1979+
content:
1980+
text/plain:
1981+
schema:
1982+
# A suitable "pattern" constraint for this
1983+
# header is left as an exercise for the reader
1984+
type: string
1985+
```
1986+
18721987
#### Responses Object
18731988

18741989
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
@@ -533,9 +533,20 @@ $defs:
533533
type: object
534534
additionalProperties:
535535
$ref: '#/$defs/encoding'
536+
prefixEncoding:
537+
type: array
538+
items:
539+
$ref: '#/$defs/encoding'
540+
itemEncoding:
541+
$ref: '#/$defs/encoding'
536542
allOf:
537-
- $ref: '#/$defs/specification-extensions'
538543
- $ref: '#/$defs/examples'
544+
- $ref: '#/$defs/specification-extensions'
545+
- dependentSchema:
546+
encoding:
547+
properties:
548+
prefixEncoding: false
549+
itemEncoding: false
539550
unevaluatedProperties: false
540551

541552
encoding:

0 commit comments

Comments
 (0)