Skip to content

Commit 6c3fa9f

Browse files
David BiesackDavid Biesack
David Biesack
authored and
David Biesack
committed
Update examplesObject
Clarify that Examples Object may be used instead of Example Object. Show JSON examples as well as YAML Clarify the $ref values used in examples to remove ambiguity. These are not references to external JSON/XML/TXT resources, but rather are references to reusable examples in the current OpenAPI definition or in an external OpenAPI definition's components/examples This does not yet address issue OAI#488 but cleans up Examples Object a bit, which is needed whether we alter the OAS as per OAI#488 .
1 parent 321bc4e commit 6c3fa9f

File tree

1 file changed

+122
-26
lines changed

1 file changed

+122
-26
lines changed

versions/3.0.md

+122-26
Original file line numberDiff line numberDiff line change
@@ -1128,20 +1128,20 @@ A request body with a referenced model definition.
11281128
"schema": {
11291129
"$ref": "#/components/schemas/User"
11301130
},
1131-
"examples": [ "http://foo.bar/examples/user-example.json" ]
1131+
"examples": [ "http://foo.bar/other-api.yaml#/components/examples/user-example.json" ]
11321132
},
11331133
"application/xml": {
11341134
"schema": {
11351135
"$ref": "#/components/schemas/User"
11361136
},
1137-
"examples": [ "http://foo.bar/examples/user-example.xml" ]
1137+
"examples": [ "http://foo.bar/other-api.yaml#/components/examples/user-example.xml" ]
11381138
},
11391139
"text/plain": {
1140-
"examples": [ "http://foo.bar/examples/user-example.txt" ]
1140+
"examples": [ "http://foo.bar/other-api.yaml#/components/examples/user-example.txt" ]
11411141
},
11421142
"*/*": {
11431143
"example": {
1144-
"$ref": "http://foo.bar/examples/user-example.whatever"
1144+
"$ref": "http://foo.bar/other-api.yaml#/components/examples/user-example.whatever"
11451145
}
11461146
}
11471147
}
@@ -1155,18 +1155,18 @@ content:
11551155
schema:
11561156
$ref: '#/components/schemas/User'
11571157
examples:
1158-
- 'http://foo.bar/examples/user-example.json'
1158+
- 'http://foo.bar/other-api.yaml#/components/examples/user-example.json'
11591159
'application/xml':
11601160
schema:
11611161
$ref: '#/components/schemas/User'
11621162
examples:
1163-
- 'http://foo.bar/examples/user-example.xml'
1163+
- 'http://foo.bar/other-api.yaml#/components/examples/user-example.xml'
11641164
'text/plain':
11651165
examples:
1166-
- 'http://foo.bar/examples/user-example.txt'
1166+
- 'http://foo.bar/other-api.yaml#/components/examples/user-example.txt'
11671167
'*/*':
11681168
example:
1169-
$ref: 'http://foo.bar/examples/user-example.whatever'
1169+
$ref: 'http://foo.bar/other-api.yaml#/components/examples/user-example.whatever'
11701170
```
11711171

11721172
A body parameter that is an array of string values:
@@ -2355,7 +2355,12 @@ description: Pets operations
23552355

23562356
#### <a name="examplesObject"></a>Examples Object
23572357

2358-
Anywhere an `example` may be given, a JSON Reference MAY be used, with the
2358+
Instead of a single `example`, you may provide an array of
2359+
[Example Object](#exampleObject) values named `examples'.
2360+
`example` and `examples` are mutually exclusive.
2361+
2362+
Anywhere an `example` may be given (including in the Examples Object array),
2363+
a JSON Reference MAY be used, with the
23592364
explicit restriction that examples having a JSON format with object named
23602365
`$ref` are not allowed. This does mean that `example`, structurally, can be
23612366
either a string primitive or an object, similar to `additionalProperties`.
@@ -2365,42 +2370,133 @@ for the value that it is accompanying. Tooling implementations MAY choose to
23652370
validate compatibility automatically, and reject the example value(s) if they
23662371
are not compatible.
23672372

2373+
Here is a single `example` in a model [Schema Object](#schemaObject):
2374+
2375+
```json
2376+
"components" : {
2377+
"schemas" : {
2378+
"identity" : {
2379+
"properties" : {
2380+
"name" : {
2381+
"type" : "string"
2382+
}
2383+
},
2384+
"example" : {
2385+
"$ref" : "#/components/examples/identity"
2386+
}
2387+
}
2388+
}
2389+
```
2390+
}
2391+
23682392
```yaml
2369-
# in a model
2370-
schemas:
2371-
properties:
2372-
name:
2373-
type: string
2393+
components:
2394+
schemas:
2395+
identity:
2396+
properties:
2397+
name:
2398+
type: string
23742399
example:
2375-
$ref: http://foo.bar#/examples/name-example
2400+
$ref: "#/components/examples/identity"
2401+
```
23762402

2377-
# in a request body, note the plural `examples` as the Content-Type is set to `*`:
2403+
Below is an `examples` array in a [Request Body Object](#requestBodyObject),
2404+
showing two in-line examples and references
2405+
to examples in the document's [Components Object](#componentsObject):
2406+
2407+
```json
2408+
"requestBody" : {
2409+
"content" : {
2410+
"application/json" : {
2411+
"schema" : {
2412+
"$ref" : "#/components/schemas/Address"
2413+
},
2414+
"examples" : [ {
2415+
"street" : "119 N Weatherly Ave.",
2416+
"zip" : "55405"
2417+
}, {
2418+
"street" : "119 N Weatherly Ave",
2419+
"street2" : "Apt. D",
2420+
"city" : "Minneapolis",
2421+
"state" : "MN",
2422+
"zip" : "55405",
2423+
"type" : "residence"
2424+
}, {
2425+
"$ref" : "#/components/examples/alt-address"
2426+
} ]
2427+
},
2428+
"application/xml" : {
2429+
"examples" : [ {
2430+
"$ref" : "#/components/examples/simple-address.xml"
2431+
}, {
2432+
"$ref" : "#/components/examples/complete-address.xml"
2433+
} ]
2434+
},
2435+
"text/plain" : {
2436+
"examples" : [ {
2437+
"$ref" : "#/components/examples/address-example.txt"
2438+
} ]
2439+
}
2440+
}
2441+
}
2442+
```
2443+
2444+
```yaml
23782445
requestBody:
23792446
content:
23802447
'application/json':
23812448
schema:
23822449
$ref: '#/components/schemas/Address'
23832450
examples:
2384-
- {"foo": "bar"}
2385-
- {"bar": "baz"}
2451+
- { "street": "119 N Weatherly Ave.", "zip" : "55405" }
2452+
- street: 119 N Weatherly Ave
2453+
street2: Apt. D
2454+
city: Minneapolis
2455+
state: MN
2456+
zip : '55405'
2457+
type: residence
2458+
- $ref: '#/components/examples/alt-address'
23862459
'application/xml':
23872460
examples:
2388-
- $ref: 'http://foo.bar#/examples/address-example.xml'
2461+
- $ref: '#/components/examples/simple-address.xml'
2462+
- $ref: '#/components/examples/complete-address.xml'
23892463
'text/plain':
23902464
examples:
2391-
- $ref: 'http://foo.bar#/examples/address-example.txt'
2392-
2393-
# in a parameter
2394-
2465+
- $ref: '#/components/examples/address-example.txt'
2466+
```
2467+
This shows referencing an example component from another OpenAPI definition,
2468+
in a [Parameter Object](#parameterObject):
2469+
```yaml
23952470
parameters:
23962471
- name: 'zipCode'
23972472
in: 'query'
23982473
schema:
23992474
type: 'string'
24002475
format: 'zip-code'
24012476
example:
2402-
$ref: 'http://foo.bar#/examples/zip-example'
2403-
# in a response, note the plural `examples`:
2477+
$ref: 'http://foo.bar/other-api.yaml#/components/examples/zip-example'
2478+
```
2479+
2480+
Finally, in a [Response Object](#responseObject):
2481+
2482+
```json
2483+
"responses" : {
2484+
"200" : {
2485+
"description" : "your car appointment has been booked",
2486+
"content" : {
2487+
"application/json" : {
2488+
"schema" : {
2489+
"$ref" : "#/components/schemas/SuccessResponse"
2490+
},
2491+
"example" : {
2492+
"$ref" : "http://foo.bar/other-api.yaml#/components/examples/address-example.json"
2493+
}
2494+
}
2495+
}
2496+
}
2497+
}```
2498+
2499+
```yaml
24042500
responses:
24052501
'200':
24062502
description: your car appointment has been booked
@@ -2409,7 +2505,7 @@ schemas:
24092505
schema:
24102506
$ref: '#/components/schemas/SuccessResponse'
24112507
example:
2412-
$ref: http://foo.bar#/examples/address-example.json
2508+
$ref: http://foo.bar/other-api.yaml#/components/examples/address-example.json
24132509
```
24142510

24152511
#### <a name="referenceObject"></a>Reference Object

0 commit comments

Comments
 (0)