Skip to content

Commit 5055496

Browse files
committed
Allow Media Type and Encoding re-use
Add the Media Type Object and Encoding Object to the Components Object, and allow a Reference Object anywhere they are allowed. To ensure that re-usable Objects can be documented clearly, add a `description` field to both Objects.
1 parent 4972cc0 commit 5055496

File tree

3 files changed

+67
-13
lines changed

3 files changed

+67
-13
lines changed

src/oas.md

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,8 @@ All objects defined within the Components Object will have no effect on the API
627627
| <a name="components-links"></a> links | Map[`string`, [Link Object](#link-object) \| [Reference Object](#reference-object)] | An object to hold reusable [Link Objects](#link-object). |
628628
| <a name="components-callbacks"></a> callbacks | Map[`string`, [Callback Object](#callback-object) \| [Reference Object](#reference-object)] | An object to hold reusable [Callback Objects](#callback-object). |
629629
| <a name="components-path-items"></a> pathItems | Map[`string`, [Path Item Object](#path-item-object)] | An object to hold reusable [Path Item Objects](#path-item-object). |
630+
| <a name="components-media-types"></a> mediaTypes | Map[`string`, [Media Type Objects](#media-type-object) \| [Reference Object](#reference-object)] | An object to hold reusable [Media Type Objects](#media-type-object). |
631+
| <a name="components-encodings"></a> encodings | Map[`string`, [Encoding Objects](#encoding-object) \| [Reference Object](#reference-object)] | An object to hold reusable [Encoding Objects](#encoding-object). |
630632

631633
This object MAY be extended with [Specification Extensions](#specification-extensions).
632634

@@ -1008,7 +1010,7 @@ For use with `in: "querystring"` and `application/x-www-form-urlencoded`, see [E
10081010

10091011
| Field Name | Type | Description |
10101012
| ---- | :----: | ---- |
1011-
| <a name="parameter-content"></a>content | Map[`string`, [Media Type Object](#media-type-object)] | A map containing the representations for the parameter. The key is the media type and the value describes it. The map MUST only contain one entry. |
1013+
| <a name="parameter-content"></a>content | Map[`string`, [Media Type Object](#media-type-object) \| [Reference Object](#reference-object)] | A map containing the representations for the parameter. The key is the media type and the value describes it. The map MUST only contain one entry. |
10121014

10131015
##### Style Values
10141016

@@ -1198,7 +1200,7 @@ Describes a single request body.
11981200
| Field Name | Type | Description |
11991201
| ---- | :----: | ---- |
12001202
| <a name="request-body-description"></a>description | `string` | A brief description of the request body. This could contain examples of use. [CommonMark syntax](https://spec.commonmark.org/) MAY be used for rich text representation. |
1201-
| <a name="request-body-content"></a>content | Map[`string`, [Media Type Object](#media-type-object)] | **REQUIRED**. The content of the request body. The key is a media type or [media type range](https://www.rfc-editor.org/rfc/rfc9110.html#appendix-A) and the value describes it. The map SHOULD have at least one entry; if it does not, the behavior is implementation-defined. For requests that match multiple keys, only the most specific key is applicable. e.g. `"text/plain"` overrides `"text/*"` |
1203+
| <a name="request-body-content"></a>content | Map[`string`, [Media Type Object](#media-type-object) \| [Reference Object](#reference-object)] | **REQUIRED**. The content of the request body. The key is a media type or [media type range](https://www.rfc-editor.org/rfc/rfc9110.html#appendix-A) and the value describes it. The map SHOULD have at least one entry; if it does not, the behavior is implementation-defined. For requests that match multiple keys, only the most specific key is applicable. e.g. `"text/plain"` overrides `"text/*"` |
12021204
| <a name="request-body-required"></a>required | `boolean` | Determines if the request body is required in the request. Defaults to `false`. |
12031205

12041206
This object MAY be extended with [Specification Extensions](#specification-extensions).
@@ -1249,11 +1251,12 @@ See [Working With Examples](#working-with-examples) for further guidance regardi
12491251

12501252
| Field Name | Type | Description |
12511253
| ---- | :----: | ---- |
1254+
| <a name="media-type-description"></a>description | `string` | A brief description of the media type. [CommonMark syntax](https://spec.commonmark.org/) MAY be used for rich text representation. |
12521255
| <a name="media-type-schema"></a>schema | [Schema Object](#schema-object) | A schema describing the complete content of the request, response, parameter, or header. |
12531256
| <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). |
12541257
| <a name="media-type-example"></a>example | Any | Example of the media type; see [Working With Examples](#working-with-examples). |
12551258
| <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). |
1256-
| <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. |
1259+
| <a name="media-type-encoding"></a>encoding | Map[`string`, [Encoding Object](#encoding-object) \| [Reference Object](#reference-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. |
12571260

12581261
This object MAY be extended with [Specification Extensions](#specification-extensions).
12591262

@@ -1639,6 +1642,7 @@ These fields MAY be used either with or without the RFC6570-style serialization
16391642

16401643
| Field Name | Type | Description |
16411644
| ---- | :----: | ---- |
1645+
| <a name="encoding-description"></a>description | `string` | A brief description of the media type. [CommonMark syntax](https://spec.commonmark.org/) MAY be used for rich text representation. |
16421646
| <a name="encoding-content-type"></a>contentType | `string` | The `Content-Type` for encoding a specific property. The value is a comma-separated list, each element of which is either a specific media type (e.g. `image/png`) or a wildcard media type (e.g. `image/*`). Default value depends on the property type as shown in the table below. |
16431647
| <a name="encoding-headers"></a>headers | Map[`string`, [Header Object](#header-object) \| [Reference Object](#reference-object)] | A map allowing additional information to be provided as headers. `Content-Type` is described separately and SHALL be ignored in this section. This field SHALL be ignored if the media type is not a `multipart`. |
16441648

@@ -1929,7 +1933,7 @@ Describes a single response from an API operation, including design-time, static
19291933
| <a name="response-summary"></a>summary | `string` | A short summary of the meaning of the response. |
19301934
| <a name="response-description"></a>description | `string` | A description of the response. [CommonMark syntax](https://spec.commonmark.org/) MAY be used for rich text representation. |
19311935
| <a name="response-headers"></a>headers | Map[`string`, [Header Object](#header-object) \| [Reference Object](#reference-object)] | Maps a header name to its definition. [RFC9110](https://www.rfc-editor.org/rfc/rfc9110.html#section-5.1) states header names are case insensitive. If a response header is defined with the name `"Content-Type"`, it SHALL be ignored. |
1932-
| <a name="response-content"></a>content | Map[`string`, [Media Type Object](#media-type-object)] | A map containing descriptions of potential response payloads. The key is a media type or [media type range](https://www.rfc-editor.org/rfc/rfc9110.html#appendix-A) and the value describes it. For responses that match multiple keys, only the most specific key is applicable. e.g. `"text/plain"` overrides `"text/*"` |
1936+
| <a name="response-content"></a>content | Map[`string`, [Media Type Object](#media-type-object) \| [Reference Object](#reference-object)] | A map containing descriptions of potential response payloads. The key is a media type or [media type range](https://www.rfc-editor.org/rfc/rfc9110.html#appendix-A) and the value describes it. For responses that match multiple keys, only the most specific key is applicable. e.g. `"text/plain"` overrides `"text/*"` |
19331937
| <a name="response-links"></a>links | Map[`string`, [Link Object](#link-object) \| [Reference Object](#reference-object)] | A map of operations links that can be followed from the response. The key of the map is a short name for the link, following the naming constraints of the names for [Component Objects](#components-object). |
19341938

19351939
This object MAY be extended with [Specification Extensions](#specification-extensions).
@@ -2449,7 +2453,7 @@ Using `content` with a `text/plain` media type is RECOMMENDED for headers where
24492453

24502454
| Field Name | Type | Description |
24512455
| ---- | :----: | ---- |
2452-
| <a name="header-content"></a>content | Map[`string`, [Media Type Object](#media-type-object)] | A map containing the representations for the header. The key is the media type and the value describes it. The map MUST only contain one entry. |
2456+
| <a name="header-content"></a>content | Map[`string`, [Media Type Object](#media-type-object) \| [Reference Object](#reference-object)] | A map containing the representations for the header. The key is the media type and the value describes it. The map MUST only contain one entry. |
24532457

24542458
##### Header Object Example
24552459

src/schemas/validation/schema.yaml

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,16 @@ $defs:
199199
type: object
200200
additionalProperties:
201201
$ref: '#/$defs/path-item'
202+
mediaTypes:
203+
type: object
204+
additionalProperties:
205+
$ref: '#/$defs/media-type-or-reference'
206+
encodings:
207+
type: object
208+
additionalProperties:
209+
$ref: '#/$defs/encoding-or-reference'
202210
patternProperties:
203-
'^(schemas|responses|parameters|examples|requestBodies|headers|securitySchemes|links|callbacks|pathItems)$':
211+
'^(schemas|responses|parameters|examples|requestBodies|headers|securitySchemes|links|callbacks|pathItems|mediaTypes|encodings)$':
204212
$comment: Enumerating all of the property names in the regex above is necessary for unevaluatedProperties to work as expected
205213
propertyNames:
206214
pattern: '^[a-zA-Z0-9._-]+$'
@@ -517,31 +525,45 @@ $defs:
517525
$comment: https://spec.openapis.org/oas/v3.2#fixed-fields-10
518526
type: object
519527
additionalProperties:
520-
$ref: '#/$defs/media-type'
528+
$ref: '#/$defs/media-type-or-reference'
521529
propertyNames:
522530
format: media-range
523531

524532
media-type:
525533
$comment: https://spec.openapis.org/oas/v3.2#media-type-object
526534
type: object
527535
properties:
536+
description:
537+
type: string
528538
schema:
529539
$dynamicRef: '#meta'
530540
itemSchema:
531541
$dynamicRef: '#meta'
532542
encoding:
533543
type: object
534544
additionalProperties:
535-
$ref: '#/$defs/encoding'
545+
$ref: '#/$defs/encoding-or-reference'
536546
allOf:
537547
- $ref: '#/$defs/specification-extensions'
538548
- $ref: '#/$defs/examples'
539549
unevaluatedProperties: false
540550

551+
media-type-or-reference:
552+
if:
553+
type: object
554+
required:
555+
- $ref
556+
then:
557+
$ref: '#/$defs/reference'
558+
else:
559+
$ref: '#/$defs/media-type'
560+
541561
encoding:
542562
$comment: https://spec.openapis.org/oas/v3.2#encoding-object
543563
type: object
544564
properties:
565+
description:
566+
type: string
545567
contentType:
546568
type: string
547569
format: media-range
@@ -566,6 +588,16 @@ $defs:
566588
- $ref: '#/$defs/styles-for-form'
567589
unevaluatedProperties: false
568590

591+
encoding-or-reference:
592+
if:
593+
type: object
594+
required:
595+
- $ref
596+
then:
597+
$ref: '#/$defs/reference'
598+
else:
599+
$ref: '#/$defs/encoding'
600+
569601
responses:
570602
$comment: https://spec.openapis.org/oas/v3.2#responses-object
571603
type: object

tests/schema/pass/media-type-examples.yaml

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@ openapi: 3.2.0
33
info:
44
title: API
55
version: 1.0.0
6+
components:
7+
mediaTypes:
8+
StreamingPets:
9+
description: |
10+
Streaming sequence of JSON pet representations,
11+
suitable for use with any of the streaming JSON
12+
media types.
13+
itemSchema:
14+
$ref: '#components/schemas/Pet'
15+
encodings:
16+
ReservedNoExplode:
17+
description: |
18+
This allows multiple values under the same name,
19+
and disables reserved percent-encoding.
20+
Clients are responsible for adequately percent-encoding
21+
the values in advance.
22+
style: form
23+
explode: false
24+
allowReserved: true
625
paths:
726
/something:
827
put:
@@ -31,8 +50,9 @@ paths:
3150
frog:
3251
$ref: '#/components/examples/frog-example'
3352
application/jsonl:
34-
itemSchema:
35-
$ref: '#components/schemas/Pet'
53+
$ref: '#/components/mediaTypes/StreamingPets'
54+
application/x-ndjson:
55+
$ref: '#/components/mediaTypes/StreamingPets'
3656
application/xml:
3757
schema:
3858
type: object
@@ -132,9 +152,7 @@ paths:
132152
schema:
133153
type: integer
134154
forCoverage:
135-
style: form
136-
explode: false
137-
allowReserved: true
155+
$ref: '#/components/encodings/ReservedNoExplode'
138156
forCoverage2:
139157
style: spaceDelimited
140158
explode: true

0 commit comments

Comments
 (0)