Skip to content

Commit e162406

Browse files
authored
Clarify the use of "binary" and "byte" formats (OAI#3187)
Add a brief explanation as a subsection of Data Types, and update the relevant examples. Specifically: * Don't use `format: byte` for a whole message body * Fix `multipart/mixed` example to be `multipart/form-data` (because `multipart/mixed` does not name its parts and therefore cannot be represented with an object schema) * Add an `application/x-www-form-urlencoded` example, which is where `format: byte` needs to be used
1 parent 9f6daf3 commit e162406

File tree

1 file changed

+33
-9
lines changed

1 file changed

+33
-9
lines changed

versions/3.0.4.md

+33-9
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,20 @@ The formats defined by the OAS are:
162162
`integer` | `int64` | signed 64 bits (a.k.a long)
163163
`number` | `float` | |
164164
`number` | `double` | |
165-
`string` | `byte` | base64 encoded characters
165+
`string` | `byte` | base64 encoded characters - [RFC4648](https://www.rfc-editor.org/rfc/rfc4648#section-4)
166166
`string` | `binary` | any sequence of octets
167167
`string` | `date` | As defined by `full-date` - [RFC3339](https://www.rfc-editor.org/rfc/rfc3339#section-5.6)
168168
`string` | `date-time` | As defined by `date-time` - [RFC3339](https://www.rfc-editor.org/rfc/rfc3339#section-5.6)
169169
`string` | `password` | A hint to UIs to obscure input.
170170

171+
#### <a name="binaryData"></a>Working With Binary Data
172+
173+
Two formats, `binary` and `byte`, describe different ways to work with binary data:
174+
175+
* `binary` is used where unencoded binary data is allowed, such as when sending a binary payload as an HTTP message body, or as part of a `multipart/*` payload that allows binary parts
176+
* `byte` is used where binary data is embedded in a text-only format such as `application/json` or `application/x-www-form-urlencoded`
177+
178+
Note that the encoding indicated by `byte`, 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.
171179

172180
### <a name="richText"></a>Rich Text Formatting
173181
Throughout the specification `description` fields are noted as supporting CommonMark markdown formatting.
@@ -1440,13 +1448,6 @@ application/json:
14401448

14411449
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:
14421450

1443-
```yaml
1444-
# content transferred with base64 encoding
1445-
schema:
1446-
type: string
1447-
format: byte
1448-
```
1449-
14501451
```yaml
14511452
# content transferred in binary (octet-stream):
14521453
schema:
@@ -1537,6 +1538,7 @@ When passing in `multipart` types, boundaries MAY be used to separate sections o
15371538
* If the property is complex, or an array of complex values, the default Content-Type is `application/json`
15381539
* If the property is a `type: string` with `format: binary` or `format: byte` (aka a file object), the default Content-Type is `application/octet-stream`
15391540

1541+
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 (`format: byte`), it has been deprecated for use with HTTP as of [RFC7578](https://www.rfc-editor.org/rfc/rfc7578#section-4.7).
15401542

15411543
Examples:
15421544

@@ -1589,10 +1591,12 @@ This object MAY be extended with [Specification Extensions](#specificationExtens
15891591

15901592
##### Encoding Object Example
15911593

1594+
`multipart/form-data` allows for binary parts:
1595+
15921596
```yaml
15931597
requestBody:
15941598
content:
1595-
multipart/mixed:
1599+
multipart/form-data:
15961600
schema:
15971601
type: object
15981602
properties:
@@ -1627,6 +1631,26 @@ requestBody:
16271631
type: integer
16281632
```
16291633

1634+
`application/x-www-form-urlencoded` is a text format, which requires base64-encoding any binary data:
1635+
1636+
```YAML
1637+
requestBody:
1638+
content:
1639+
application/x-www-form-urlencoded:
1640+
schema:
1641+
type: object
1642+
properties:
1643+
name:
1644+
type: string
1645+
icon:
1646+
# default is text/plain, need to declare an image type only!
1647+
type: string
1648+
format: byte
1649+
encoding:
1650+
icon:
1651+
contentType: image/png, image/jpeg
1652+
```
1653+
16301654
#### <a name="responsesObject"></a>Responses Object
16311655

16321656
A container for the expected responses of an operation.

0 commit comments

Comments
 (0)