Skip to content

Commit 69662e4

Browse files
Update "format" and "content*" for new JSON Schema (#2200)
* Update "format" and "content*" for new JSON Schema This removes OAS formats and examples that are now superfluous as they are part of the 2019-09 JSON Schema draft. Similarly it deprecates the "byte" and "binary" formats in favor of JSON Schema's "contentEncoding" and "contentMediaType" keywords, and updates various related exapmles and other guidance. It also removes confusingly blank rows in the OAS format table. * "format" is an annotation * Fix broken table, type, in Encoding Object Broke some things while updating for "content*" * Fix format of `format` Backticks, not double quotes. * Remove unneeded detail on "format" This was just duplicating info from the JSON Schema spec. Co-authored-by: Darrel <[email protected]> * Remove "byte" and "binary" formats altogether. Instead of just deprecating. The "content*" keywords now cover these use cases. * Harmonize JSON Schema content* + Media Type Object Includes harmonizing with the Encoding Object. In general, OpenAPI objects set the media type, although there is a case for `contentMediaType` with multipart/form-data. Otherwise, `contentEncoding` replaces the now-removed custom formats. A possibly controversial change is to indicate unencoded binary data by omitting `type` (or omitting the schema altogether), as binary data does not conform to JSON string requirements. This could still be done with `type: string` if that is preferred. It's going to be a bit weird either way. I can add wording in the next JSON Schema draft to clarify whichever approach makes more sense. * Fix typos from review * Remove stray {} * Fix inconsistencies contentMediaType and Encoding Object Co-authored-by: Darrel <[email protected]>
1 parent ece5497 commit 69662e4

File tree

1 file changed

+42
-43
lines changed

1 file changed

+42
-43
lines changed

versions/3.1.0.md

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,14 @@ Primitive data types in the OAS are based on the types supported by the [JSON Sc
147147
Note that `integer` as a type is also supported and is defined as a JSON number without a fraction or exponent part.
148148
Models are defined using the [Schema Object](#schemaObject), which is a superset of JSON Schema Specification Draft 2019-09.
149149

150-
<a name="dataTypeFormat"></a>Primitives have an optional modifier property: `format`.
151-
OAS uses several known formats to define in fine detail the data type being used.
150+
<a name="dataTypeFormat"></a>Primitives have an optional modifier property: `format`, which is defined by JSON Schema.
151+
OAS uses several known additional formats to define in fine detail the data type being used.
152152
However, to support documentation needs, the `format` property is an open `string`-valued property, and can have any value.
153-
Formats such as `"email"`, `"uuid"`, and so on, MAY be used even though undefined by this specification.
153+
Additional formats MAY be used even though undefined by either JSON Schema or this specification.
154154
Types that are not accompanied by a `format` property follow the type definition in the JSON Schema. Tools that do not recognize a specific `format` MAY default back to the `type` alone, as if the `format` is not specified.
155155

156+
Note that by default, JSON Schema validators will not attempt to validate the `format` keyword. https://json-schema.org/draft/2019-09/release-notes.html#format-vocabulary
157+
156158
The formats defined by the OAS are:
157159

158160
[`type`](#dataTypes) | [`format`](#dataTypeFormat) | Comments
@@ -161,15 +163,8 @@ The formats defined by the OAS are:
161163
`integer` | `int64` | signed 64 bits (a.k.a long)
162164
`number` | `float` | |
163165
`number` | `double` | |
164-
`string` | | |
165-
`string` | `byte` | base64 encoded characters
166-
`string` | `binary` | any sequence of octets
167-
`boolean` | | |
168-
`string` | `date` | As defined by `full-date` - [RFC3339](https://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14)
169-
`string` | `date-time` | As defined by `date-time` - [RFC3339](https://xml2rfc.ietf.org/public/rfc/html/rfc3339.html#anchor14)
170166
`string` | `password` | A hint to UIs to obscure input.
171167

172-
173168
### <a name="richText"></a>Rich Text Formatting
174169
Throughout the specification `description` fields are noted as supporting CommonMark markdown formatting.
175170
Where OpenAPI tooling renders rich text it MUST support, at a minimum, markdown syntax as described by [CommonMark 0.27](https://spec.commonmark.org/0.27/). Tooling MAY choose to ignore some CommonMark features to address security concerns.
@@ -1447,20 +1442,35 @@ application/json:
14471442

14481443
##### Considerations for File Uploads
14491444

1450-
In contrast with the 2.0 specification, `file` input/output content in OpenAPI is described with the same semantics as any other schema type. Specifically:
1445+
In contrast with the 2.0 specification, `file` input/output content in OpenAPI is described with the same semantics as any other schema type. In contrast with the 3.0 specification, such schemas use the `contentEncoding` JSON Schema keyword rather than the `format` keyword. This keyword supports all encodings defined in [RFC4648](https://tools.ietf.org/html/rfc4648), including "base64" and "base64url", as well as "quoted-printable" from [RFC2045](https://tools.ietf.org/html/rfc2045#section-6.7).
1446+
1447+
JSON Schema also offers a `contentMediaType` keyword. However, when the media type is already specified by the
1448+
Media Type Object's key, or by the `contentType` field of an [Encoding Object](#encodingObject), the `contentMediaType` keyword SHALL be ignored if present.
1449+
1450+
Examples:
1451+
1452+
Content transferred in binary (octet-stream) SHOULD omit `schema`, as no JSON Schema type is suitable:
14511453

14521454
```yaml
1453-
# content transferred with base64 encoding
1454-
schema:
1455-
type: string
1456-
format: base64
1455+
# a PNG image as a binary file:
1456+
content:
1457+
image/png: {}
14571458
```
14581459

14591460
```yaml
1460-
# content transferred in binary (octet-stream):
1461-
schema:
1462-
type: string
1463-
format: binary
1461+
# an arbitrary binary file:
1462+
content:
1463+
application/octet-stream: {}
1464+
```
1465+
1466+
Binary content transferred with base64 encoding:
1467+
1468+
```yaml
1469+
content:
1470+
image/png:
1471+
schema:
1472+
type: string
1473+
contentEncoding: base64
14641474
```
14651475

14661476
These examples apply to either input payloads of file uploads or response payloads.
@@ -1470,11 +1480,7 @@ A `requestBody` for submitting a file in a `POST` operation may look like the fo
14701480
```yaml
14711481
requestBody:
14721482
content:
1473-
application/octet-stream:
1474-
schema:
1475-
# a binary file of any type
1476-
type: string
1477-
format: binary
1483+
application/octet-stream: {}
14781484
```
14791485

14801486
In addition, specific media types MAY be specified:
@@ -1484,14 +1490,8 @@ In addition, specific media types MAY be specified:
14841490
requestBody:
14851491
content:
14861492
# a binary file of type png or jpeg
1487-
'image/jpeg':
1488-
schema:
1489-
type: string
1490-
format: binary
1491-
'image/png':
1492-
schema:
1493-
type: string
1494-
format: binary
1493+
image/jpeg: {}
1494+
image/png: {}
14951495
```
14961496

14971497
To upload multiple files, a `multipart` media type MUST be used:
@@ -1506,9 +1506,7 @@ requestBody:
15061506
file:
15071507
type: array
15081508
items:
1509-
type: string
1510-
format: binary
1511-
1509+
contentMediaType: application/octet-stream
15121510
```
15131511

15141512
##### Support for x-www-form-urlencoded Request Bodies
@@ -1546,8 +1544,10 @@ When passing in `multipart` types, boundaries MAY be used to separate sections o
15461544

15471545
* If the property is a primitive, or an array of primitive values, the default Content-Type is `text/plain`
15481546
* If the property is complex, or an array of complex values, the default Content-Type is `application/json`
1549-
* If the property is a `type: string` with `format: binary` or `format: base64` (aka a file object), the default Content-Type is `application/octet-stream`
1547+
* If the property is a `type: string` with a `contentEncoding`, the default Content-Type is `application/octet-stream`
1548+
* If the JSON Schema keyword `contentMediaType` is used and no Encoding Object is present, then the Content-Type is that which is specified by `contentMediaType`, however if an Encoding Object is present, then `contentMediaType` SHALL be ignored
15501549

1550+
As with non-multipart request or response bodies, when using `contentMediaType` to specify a binary Content-Type without also using `contentEncoding`, the JSON Schema `type` keyword is omitted.
15511551

15521552
Examples:
15531553

@@ -1566,9 +1566,8 @@ requestBody:
15661566
type: object
15671567
properties: {}
15681568
profileImage:
1569-
# default Content-Type for string/binary is `application/octet-stream`
1570-
type: string
1571-
format: binary
1569+
# Content-Type with contentMediaType is the contentMediaType (image/png here)
1570+
contentMediaType: image/png
15721571
children:
15731572
# default Content-Type for arrays is based on the `inner` type (text/plain here)
15741573
type: array
@@ -1578,7 +1577,8 @@ requestBody:
15781577
# default Content-Type for arrays is based on the `inner` type (object shown, so `application/json` in this example)
15791578
type: array
15801579
items:
1581-
type: '#/components/schemas/Address'
1580+
type: object
1581+
$ref: '#/components/schemas/Address'
15821582
```
15831583
15841584
An `encoding` attribute is introduced to give you control over the serialization of parts of `multipart` request bodies. This attribute is _only_ applicable to `multipart` and `application/x-www-form-urlencoded` request bodies.
@@ -1590,7 +1590,7 @@ A single encoding definition applied to a single schema property.
15901590
##### Fixed Fields
15911591
Field Name | Type | Description
15921592
---|:---:|---
1593-
<a name="encodingContentType"></a>contentType | `string` | The Content-Type for encoding a specific property. Default value depends on the property type: for `string` with `format` being `binary` – `application/octet-stream`; for other primitive types – `text/plain`; for `object` - `application/json`; for `array` – the default is defined based on the inner type. The value can be a specific media type (e.g. `application/json`), a wildcard media type (e.g. `image/*`), or a comma-separated list of the two types.
1593+
<a name="encodingContentType"></a>contentType | `string` | The Content-Type for encoding a specific property. Default value depends on the property type: when `type` is absent and `contentMediaType` is present - the value of `contentMediaType`; when both `type` and `contentMediaType` are absent - `application/octet-stream`; for `string` with a `contentEncoding` - `application/octet-string`; for other primitive types – `text/plain`; for `object` - `application/json`; for `array` – the default is defined based on the inner type. The value can be a specific media type (e.g. `application/json`), a wildcard media type (e.g. `image/*`), or a comma-separated list of the two types.
15941594
<a name="encodingHeaders"></a>headers | Map[`string`, [Header Object](#headerObject) \| [Reference Object](#referenceObject)] | A map allowing additional information to be provided as headers, for example `Content-Disposition`. `Content-Type` is described separately and SHALL be ignored in this section. This property SHALL be ignored if the request body media type is not a `multipart`.
15951595
<a name="encodingStyle"></a>style | `string` | Describes how a specific property value will be serialized depending on its type. See [Parameter Object](#parameterObject) for details on the [`style`](#parameterStyle) property. The behavior follows the same values as `query` parameters, including default values. This property SHALL be ignored if the request body media type is not `application/x-www-form-urlencoded` or `multipart/form-data`. If a value is explicitly defined, then the value of [`contentType`](#encodingContentType) (implicit or explicit) SHALL be ignored.
15961596
<a name="encodingExplode"></a>explode | `boolean` | When this is true, property values of type `array` or `object` generate separate parameters for each value of the array, or key-value-pair of the map. For other types of properties this property has no effect. When [`style`](#encodingStyle) is `form`, the default value is `true`. For all other styles, the default value is `false`. This property SHALL be ignored if the request body media type is not `application/x-www-form-urlencoded` or `multipart/form-data`. If a value is explicitly defined, then the value of [`contentType`](#encodingContentType) (implicit or explicit) SHALL be ignored.
@@ -1621,9 +1621,8 @@ requestBody:
16211621
type: object
16221622
properties: {}
16231623
profileImage:
1624-
# default is application/octet-stream, need to declare an image type only!
16251624
type: string
1626-
format: binary
1625+
contentMediaType: image/jpeg
16271626
encoding:
16281627
historyMetadata:
16291628
# require XML Content-Type in utf-8 encoding

0 commit comments

Comments
 (0)