Skip to content

Commit d3e0238

Browse files
authored
Consolidate guidance around examples (3.0.4) (#3893)
* Consolidate guidance around examples The three (four in 3.1) ways of specifying parameter or media type examples are confusing. There is a lot of guidance that is repeated, making it harder to see the essentials. Some of that guidance is also contradictory, with most saying that the examples SHOULD match various conditions, but one saying that they MUST. Research shows that SHOULD was repeatedly advocated, and instances of MUST corrected back to SHOULD, except for one final commit where MUST was introduced, and the subsequent commit to revert to SHOULD seems to have simply missed one instance. So this change takes the position that the MUST was an error and weakens it to SHOULD to match the other four or five places where the requirement was specified. * Fix typo in examples changes
1 parent 82fe810 commit d3e0238

File tree

1 file changed

+105
-8
lines changed

1 file changed

+105
-8
lines changed

versions/3.0.4.md

Lines changed: 105 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,16 +1066,17 @@ Field Name | Type | Description
10661066
###### Fixed Fields for use with `schema`
10671067

10681068
For simpler scenarios, a [`schema`](#parameterSchema) and [`style`](#parameterStyle) can describe the structure and syntax of the parameter.
1069-
When `example` or `examples` are provided in conjunction with the `schema` object, the example MUST follow the prescribed serialization strategy for the parameter.
1069+
When `example` or `examples` are provided in conjunction with the `schema` object, the example SHOULD match the specified schema and follow the prescribed serialization strategy for the parameter.
1070+
The `example` and `examples` fields are mutually exclusive, and if either is present it SHALL _override_ any `example` in the schema.
10701071

10711072
Field Name | Type | Description
10721073
---|:---:|---
10731074
<a name="parameterStyle"></a>style | `string` | Describes how the parameter value will be serialized depending on the type of the parameter value. Default values (based on value of `in`): for `query` - `form`; for `path` - `simple`; for `header` - `simple`; for `cookie` - `form`.
10741075
<a name="parameterExplode"></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 property has no effect. When [`style`](#parameterStyle) is `form`, the default value is `true`. For all other styles, the default value is `false`.
10751076
<a name="parameterAllowReserved"></a>allowReserved | `boolean` | Determines whether the parameter value SHOULD allow reserved characters, as defined by [RFC3986](https://tools.ietf.org/html/rfc3986#section-2.2) `:/?#[]@!$&'()*+,;=` to be included without percent-encoding. This property only applies to parameters with an `in` value of `query`. The default value is `false`.
10761077
<a name="parameterSchema"></a>schema | [Schema Object](#schemaObject) \| [Reference Object](#referenceObject) | The schema defining the type used for the parameter.
1077-
<a name="parameterExample"></a>example | Any | Example of the parameter's potential value. The example SHOULD match the specified schema and encoding properties if present. The `example` field is mutually exclusive of the `examples` field. Furthermore, if referencing a `schema` that contains an example, the `example` value SHALL _override_ the example provided by the schema. To represent examples of media types that cannot naturally be represented in JSON or YAML, a string value can contain the example with escaping where necessary.
1078-
<a name="parameterExamples"></a>examples | Map[ `string`, [Example Object](#exampleObject) \| [Reference Object](#referenceObject)] | Examples of the parameter's potential value. Each example SHOULD contain a value in the correct format as specified in the parameter encoding. The `examples` field is mutually exclusive of the `example` field. Furthermore, if referencing a `schema` that contains an example, the `examples` value SHALL _override_ the example provided by the schema.
1078+
<a name="parameterExample"></a>example | Any | Example of the parameter's potential value; see [Working With Examples](#working-with-examples).
1079+
<a name="parameterExamples"></a>examples | Map[ `string`, [Example Object](#exampleObject) \| [Reference Object](#referenceObject)] | Examples of the parameter's potential value; see [Working With Examples](#working-with-examples).
10791080

10801081
###### Fixed Fields and considerations for use with `content`
10811082

@@ -1409,12 +1410,16 @@ content:
14091410
#### <a name="mediaTypeObject"></a>Media Type Object
14101411
Each Media Type Object provides schema and examples for the media type identified by its key.
14111412

1413+
When `example` or `examples` are provided, the example SHOULD match the specified schema and be in the correct format as specified by the media type and its encoding.
1414+
The `example` and `examples` fields are mutually exclusive, and if either is present it SHALL _override_ any `example` in the schema.
1415+
See [Working With Examples](#working-with-examples) for further guidance regarding the different ways of specifying examples, including non-JSON/YAML values.
1416+
14121417
##### Fixed Fields
14131418
Field Name | Type | Description
14141419
---|:---:|---
14151420
<a name="mediaTypeSchema"></a>schema | [Schema Object](#schemaObject) \| [Reference Object](#referenceObject) | The schema defining the content of the request, response, or parameter.
1416-
<a name="mediaTypeExample"></a>example | Any | Example of the media type. The example object SHOULD be in the correct format as specified by the media type. The `example` field is mutually exclusive of the `examples` field. Furthermore, if referencing a `schema` which contains an example, the `example` value SHALL _override_ the example provided by the schema.
1417-
<a name="mediaTypeExamples"></a>examples | Map[ `string`, [Example Object](#exampleObject) \| [Reference Object](#referenceObject)] | Examples of the media type. Each example object SHOULD match the media type and specified schema if present. The `examples` field is mutually exclusive of the `example` field. Furthermore, if referencing a `schema` which contains an example, the `examples` value SHALL _override_ the example provided by the schema.
1421+
<a name="mediaTypeExample"></a>example | Any | Example of the media type; see [Working With Examples](#working-with-examples).
1422+
<a name="mediaTypeExamples"></a>examples | Map[ `string`, [Example Object](#exampleObject) \| [Reference Object](#referenceObject)] | Examples of the media type; see [Working With Examples](#working-with-examples).
14181423
<a name="mediaTypeEncoding"></a>encoding | Map[`string`, [Encoding Object](#encodingObject)] | A map between a property name and its encoding information. The key, being the property name, MUST exist in the schema as a property. The encoding attribute SHALL only apply to [Request Body Objects](#requestBodyObject), and only 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.
14191424

14201425
This object MAY be extended with [Specification Extensions](#specificationExtensions).
@@ -1999,9 +2004,27 @@ Field Name | Type | Description
19992004

20002005
This object MAY be extended with [Specification Extensions](#specificationExtensions).
20012006

2002-
In all cases, the example value is expected to be compatible with the type schema
2003-
of its associated value. Tooling implementations MAY choose to
2004-
validate compatibility automatically, and reject the example value(s) if incompatible.
2007+
In all cases, the example value SHOULD be compatible with the schema of its associated value.
2008+
Tooling implementations MAY choose to validate compatibility automatically, and reject the example value(s) if incompatible.
2009+
2010+
#### Working With Examples
2011+
2012+
Example Objects can be used in both [Parameter Objects](#parameterObject) and [Media Type Objects](#mediaTypeObject).
2013+
In both Objects, this is done through the `examples` (plural) field.
2014+
However, there are two other ways to provide examples: The `example` (singular) field that is mutually exclusive with `examples` in both Objects, and the `example` (singular) field in the [Schema Object](#schemaObject) that appears in the `schema` field of both Objects.
2015+
Each of these fields has slightly different considerations.
2016+
2017+
The Schema Object's `example` field is used to show example values without regard to how they might be formatted as parameters or within media type representations.
2018+
2019+
The mutually exclusive fields in the Parameter or Media Type Objects are used to show example values which SHOULD both match the schema and be formatted as they would appear as a serialized parameter or within a media type representation.
2020+
The exact serialization and encoding is determined by various fields in the Parameter Object, or in the Media Type Object's [Encoding Object](#encodingObject).
2021+
Because examples using these fields represent the final serialized form of the data, they SHALL _override_ any `example` in the corresponding Schema Object.
2022+
2023+
The singular `example` field in the Parameter or Media Type object is concise and convenient for simple examples, but does not offer any other advantages over using Example Objects under `examples`.
2024+
2025+
Some examples cannot be represented directly in JSON or YAML.
2026+
For all three ways of providing examples, these can be shown as string values with any escaping necessary to make the string valid in the JSON or YAML format of the OpenAPI Description document.
2027+
With the Example Object, such values can alternatively be handled through the `externalValue` field.
20052028

20062029
##### Example Object Examples
20072030

@@ -2061,6 +2084,80 @@ responses:
20612084
$ref: '#/components/examples/confirmation-success'
20622085
```
20632086

2087+
Two different uses of JSON strings:
2088+
2089+
First, a request or response body that is just a JSON string (not an object containing a string):
2090+
2091+
```json
2092+
"application/json": {
2093+
"schema": {
2094+
"type": "string"
2095+
},
2096+
"examples": {
2097+
"jsonBody": {
2098+
"description": "A body of just the JSON string \"json\"",
2099+
"value": "json"
2100+
}
2101+
}
2102+
}
2103+
```
2104+
2105+
```yaml
2106+
application/json:
2107+
schema:
2108+
type: string
2109+
examples:
2110+
jsonBody:
2111+
description: 'A body of just the JSON string "json"'
2112+
value: json
2113+
```
2114+
2115+
In the above example, we can just show the JSON string (or any JSON value) as-is, rather than stuffing a serialized JSON value into a JSON string, which would have looked like `"\"json\""`.
2116+
2117+
2118+
In contrast, a JSON string encoded inside of a URL-style form body:
2119+
2120+
```json
2121+
"application/x-www-form-urlencoded": {
2122+
"schema": {
2123+
"type": "object",
2124+
"properties": {
2125+
"jsonValue": {
2126+
"type": "string"
2127+
}
2128+
}
2129+
},
2130+
"encoding": {
2131+
"jsonValue": {
2132+
"contentType": "application/json"
2133+
}
2134+
},
2135+
"examples": {
2136+
"jsonFormValue": {
2137+
"description": "The JSON string \"json\" as a form value",
2138+
"value": "jsonValue=%22json%22"
2139+
}
2140+
}
2141+
}
2142+
```
2143+
2144+
```yaml
2145+
application/x-www-form-urlencoded:
2146+
schema:
2147+
type: object
2148+
properties:
2149+
jsonValue:
2150+
type: string
2151+
encoding:
2152+
jsonValue:
2153+
contentType: application/json
2154+
examples:
2155+
jsonFormValue:
2156+
description: 'The JSON string "json" as a form value'
2157+
value: jsonValue=%22json%22
2158+
```
2159+
2160+
In this example, the JSON string had to be serialized before encoding it into the URL form value, so the example includes the quotation marks that are part of the JSON serialization, which are then URL percent-encoded.
20642161

20652162
#### <a name="linkObject"></a>Link Object
20662163

0 commit comments

Comments
 (0)