Skip to content

Commit 4365723

Browse files
authored
Merge pull request #3770 from handrews/binary-320
2 parents a723ddd + 701f93f commit 4365723

File tree

1 file changed

+62
-16
lines changed

1 file changed

+62
-16
lines changed

versions/3.2.0.md

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,42 @@ The formats defined by the OAS are:
160160
`number` | `double` | |
161161
`string` | `password` | A hint to UIs to obscure input.
162162

163+
#### <a name="binaryData"></a>Working With Binary Data
164+
165+
The OAS can describe either _raw_ or _encoded_ binary data.
166+
167+
* **raw binary** is used where unencoded binary data is allowed, such as when sending a binary payload as the entire HTTP message body, or as part of a `multipart/*` payload that allows binary parts
168+
* **encoded binary** is used where binary data is embedded in a text-only format such as `application/json` or `application/x-www-form-urlencoded` (either as a message body or in the URL query string).
169+
170+
In the following table showing how to use Schema Object keywords for binary data, we use `image/png` as an example binary media type. Any binary media type, including `application/octet-stream`, is sufficient to indicate binary content.
171+
172+
Keyword | Raw | Encoded | Comments
173+
------- | --- | ------- | --------
174+
`type` | _omit_ | `string` | raw binary is [outside of `type`](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-00#section-4.2.3)
175+
`contentMediaType` | `image/png` | `image/png` | can sometimes be omitted if redundant (see below)
176+
`contentEncoding` | _omit_ | `base64`&nbsp;or&nbsp;`base64url` | other encodings are [allowed](https://datatracker.ietf.org/doc/html/draft-bhutton-json-schema-validation-00#section-8.3)
177+
178+
Note that the encoding indicated by `contentEncoding`, which inflates the size of data in order to represent it as 7-bit ASCII text, is unrelated to HTTP's `Content-Encoding` header, which indicates whether and how a message body has been compressed and is applied after all content serialization described in this section has occurred. Since HTTP allows unencoded binary message bodies, there is no standardized HTTP header for indicating base64 or similar encoding of an entire message body.
179+
180+
Using a `contentEncoding` of `base64url` ensures that URL encoding (as required in the query string and in message bodies of type `application/x-www-form-urlencoded`) does not need to further encode any part of the already-encoded binary data.
181+
182+
The `contentMediaType` keyword is redundant if the media type is already set:
183+
184+
* as the key for a [`MediaType Object`](#mediaTypeObject)
185+
* in the `contentType` field of an [`Encoding Object`](#encodingObject)
186+
187+
If the 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.
188+
189+
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.
190+
191+
##### Migrating binary descriptions from OAS 3.0
192+
The following table shows how to migrate from OAS 3.0 binary data descriptions, continuing to use `image/png` as the example binary media type:
193+
194+
OAS < 3.1 | OAS 3.1+ | Comments
195+
--------- | -------- | --------
196+
`type: string`<br />`format: binary` | `contentMediaType: image/png` | if redundant, can be omitted, often resulting in an empty Schema Object
197+
`type: string`<br />`format: byte` | `type: string`<br />`contentMediaType: image/png`<br />`contentEncoding: base64` | note that `base64url` can be used to avoid re-encoding the base64 string to be URL-safe
198+
163199
### <a name="richText"></a>Rich Text Formatting
164200
Throughout the specification `description` fields are noted as supporting CommonMark markdown formatting.
165201
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.
@@ -1448,9 +1484,7 @@ application/json:
14481484

14491485
In contrast with the 2.0 specification, `file` input/output content in OpenAPI is described with the same semantics as any other schema type.
14501486

1451-
In contrast with the 3.0 specification, the `format` keyword has no effect on the content-encoding of the schema. JSON Schema offers a `contentEncoding` keyword, which may be used to specify the `Content-Encoding` for the schema. The `contentEncoding` 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). The encoding specified by the `contentEncoding` keyword is independent of an encoding specified by the `Content-Type` header in the request or response or metadata of a multipart body -- when both are present, the encoding specified in the `contentEncoding` is applied first and then the encoding specified in the `Content-Type` header.
1452-
1453-
JSON Schema also offers a `contentMediaType` keyword. However, when the media type is already specified by the Media Type Object's key, or by the `contentType` field of an [Encoding Object](#encodingObject), the `contentMediaType` keyword SHALL be ignored if present.
1487+
In contrast with the 3.0 specification, the `format` keyword has no effect on the content-encoding of the schema. Instead, JSON Schema's `contentEncoding` and `contentMediaType` keywords are used. See [Working With Binary Data](#binaryData) for how to model various scenarios with these keywords, and how to migrate from the previous `format` usage.
14541488

14551489
Examples:
14561490

@@ -1468,19 +1502,6 @@ content:
14681502
application/octet-stream: {}
14691503
```
14701504

1471-
Binary content transferred with base64 encoding:
1472-
1473-
```yaml
1474-
content:
1475-
image/png:
1476-
schema:
1477-
type: string
1478-
contentMediaType: image/png
1479-
contentEncoding: base64
1480-
```
1481-
1482-
Note that the `Content-Type` remains `image/png`, describing the semantics of the payload. The JSON Schema `type` and `contentEncoding` fields explain that the payload is transferred as text. The JSON Schema `contentMediaType` is technically redundant, but can be used by JSON Schema tools that may not be aware of the OpenAPI context.
1483-
14841505
These examples apply to either input payloads of file uploads or response payloads.
14851506

14861507
A `requestBody` for submitting a file in a `POST` operation may look like the following example:
@@ -1557,6 +1578,8 @@ When passing in `multipart` types, boundaries MAY be used to separate sections o
15571578

15581579
Per the JSON Schema specification, `contentMediaType` without `contentEncoding` present is treated as if `contentEncoding: identity` were present. While useful for embedding text documents such as `text/html` into JSON strings, it is not useful for a `multipart/form-data` part, as it just causes the document to be treated as `text/plain` instead of its actual media type. Use the Encoding Object without `contentMediaType` if no `contentEncoding` is required.
15591580

1581+
Note that only `multipart/*` media types with named parts can be described as shown here. Note also that while `multipart/form-data` originally defined a per-part `Content-Transfer-Encoding` header that could indicate base64 encoding (`contentEncoding: base64`), it has been deprecated for use with HTTP as of [RFC7578](https://www.rfc-editor.org/rfc/rfc7578#section-4.7).
1582+
15601583
Examples:
15611584

15621585
```yaml
@@ -1610,6 +1633,8 @@ This object MAY be extended with [Specification Extensions](#specificationExtens
16101633

16111634
##### Encoding Object Example
16121635

1636+
`multipart/form-data` allows for binary parts:
1637+
16131638
```yaml
16141639
requestBody:
16151640
content:
@@ -1645,6 +1670,27 @@ requestBody:
16451670
type: integer
16461671
```
16471672

1673+
`application/x-www-form-urlencoded` is a text format, which requires base64-encoding any binary data:
1674+
1675+
```YAML
1676+
requestBody:
1677+
content:
1678+
application/x-www-form-urlencoded:
1679+
schema:
1680+
type: object
1681+
properties:
1682+
name:
1683+
type: string
1684+
icon:
1685+
# default for type string is text/plain, need to declare
1686+
# the appropriate contentType in the Encoding Object
1687+
type: string
1688+
contentEncoding: base64url
1689+
encoding:
1690+
icon:
1691+
contentType: image/png, image/jpeg
1692+
```
1693+
16481694
#### <a name="responsesObject"></a>Responses Object
16491695

16501696
A container for the expected responses of an operation.

0 commit comments

Comments
 (0)