Skip to content

Commit 691f6fc

Browse files
committed
Sequential media types and link to registry
This adds a link to the forthcoming media type registry, and also adds support for various sequential media types: * `application/json-seq` * `application/jsonl` * `application/x-ndjson` * `text/event-stream` Given how various modeling and encoding techniques are scattered throughout the specification, the Media Types section seemed like the best place to add these, preceded by a link to he new Media Type Registry which will essentially be a catalog of where to find the existing guidance for various media types.
1 parent 04cf0a5 commit 691f6fc

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

src/oas.md

+124
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,130 @@ Some examples of possible media type definitions:
8484
application/vnd.github.v3.patch
8585
```
8686

87+
#### Media Type Registry
88+
89+
While the [Schema Object](#schema-object) is designed to describe and validate JSON, several other media types are commonly used in APIs.
90+
Requirements regarding support for other media types are documented in this Media Types section and in several Object sections later in this specification.
91+
For convenience and future extensibility, these are cataloged in the OpenAPI Initiative's [Media Type Registry](https://spec.openapis.org/registry/media-type/), which indicates where in this specification the relevant requirements can be found.
92+
93+
#### Sequential Media Types
94+
95+
Several media types exist to transport a sequence of values, separated by some delimiter, either as a single document or as multiple documents representing chunks of a logical stream.
96+
Depending on the media type, the values could either be in another existing format such as JSON, or in a custom format specific to the sequential media type.
97+
98+
Implementations MUST support modeling sequential media types with the [Schema Object](#schema-object) by treating the sequence as an array with the same items and ordering as the sequence.
99+
100+
##### Working With Indefinite-Length Streams
101+
102+
In addition to regular document-style use, sequential media types can be used to represent some portion of a stream that may not have a well-defined beginning or end.
103+
In such use cases, either the client or server makes a decision to work with one or more elements in the sequence at a time, but this subsequence is not a complete array in the sense of normal JSON arrays.
104+
105+
OpenAPI Description authors are responsible for avoiding the use of JSON Schema keywords such as `prefixItems`, `minItems`, `maxItems`, `contains`, `minContains`, or `maxContains` that rely on a beginning (for relative positioning) or an ending (to determine if a threshold has been reached or a limit has been exceeded) when the sequence is intended to represent a subsequence of a larger stream.
106+
If such keywords are used, their behavior remains well-defined but may be counter-intuitive for users that expect them to apply to the stream as a whole rather than each subsequence as it is processed.
107+
108+
##### Sequential JSON
109+
110+
For any media type where the items in the sequence are JSON values, no conversion beyond treating the sequence as an array is required.
111+
JSON Text Sequences (`application/json-seq` and the `+json-seq` suffix, [[?RFC7464]]), JSON Lines (`application/jsonl`), and NDJSON (`application/x-ndjson`) are all in this category.
112+
Note that the media types for JSON Lines and NDJSON are not registered with the IANA, but are in common use.
113+
114+
The following example, which uses `application/json-seq` but would be identical aside from the media type for either `application/jsonl` or `application/ndjson`, models a finite stream consisting of a single metadata document followed by an indefinite number of data documents consisting of numeric measurements with units:
115+
116+
```YAML
117+
content:
118+
application/json-seq:
119+
schema:
120+
type: array
121+
prefixItems:
122+
- $comment: Metadata for all subsequent data documents
123+
type: object
124+
required:
125+
- subject
126+
- dateCollected
127+
properties:
128+
subject:
129+
type: string
130+
dateCollected:
131+
type: string
132+
format: date-time
133+
items:
134+
$comment: A JSON document holding data
135+
type: object
136+
required:
137+
- measurement
138+
- unit
139+
properties:
140+
measurement:
141+
type: number
142+
unit:
143+
type: string
144+
```
145+
146+
##### Server-Sent Event Streams
147+
148+
The `text/event-stream` from the [HTML specification](https://html.spec.whatwg.org/multipage/iana.html#text/event-stream), which is also not IANA-registered, uses a custom named field format for its items.
149+
Field names can be repeated within an item to allow splitting the value across multiple lines; such split values MUST be treated the same as if they were a single field, with newlines added as required by the `text/event-stream` specification.
150+
151+
Field value types MUST be handled as specified by the `text/event-stream` specification (e.g. the `retry` field value is modeled as a JSON number that is expected to be of JSON Schema `type: integer`), and fields not given an explicit value type MUST be handled as strings.
152+
153+
The `text/event-stream` specification requires that fields with Unknown names, as well as `id` fields where the value contains `U+0000 NULL` be ignored.
154+
These fields SHOULD NOT be present in the data used with the Schema Object.
155+
156+
For example, the following `text/event-stream` document:
157+
158+
```EVENTSTREAM
159+
event: add
160+
data: This data is formatted
161+
data: across two lines
162+
retry: 5
163+
164+
event: add
165+
data: 1234.5678
166+
unknown-field: this is ignored
167+
```
168+
169+
is equivalent to this JSON instance for the purpose of working with the Schema Object:
170+
171+
```JSON
172+
[
173+
{
174+
"event": "add",
175+
"data": "This data is formatted\nacross two lines",
176+
"retry": 5
177+
},
178+
{
179+
"event": "add",
180+
"data": "1234.5678"
181+
}
182+
]
183+
```
184+
185+
Note that `"1234.5678"` is a string, which avoids problems with number sizes and precision.
186+
See [Data Type Format](#data-type-format) for options for handling numbers transported as strings.
187+
Note also the newline inserted in the string in the first entry, and the absence of the field labeled `unknown-field` in the second entry.
188+
189+
The following Schema Object is a generic schema for the `text/event-stream` media type as documented by the HTML specification as of the time of this writing:
190+
191+
```YAML
192+
type: array
193+
items:
194+
type: object
195+
required:
196+
- data
197+
properties:
198+
data:
199+
type: string
200+
event:
201+
type: string
202+
id:
203+
type: string
204+
retry:
205+
type: integer
206+
```
207+
208+
Some users of `text/event-stream` use a format such as JSON for field values, particularly the `data` field.
209+
Use JSON Schema's keywords for working with the [contents of string-encoded data](https://www.ietf.org/archive/id/draft-bhutton-json-schema-validation-01.html#name-a-vocabulary-for-the-conten), particularly `contentMediaType` and `contentSchema`, to describe and validate such fields with more detail than string-related validation keywords such as `pattern` can support.
210+
87211
### HTTP Status Codes
88212

89213
The HTTP Status Codes are used to indicate the status of the executed operation.

0 commit comments

Comments
 (0)