Adding Examples
You can add examples to parameters, properties and objects to make OpenAPI specification of your web service clearer. Examples can be read by tools and libraries that process your API in some way. For example, an API mocking tool can use sample values to generate mock requests. You can specify examples for objects, individual properties and operation parameters. To specify an example, you use the example
or examples
keys. See below for details.
Note for Swagger UI users: Support for multiple examples
is available since Swagger UI 3.23.0 and Swagger Editor 3.6.31.
Note: Do not confuse example values with default values. An example illustrates what the value is supposed to be. A default value is what the server uses if the client does not provide the value.
Parameter Examples
Here is an example of a parameter value:
1parameters:2 - in: query3 name: status4 schema:5 type: string6 enum: [approved, pending, closed, new]7 example: approved # Example of a parameter value
Multiple examples for a parameter:
1parameters:2 - in: query3 name: limit4 schema:5 type: integer6 maximum: 507 examples: # Multiple examples8 zero: # Distinct name9 value: 0 # Example value10 summary: A sample limit value # Optional description11 max: # Distinct name12 value: 50 # Example value13 summary: A sample limit value # Optional description
As you can see, each example has a distinct key name. Also, in the code above, we used an optional summary
keys with description. Note: the sample values you specify should match the parameter data type.
Request and Response Body Examples
Here is an example of the example
keyword in a request body:
1paths:2 /users:3 post:4 summary: Adds a new user5 requestBody:6 content:7 application/json:8 schema: # Request body contents9 type: object10 properties:11 id:12 type: integer13 name:14 type: string15 example: # Sample object16 id: 1017 name: Jessica Smith18 responses:19 "200":20 description: OK
Note that in the code above, example
is a child of schema
. If schema
refers to some object defined in the components
section, then you should make example
a child of the media type keyword:
1paths:2 /users:3 post:4 summary: Adds a new user5 requestBody:6 content:7 application/json: # Media type8 schema: # Request body contents9 $ref: "#/components/schemas/User" # Reference to an object10 example: # Child of media type because we use $ref above11 # Properties of a referenced object12 id: 1013 name: Jessica Smith14 responses:15 "200":16 description: OK
This is needed because $ref
overwrites all the siblings alongside it. If needed, you can use multiple examples:
1paths:2 /users:3 post:4 summary: Adds a new user5 requestBody:6 content:7 application/json: # Media type8 schema: # Request body contents9 $ref: "#/components/schemas/User" # Reference to an object10 examples: # Child of media type11 Jessica: # Example 112 value:13 id: 1014 name: Jessica Smith15 Ron: # Example 216 value:17 id: 1118 name: Ron Stewart19 responses:20 "200":21 description: OK
Here is an example of the example
in response bodies:
1responses:2 "200":3 description: A user object.4 content:5 application/json:6 schema:7 $ref: "#/components/schemas/User" # Reference to an object8 example:9 # Properties of the referenced object10 id: 1011 name: Jessica Smith
Multiple examples in response bodies:
1responses:2 "200":3 description: A user object.4 content:5 application/json:6 schema:7 $ref: "#/components/schemas/User" # Reference to an object8 examples:9 Jessica:10 value:11 id: 1012 name: Jessica Smith13 Ron:14 value:15 id: 2016 name: Ron Stewart
Note: The examples in response and request bodies are free-form, but are expected to be compatible with the body schema.
Object and Property Examples
You can also specify examples for objects and individual properties in the components
section.
- Property-level example:
1components:2 schemas:3 User: # Schema name4 type: object5 properties:6 id:7 type: integer8 format: int649 example: 1 # Property example10 name:11 type: string12 example: New order # Property example
- Object-level example:
1components:2 schemas:3 User: # Schema name4 type: object5 properties:6 id:7 type: integer8 name:9 type: string10 example: # Object-level example11 id: 112 name: Jessica Smith
Note that schemas and properties support single example
but not multiple examples
.
Array Example
You can add an example of an individual array item:
1components:2 schemas:3 ArrayOfInt:4 type: array5 items:6 type: integer7 format: int648 example: 1
or an array-level example containing multiple items:
1components:2 schemas:3 ArrayOfInt:4 type: array5 items:6 type: integer7 format: int648 example: [1, 2, 3]
If the array contains objects, you can specify a multi-item example as follows:
1components:2 schemas:3 ArrayOfUsers:4 type: array5 items:6 type: object7 properties:8 id:9 type: integer10 name:11 type: string12 example:13 - id: 1014 name: Jessica Smith15 - id: 2016 name: Ron Stewart
Note that arrays and array items support single example
but not multiple examples
.
Examples for XML and HTML Data
To describe an example value that cannot be presented in JSON or YAML format, specify it as a string:
1content:2 application/xml:3 schema:4 $ref: "#/components/schemas/xml"5 examples:6 xml:7 summary: A sample XML response8 value: "<objects><object><id>1</id><name>new</name></object><object><id>2</id></object></objects>"9 text/html:10 schema:11 type: string12 examples:13 html:14 summary: A list containing two items15 value: "<html><body><ul><li>item 1</li><li>item 2</li></ul></body></html>"
You can find information on writing multiline string in YAML in this Stack Overflow post: https://stackoverflow.com/questions/3790454/in-yaml-how-do-i-break-a-string-over-multiple-lines.
External Examples
If a sample value cannot be inserted into your specification for some reason, for instance, it is neither YAML-, nor JSON-conformant, you can use the externalValue
keyword to specify the URL of the example value. The URL should point to the resource that contains the literal example contents (an object, file or image, for example):
1content:2 application/json:3 schema:4 $ref: "#/components/schemas/MyObject"5 examples:6 jsonObject:7 summary: A sample object8 externalValue: "http://example.com/examples/object-example.json"9 application/pdf:10 schema:11 type: string12 format: binary13 examples:14 sampleFile:15 summary: A sample file16 externalValue: "http://example.com/examples/example.pdf"
Reusing Examples
You can define common examples in the components/examples
section of your specification and then re-use them in various parameter descriptions, request and response body descriptions, objects and properties:
1 content:2 application/json:3 schema:4 $ref: '#/components/schemas/MyObject'5 examples:6 objectExample:7 $ref: '#/components/examples/objectExample'8 ...9 components:10 examples:11 objectExample:12 value:13 id: 114 name: new object15 summary: A sample object
Did not find what you were looking for? Ask the community
Found a mistake? Let us know