You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -84,6 +84,40 @@ Some examples of possible media type definitions:
84
84
application/vnd.github.v3.patch
85
85
```
86
86
87
+
JSON-based and JSON-compatible YAML-based media types can make direct use of the [Schema Object](#schema-object) as the Object uses JSON Schema.
88
+
The use of the Schema Object with other media types is handled by mapping them into the JSON Schema [instance data model](https://www.ietf.org/archive/id/draft-bhutton-json-schema-01.html#name-instance-data-model).
89
+
These mappings may be implicit based on the media type, or explicit based on the values of particular fields.
90
+
Each mapping is addressed where the relevant media type is discussed in this section or under the [Media Type Object](#media-type-object) or [Encoding Object](#encoding-object)
91
+
92
+
#### Sequential Media Types
93
+
94
+
Within this specification, a _sequential media type_ is defined as any media type that consists of a repeating structure, without any sort of header, footer, envelope, or other metadata in addition to the sequence.
95
+
96
+
Some examples of sequential media types (including some that are not IANA-registered but are in common use) are:
97
+
98
+
```text
99
+
application/jsonl
100
+
application/x-ndjson
101
+
application/json-seq
102
+
application/geo+json-seq
103
+
text/event-stream
104
+
```
105
+
106
+
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
+
109
+
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.
110
+
111
+
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.
112
+
113
+
#### Media Type Registry
114
+
115
+
While the [Schema Object](#schema-object) is designed to describe and validate JSON, several other media types are commonly used in APIs.
116
+
Requirements regarding support for other media types are documented in this Media Types section and in several Object sections later in this specification.
117
+
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.
118
+
119
+
See also the [Media Type Object](#media-type-object) for further information on working with specific media types.
120
+
87
121
### HTTP Status Codes
88
122
89
123
The HTTP Status Codes are used to indicate the status of the executed operation.
@@ -279,7 +313,7 @@ The `contentMediaType` keyword is redundant if the media type is already set:
279
313
280
314
If the [Schema Object](#schema-object) will be processed by a non-OAS-aware JSON Schema implementation, it may be useful to include `contentMediaType` even if it is redundant. However, if `contentMediaType` contradicts a relevant Media Type Object or Encoding Object, then `contentMediaType` SHALL be ignored.
281
315
282
-
The `maxLength` keyword MAY be used to set an expected upper bound on the length of a streaming payload. The keyword can be applied to either string data, including encoded binary data, or to unencoded binary data. For unencoded binary, the length is the number of octets.
316
+
See [Complete vs Streaming Content](#complete-vs-streaming-content) for guidance on streaming binary payloads.
283
317
284
318
##### Migrating binary descriptions from OAS 3.0
285
319
@@ -1602,7 +1636,8 @@ content:
1602
1636
1603
1637
#### Media Type Object
1604
1638
1605
-
Each Media Type Object provides schema and examples for the media type identified by its key.
1639
+
Each Media Type Object describes content structured in accordance with the media type identified by its key.
1640
+
Multiple Media Type Objects can be used to describe content that can appear in any of several different media types.
1606
1641
1607
1642
When `example` or `examples` are provided, the example SHOULD match the specified schema and be in the correct format as specified by the media type and its encoding.
1608
1643
The `example` and `examples` fields are mutually exclusive, and if either is present it SHALL _override_ any `example` in the schema.
@@ -1612,13 +1647,64 @@ See [Working With Examples](#working-with-examples) for further guidance regardi
1612
1647
1613
1648
| Field Name | Type | Description |
1614
1649
| ---- | :----: | ---- |
1615
-
| <a name="media-type-schema"></a>schema | [Schema Object](#schema-object) | The schema defining the content of the request, response, parameter, or header. |
1650
+
| <a name="media-type-schema"></a>schema | [Schema Object](#schema-object) | A schema describing the complete content of the request, response, parameter, or header. |
1651
+
| <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). |
1616
1652
| <a name="media-type-example"></a>example | Any | Example of the media type; see [Working With Examples](#working-with-examples). |
1617
1653
| <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). |
1618
1654
| <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. |
1619
1655
1620
1656
This object MAY be extended with [Specification Extensions](#specification-extensions).
1621
1657
1658
+
See also the [Media Type Registry](#media-type-registry).
1659
+
1660
+
##### Complete vs Streaming Content
1661
+
1662
+
The `schema` field MUST be applied to the complete content, as defined by the media type and the context ([Request Body Object](#request-body-object), [Response Object](#response-object), [Parameter Object](#parameter-object), or [Header Object](#header-object).
1663
+
Because this requires loading the content into memory in its entirety, it poses a challenge for streamed content.
1664
+
Use cases where client is intended to choose when to stop reading are particularly challenging as there is no well-defined end to the stream.
1665
+
1666
+
###### Binary Streams
1667
+
1668
+
The `maxLength` keyword MAY be used to set an expected upper bound on the length of a streaming payload that consists of either string data, including encoded binary data, or unencoded binary data.
1669
+
For unencoded binary, the length is the number of octets.
1670
+
For this use case, `maxLength` MAY be implemented outside of regular JSON Schema evaluation as JSON Schema does not directly apply to binary data, and an encoded binary stream may be impractical to store in memory in its entirety.
1671
+
1672
+
###### Streaming Sequential Media Types
1673
+
1674
+
The `itemSchema` field is provided to support streaming use cases for sequential media types.
1675
+
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.
1676
+
1677
+
Both `schema` and `itemSchema` MAY be used in the same Media Type Object.
1678
+
However, doing so is unlikely to have significant advantages over using the `items` keyword within the `schema` field.
1679
+
1680
+
##### Special Considerations for `text/event-stream` Content
1681
+
1682
+
For `text/event-stream`, implementations MUST work with event data after it has been parsed according to the [`text/event-stream` specification](https://html.spec.whatwg.org/multipage/server-sent-events.html#parsing-an-event-stream), including all guidance on ignoring certain fields (including comments) and/or values, and on combining values split across multiple lines.
1683
+
1684
+
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.
1685
+
1686
+
Some users of `text/event-stream` use a format such as JSON for field values, particularly the `data` field.
1687
+
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.
1688
+
Note that `contentSchema` is [not automatically validated by default](https://www.ietf.org/archive/id/draft-bhutton-json-schema-validation-01.html#name-implementation-requirements-2) (see also the [Non-validating constraint keywords](#non-validating-constraint-keywords) section of this specification).
1689
+
1690
+
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:
1691
+
1692
+
```YAML
1693
+
type: object
1694
+
required:
1695
+
- data
1696
+
properties:
1697
+
data:
1698
+
type: string
1699
+
event:
1700
+
type: string
1701
+
id:
1702
+
type: string
1703
+
retry:
1704
+
type: integer
1705
+
minimum: 0
1706
+
```
1707
+
1622
1708
##### Encoding Usage and Restrictions
1623
1709
1624
1710
The `encoding` field defines how to map each [Encoding Object](#encoding-object) to a specific value in the data.
@@ -1644,40 +1730,12 @@ See [Encoding `multipart` Media Types](#encoding-multipart-media-types) for furt
1644
1730
1645
1731
##### Media Type Examples
1646
1732
1647
-
```json
1648
-
{
1649
-
"application/json": {
1650
-
"schema": {
1651
-
"$ref": "#/components/schemas/Pet"
1652
-
},
1653
-
"examples": {
1654
-
"cat": {
1655
-
"summary": "An example of a cat",
1656
-
"value": {
1657
-
"name": "Fluffy",
1658
-
"petType": "Cat",
1659
-
"color": "White",
1660
-
"gender": "male",
1661
-
"breed": "Persian"
1662
-
}
1663
-
},
1664
-
"dog": {
1665
-
"summary": "An example of a dog with a cat's name",
1666
-
"value": {
1667
-
"name": "Puma",
1668
-
"petType": "Dog",
1669
-
"color": "Black",
1670
-
"gender": "Female",
1671
-
"breed": "Mixed"
1672
-
}
1673
-
},
1674
-
"frog": {
1675
-
"$ref": "#/components/examples/frog-example"
1676
-
}
1677
-
}
1678
-
}
1679
-
}
1680
-
```
1733
+
For form-related media type examples, see the [Encoding Object](#encoding-object).
1734
+
1735
+
###### JSON
1736
+
1737
+
Note that since this example is written in YAML, the Example Object `value` field can be formatted as YAML due to the trivial conversion to JSON.
1738
+
This avoids needing to embed JSON as a string.
1681
1739
1682
1740
```yaml
1683
1741
application/json:
@@ -1704,6 +1762,214 @@ application/json:
1704
1762
$ref: '#/components/examples/frog-example'
1705
1763
```
1706
1764
1765
+
Alternatively, since all JSON is valid YAML, the example value can use JSON syntax within a YAML document:
1766
+
1767
+
```yaml
1768
+
application/json:
1769
+
schema:
1770
+
$ref: '#/components/schemas/Pet'
1771
+
examples:
1772
+
cat:
1773
+
summary: An example of a cat
1774
+
value: {
1775
+
"name": "Fluffy",
1776
+
"petType": "Cat",
1777
+
"color": "White",
1778
+
"gender": "male",
1779
+
"breed": "Persian"
1780
+
}
1781
+
dog:
1782
+
summary: An example of a dog with a cat's name
1783
+
value: {
1784
+
"name": "Puma",
1785
+
"petType": "Dog",
1786
+
"color": "Black",
1787
+
"gender": "Female",
1788
+
"breed": "Mixed"
1789
+
}
1790
+
frog:
1791
+
$ref: '#/components/examples/frog-example'
1792
+
```
1793
+
1794
+
###### Sequential JSON
1795
+
1796
+
For any [sequential media type](#sequential-media-types) where the items in the sequence are JSON values, no conversion of each value is required.
1797
+
JSON Text Sequences ([[?RFC7464]] `application/json-seq` and [[?RFC8091]] the `+json-seq` structured suffix), [JSON Lines](https://jsonlines.org/) (`application/jsonl`), and [NDJSON](https://github.com/ndjson/ndjson-spec) (`application/x-ndjson`) are all in this category.
1798
+
Note that the media types for JSON Lines and NDJSON are not registered with the IANA, but are in common use.
1799
+
1800
+
The following example shows Media Type Objects for both streaming log entries and returning a fixed-length set in response to a query.
1801
+
This shows the relationship between `schema` and `itemSchema`, and when to use each even though the `examples` field is the same either way.
1802
+
1803
+
```YAML
1804
+
components:
1805
+
schemas:
1806
+
LogEntry:
1807
+
type: object
1808
+
properties:
1809
+
timestamp:
1810
+
type: string
1811
+
format: date-time
1812
+
level:
1813
+
type: integer
1814
+
minimum: 0
1815
+
message:
1816
+
type: string
1817
+
Log:
1818
+
type: array
1819
+
items:
1820
+
$ref: "#/components/schemas/LogEntry"
1821
+
maxItems: 100
1822
+
examples:
1823
+
LogJSONSeq:
1824
+
summary: Log entries in application/json-seq
1825
+
# JSON Text Sequences require an unprintable character
1826
+
# that cannot be escaped in a YAML string, and therefore
1827
+
# must be placed in an external document shown below
1828
+
externalValue: examples/log.json-seq
1829
+
LogJSONPerLine:
1830
+
summary: Log entries in application/jsonl or application/x-ndjson
1831
+
description: JSONL and NDJSON are identical for this example
1832
+
# Note that the value must be written as a string with newlines,
A stream of JSON-format log messages that can be read
1841
+
for as long as the application is running, and is available
1842
+
in any of the sequential JSON media types.
1843
+
content:
1844
+
application/json-seq:
1845
+
itemSchema:
1846
+
$ref: "#/components/schemas/LogEntry"
1847
+
examples:
1848
+
JSON-SEQ:
1849
+
$ref: "#/components/examples/LogJSONSeq"
1850
+
application/jsonl:
1851
+
itemSchema:
1852
+
$ref: "#/components/schemas/LogEntry"
1853
+
examples:
1854
+
JSONL:
1855
+
$ref: "#/components/examples/LogJSONPerLine"
1856
+
application/x-ndjson:
1857
+
itemSchema:
1858
+
$ref: "#/components/schemas/LogEntry"
1859
+
examples:
1860
+
NDJSON:
1861
+
$ref: "#/components/examples/LogJSONPerLine"
1862
+
LogExcerpt:
1863
+
description: |
1864
+
A response consisting of no more than 100 log records,
1865
+
generally as a result of a query of the historical log,
1866
+
available in any of the sequential JSON media types.
1867
+
content:
1868
+
application/json-seq:
1869
+
schema:
1870
+
$ref: "#/components/schemas/Log"
1871
+
examples:
1872
+
JSON-SEQ:
1873
+
$ref: "#/components/examples/LogJSONSeq"
1874
+
application/jsonl:
1875
+
schema:
1876
+
$ref: "#/components/schemas/Log"
1877
+
examples:
1878
+
JSONL:
1879
+
$ref: "#/components/examples/LogJSONPerLine"
1880
+
application/x-ndjson:
1881
+
schema:
1882
+
$ref: "#/components/schemas/Log"
1883
+
examples:
1884
+
NDJSON:
1885
+
$ref: "#/components/examples/LogJSONPerLine"
1886
+
```
1887
+
1888
+
Our `application/json-seq` example has to be an external document because of the use of both newlines and of the unprintable Record Separator (`0x1E`) character, which cannot be escaped in YAML block literals:
1889
+
1890
+
```JSONSEQ
1891
+
0x1E{
1892
+
"timestamp": "1985-04-12T23:20:50.52Z",
1893
+
"level": 1,
1894
+
"message": "Hi!"
1895
+
}
1896
+
0x1E{
1897
+
"timestamp": "1985-04-12T23:20:51.37Z",
1898
+
"level": 1,
1899
+
"message": "Bye!"
1900
+
}
1901
+
```
1902
+
1903
+
###### Server-Sent Event Streams
1904
+
1905
+
For this example, assume that the generic event schema provided in the "Special Considerations for `text/event-stream` Content" section is available at `#/components/schemas/Event`:
1906
+
1907
+
```YAML
1908
+
description: A request body to add a stream of typed data.
1909
+
required: true
1910
+
content:
1911
+
text/event-stream:
1912
+
itemSchema:
1913
+
$ref: "#/components/schemas/Event"
1914
+
required: [event]
1915
+
oneOf:
1916
+
- properties:
1917
+
event:
1918
+
const: addString
1919
+
- properties:
1920
+
event:
1921
+
const: addInt64
1922
+
data:
1923
+
$comment: |
1924
+
Since the data field is a string,
1925
+
we need a format to signal that it
1926
+
should be handled as a 64-bit integer.
1927
+
format: int64
1928
+
- properties:
1929
+
event:
1930
+
const: addJson
1931
+
data:
1932
+
$comment: |
1933
+
These content fields indicate
1934
+
that the string value should
1935
+
be parsed and validated as a
1936
+
JSON document (since JSON is not
1937
+
a binary format, contentEncoding
1938
+
is not needed)
1939
+
contentMediaType: application/json
1940
+
contentSchema:
1941
+
type: object
1942
+
required: [foo]
1943
+
properties:
1944
+
foo:
1945
+
type: integer
1946
+
```
1947
+
1948
+
The following `text/event-stream` document is an example of a valid request body for the above example:
1949
+
1950
+
```EVENTSTREAM
1951
+
event: addString
1952
+
data: This data is formatted
1953
+
data: across two lines
1954
+
retry: 5
1955
+
1956
+
event: addInt64
1957
+
data: 1234.5678
1958
+
unknownField: this is ignored
1959
+
1960
+
: This is a comment
1961
+
event: addJSON
1962
+
data: {"foo": 42}
1963
+
```
1964
+
1965
+
To more clearly see how this stream is handled, the following is the equivalent JSON Lines document, which shows how the numeric and JSON data are handled as strings, and how unknown fields and comments are ignored and not passed to schema validation:
1966
+
1967
+
```JSONL
1968
+
{"event": "addString", "data": "This data is formatted\nacross two lines", "retry": 5}
1969
+
{"event": "addInt64", "data": "1234.5678"}
1970
+
{"event": "addJSON", "data": "{\"foo\": 42}"}
1971
+
```
1972
+
1707
1973
##### Considerations for File Uploads
1708
1974
1709
1975
In contrast to OpenAPI 2.0, `file` input/output content in OAS 3.x is described with the same semantics as any other schema type.
0 commit comments