Skip to content

Commit 840e43a

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 22fbdc9 commit 840e43a

File tree

5 files changed

+81
-15
lines changed

5 files changed

+81
-15
lines changed

src/oas.md

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,14 +101,18 @@ 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.
110+
Note that while `multipart` formats technically allow a preamble and an epilogue, the RFC directs that they are to be ignored, making them effectively comments, and this specification does not model them.
108111

109112
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.
110113

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

113117
#### Media Type Registry
114118

@@ -1253,7 +1257,9 @@ See [Working With Examples](#working-with-examples) for further guidance regardi
12531257
| <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). |
12541258
| <a name="media-type-example"></a>example | Any | Example of the media type; see [Working With Examples](#working-with-examples). |
12551259
| <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. |
1260+
| <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. |
1261+
| <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. |
1262+
| <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. |
12571263

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

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

12741280
###### Streaming Sequential Media Types
12751281

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

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

13101317
##### Encoding Usage and Restrictions
13111318

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

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.
1322+
###### Encoding By Name
13171323

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

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

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-
13311337
See [Encoding `multipart` Media Types](#encoding-multipart-media-types) for further guidance and examples, both with and without the `encoding` field.
13321338

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

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

13371361
###### JSON
13381362

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

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

16511676
| `type` | `contentEncoding` | Default `contentType` |
16521677
| ---- | ---- | ---- |

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+
- dependentSchemas:
546+
encoding:
547+
properties:
548+
prefixEncoding: false
549+
itemEncoding: false
539550
unevaluatedProperties: false
540551

541552
encoding:
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
openapi: 3.2.0
2+
info:
3+
title: API
4+
version: 1.0.0
5+
components:
6+
requestBodies:
7+
content:
8+
multipart/mixed:
9+
encoding: {}
10+
itemEncoding: {}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
openapi: 3.2.0
2+
info:
3+
title: API
4+
version: 1.0.0
5+
components:
6+
requestBodies:
7+
content:
8+
multipart/mixed:
9+
encoding: {}
10+
prefixEncoding: {}

tests/schema/pass/media-type-examples.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,13 @@ paths:
138138
forCoverage2:
139139
style: spaceDelimited
140140
explode: true
141+
multipart/related:
142+
schema:
143+
type: array
144+
itemEncoding:
145+
contentType: text/plain
146+
prefixEncoding:
147+
- headers:
148+
Content-Location:
149+
schema:
150+
type: string

0 commit comments

Comments
 (0)