Skip to content

Commit 67719d2

Browse files
committed
Allow Parameter/Header examples w/content
Parameter and Header serialization is complex, particularly when using the `content` field to use a Media Type Object. In such scenarios, the serialization occurs in two steps: The first step is to serialize the data to the media type, which can be captured by the `examples` field of the Media Type Object. The second is the encoding/escaping of the media type document for use in a URI, HTTP header, or other location with its own rules. Sometimes the part needing illustration with an example is at one level, sometimes at another, and sometimes it is helpful to show both. For simplicity, the "data" examples are always treated as the overall input data, so they would be the same at both levels. This is also because it is not always possible to show each step, particularly when there are binary serializations. This allows showing either step (or both steps) with both data and serialization, depending on what makes sense for the use case.
1 parent ec0271b commit 67719d2

File tree

2 files changed

+63
-15
lines changed

2 files changed

+63
-15
lines changed

src/oas.md

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,7 @@ See [Appendix B](#appendix-b-data-type-conversion) for a discussion of convertin
964964
###### Common Fixed Fields
965965

966966
These fields MAY be used with either `content` or `schema`.
967+
The `example` and `examples` fields are mutually exclusive, and if either is present it SHALL _override_ any `example` in the schema.
967968

968969
| Field Name | Type | Description |
969970
| ---- | :----: | ---- |
@@ -973,6 +974,8 @@ These fields MAY be used with either `content` or `schema`.
973974
| <a name="parameter-required"></a>required | `boolean` | Determines whether this parameter is mandatory. If the [parameter location](#parameter-in) is `"path"`, this field is **REQUIRED** and its value MUST be `true`. Otherwise, the field MAY be included and its default value is `false`. |
974975
| <a name="parameter-deprecated"></a> deprecated | `boolean` | Specifies that a parameter is deprecated and SHOULD be transitioned out of usage. Default value is `false`. |
975976
| <a name="parameter-allow-empty-value"></a> allowEmptyValue | `boolean` | If `true`, clients MAY pass a zero-length string value in place of parameters that would otherwise be omitted entirely, which the server SHOULD interpret as the parameter being unused. Default value is `false`. If [`style`](#parameter-style) is used, and if [behavior is _n/a_ (cannot be serialized)](#style-examples), the value of `allowEmptyValue` SHALL be ignored. Interactions between this field and the parameter's [Schema Object](#schema-object) are implementation-defined. This field is valid only for `query` parameters. <br><br>**Deprecated:** Use of this field is NOT RECOMMENDED, and it is likely to be removed in a later revision. |
977+
| <a name="parameter-example"></a>example | Any | Example of the parameter's potential value; see [Working With Examples](#working-with-examples). |
978+
| <a name="parameter-examples"></a>examples | Map[ `string`, [Example Object](#example-object) \| [Reference Object](#reference-object)] | Examples of the parameter's potential value; see [Working With Examples](#working-with-examples). |
976979

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

@@ -982,7 +985,6 @@ Note that while `"Cookie"` as a `name` is not forbidden if `in` is `"header"`, t
982985

983986
For simpler scenarios, a [`schema`](#parameter-schema) and [`style`](#parameter-style) can describe the structure and syntax of the parameter.
984987
When `example` or `examples` are provided in conjunction with the `schema` field, the example SHOULD match the specified schema and follow the prescribed serialization strategy for the parameter.
985-
The `example` and `examples` fields are mutually exclusive, and if either is present it SHALL _override_ any `example` in the schema.
986988

987989
These fields MUST NOT be used with `in: "querystring"`.
988990
@@ -994,8 +996,6 @@ Serializing with `schema` is NOT RECOMMENDED for `in: "cookie"` parameters, `in:
994996
| <a name="parameter-explode"></a>explode | `boolean` | When this is true, parameter 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 parameters this field has no effect. When [`style`](#parameter-style) is `"form"`, the default value is `true`. For all other styles, the default value is `false`. Note that despite `false` being the default for `deepObject`, the combination of `false` with `deepObject` is undefined. |
995997
| <a name="parameter-allow-reserved"></a>allowReserved | `boolean` | When this is true, parameter values are serialized using reserved expansion, as defined by [RFC6570](https://datatracker.ietf.org/doc/html/rfc6570#section-3.2.3), which allows [RFC3986's reserved character set](https://datatracker.ietf.org/doc/html/rfc3986#section-2.2), as well as percent-encoded triples, to pass through unchanged, while still percent-encoding all other disallowed characters (including `%` outside of percent-encoded triples). Applications are still responsible for percent-encoding reserved characters that are not allowed by the rules of the `in` destination or media type, or are [not allowed in the path by this specification](#path-templating); see Appendices [C](#appendix-c-using-rfc6570-based-serialization) and [E](#appendix-e-percent-encoding-and-form-media-types) for details. The default value is `false`. |
996998
| <a name="parameter-schema"></a>schema | [Schema Object](#schema-object) | The schema defining the type used for the parameter. |
997-
| <a name="parameter-example"></a>example | Any | Example of the parameter's potential value; see [Working With Examples](#working-with-examples). |
998-
| <a name="parameter-examples"></a>examples | Map[ `string`, [Example Object](#example-object) \| [Reference Object](#reference-object)] | Examples of the parameter's potential value; see [Working With Examples](#working-with-examples). |
999999

10001000
See also [Appendix C: Using RFC6570-Based Serialization](#appendix-c-using-rfc6570-based-serialization) for additional guidance.
10011001

@@ -1146,6 +1146,38 @@ content:
11461146
type: number
11471147
```
11481148

1149+
A querystring parameter using regular form encoding, but managed with a Media Type Object:
1150+
1151+
```yaml
1152+
in: querystring
1153+
content:
1154+
application/x-www-form-urlencoded:
1155+
schema:
1156+
type: object
1157+
properties:
1158+
foo:
1159+
type: string
1160+
bar:
1161+
type: boolean
1162+
examples:
1163+
spacesAndPluses:
1164+
description: Note handling of spaces and "+" per media type.
1165+
dataValue:
1166+
foo: a + b
1167+
bar: true
1168+
serializedValue:
1169+
foo=a+%2B+b&bar=true
1170+
examples:
1171+
spacesAndPluses:
1172+
description: |
1173+
Note that no additional percent encoding is done, as this
1174+
media type is URI query string-ready by definition.
1175+
dataValue:
1176+
foo: a + b
1177+
bar: true
1178+
serializedValue:
1179+
foo=a+%2B+b&bar=true
1180+
11491181
A querystring parameter that uses JSON for the entire string (not as a single query parameter value):
11501182
11511183
```yaml
@@ -1157,13 +1189,24 @@ content:
11571189
# Allow an arbitrary JSON object to keep
11581190
# the example simple
11591191
type: object
1160-
example: {
1192+
examples:
1193+
TwoNoFlag:
1194+
description: Serialize with minimized whitespace
1195+
dataValue: {
1196+
"numbers": [1, 2],
1197+
"flag": null
1198+
}
1199+
serializedValue: '{"numbers":[1,2],"flag":null}'
1200+
examples:
1201+
TwoNoFlag:
1202+
dataValue: {
11611203
"numbers": [1, 2],
11621204
"flag": null
11631205
}
1206+
serializedValue: %7B%22numbers%22%3A%5B1%2C2%5D%2C%22flag%22%3Anull%7D
11641207
```
11651208
1166-
Assuming a path of `/foo`, a server of `https://example.com`, the full URL incorporating the value from the `example` field (with whitespace minimized) would be:
1209+
Assuming a path of `/foo`, a server of `https://example.com`, the full URL incorporating the value serialized as shown by the Example Objects would be:
11671210

11681211
```uri
11691212
https://example.com/foo?%7B%22numbers%22%3A%5B1%2C2%5D%2C%22flag%22%3Anull%7D
@@ -1179,11 +1222,15 @@ content:
11791222
schema:
11801223
type: string
11811224
example: $.a.b[1:1]
1225+
examples:
1226+
Selector:
1227+
dataValue: $.a.b[1:1]
1228+
serializedValue: %24.a.b%5B1%3A1%5D
11821229
```
11831230

11841231
As there is not, as of this writing, a [registered](#media-type-registry) mapping between the JSON Schema data model and JSONPath, the details of the string's allowed structure would need to be conveyed either in a human-readable `description` field, or through a mechanism outside of the OpenAPI Description, such as a JSON Schema for the data structure to be queried.
11851232

1186-
Assuming a path of `/foo` and a server of `https://example.com`, the full URL incorporating the value from the `example` field would be:
1233+
Assuming a path of `/foo` and a server of `https://example.com`, the full URL incorporating the value from the Example Object would be:
11871234

11881235
```uri
11891236
https://example.com/foo?%24.a.b%5B1%3A1%5D
@@ -2411,11 +2458,16 @@ The Header Object follows the structure of the [Parameter Object](#parameter-obj
24112458

24122459
These fields MAY be used with either `content` or `schema`.
24132460

2461+
When `example` or `examples` are provided in conjunction with the `schema` field, the example SHOULD match the specified schema and follow the prescribed serialization strategy for the header.
2462+
The `example` and `examples` fields are mutually exclusive, and if either is present it SHALL _override_ any `example` in the schema.
2463+
24142464
| Field Name | Type | Description |
24152465
| ---- | :----: | ---- |
24162466
| <a name="header-description"></a>description | `string` | A brief description of the header. This could contain examples of use. [CommonMark syntax](https://spec.commonmark.org/) MAY be used for rich text representation. |
24172467
| <a name="header-required"></a>required | `boolean` | Determines whether this header is mandatory. The default value is `false`. |
24182468
| <a name="header-deprecated"></a> deprecated | `boolean` | Specifies that the header is deprecated and SHOULD be transitioned out of usage. Default value is `false`. |
2469+
| <a name="header-example"></a>example | Any | Example of the header's potential value; see [Working With Examples](#working-with-examples). |
2470+
| <a name="header-examples"></a>examples | Map[ `string`, [Example Object](#example-object) \| [Reference Object](#reference-object)] | Examples of the header's potential value; see [Working With Examples](#working-with-examples). |
24192471

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

@@ -2426,16 +2478,11 @@ When `example` or `examples` are provided in conjunction with the `schema` field
24262478

24272479
Serializing with `schema` is NOT RECOMMENDED for headers with parameters (name=value pairs following a `;`) in their values, or where values might have non-URL-safe characters; see [Appendix D](#appendix-d-serializing-headers-and-cookies) for details.
24282480

2429-
When `example` or `examples` are provided in conjunction with the `schema` field, the example SHOULD match the specified schema and follow the prescribed serialization strategy for the header.
2430-
The `example` and `examples` fields are mutually exclusive, and if either is present it SHALL _override_ any `example` in the schema.
2431-
24322481
| Field Name | Type | Description |
24332482
| ---- | :----: | ---- |
24342483
| <a name="header-style"></a>style | `string` | Describes how the header value will be serialized. The default (and only legal value for headers) is `"simple"`. |
24352484
| <a name="header-explode"></a>explode | `boolean` | When this is true, header values of type `array` or `object` generate a single header whose value is a comma-separated list of the array items or key-value pairs of the map, see [Style Examples](#style-examples). For other data types this field has no effect. The default value is `false`. |
24362485
| <a name="header-schema"></a>schema | [Schema Object](#schema-object) \| [Reference Object](#reference-object) | The schema defining the type used for the header. |
2437-
| <a name="header-example"></a>example | Any | Example of the header's potential value; see [Working With Examples](#working-with-examples). |
2438-
| <a name="header-examples"></a>examples | Map[ `string`, [Example Object](#example-object) \| [Reference Object](#reference-object)] | Examples of the header's potential value; see [Working With Examples](#working-with-examples). |
24392486

24402487
See also [Appendix C: Using RFC6570-Based Serialization](#appendix-c-using-rfc6570-based-serialization) for additional guidance.
24412488

src/schemas/validation/schema.yaml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,8 @@ $defs:
366366
- required:
367367
- content
368368
allOf:
369+
- $ref: '#/$defs/examples'
370+
- $ref: '#/$defs/specification-extensions'
369371
- if:
370372
properties:
371373
in:
@@ -403,7 +405,6 @@ $defs:
403405
default: false
404406
type: boolean
405407
allOf:
406-
- $ref: '#/$defs/examples'
407408
- $ref: '#/$defs/parameter/dependentSchemas/schema/$defs/styles-for-path'
408409
- $ref: '#/$defs/parameter/dependentSchemas/schema/$defs/styles-for-header'
409410
- $ref: '#/$defs/parameter/dependentSchemas/schema/$defs/styles-for-query'
@@ -474,7 +475,6 @@ $defs:
474475
default: form
475476
const: form
476477

477-
$ref: '#/$defs/specification-extensions'
478478
unevaluatedProperties: false
479479

480480
parameter-or-reference:
@@ -728,8 +728,9 @@ $defs:
728728
explode:
729729
default: false
730730
type: boolean
731-
$ref: '#/$defs/examples'
732-
$ref: '#/$defs/specification-extensions'
731+
allOf:
732+
- $ref: '#/$defs/examples'
733+
- $ref: '#/$defs/specification-extensions'
733734
unevaluatedProperties: false
734735

735736
header-or-reference:

0 commit comments

Comments
 (0)