Adding Examples
An API specification can include examples for:
- response MIME types,
- schemas (data models),
- individual properties in schemas.
Examples can be used by tools and libraries, for instance, Swagger UI auto-populates request bodies based on input schema examples, and some API mocking tools use examples to generate mock responses.
Note: Do not confuse example values with the default
values. An example is used to illustrate what the value is supposed to be like. A default value is something that the server uses if the value is not provided in the request.
Schema Examples
The example
key is used to provide a schema example. Examples can be given for individual properties, objects and the whole schema.
Property Examples
Property examples can be specified inline. The example value must conform to the property type.
Note that multiple example values per property or schema are not supported, that is, you cannot have:
Object Examples
Properties of a type object can have complex inline examples that include that object’s properties. The example should comply with the object schema.
Array Examples
An example for an array of primitives:
Similarly, an array of objects would be specified as:
Whole Schema Examples
An example
can be specified for the entire schema (including all nested schema), provided that the example conforms to the schema.
Response Examples
Swagger allows examples on the response level, each example corresponding to a specific MIME type returned by the operation. Such as one example for application/json
, another one for text/csv
and so on. Each MIME type must be one of the operation’s produces
values — either explicit or inherited from the global scope.
All examples are free-form, meaning their interpretation is up to tools and libraries.
JSON and YAML Examples
Since JSON and YAML are interchangeable (YAML is a superset of JSON), both can be specified either using the JSON syntax:
or the YAML syntax:
XML Examples
There is no specific syntax for XML response examples. But, since the response examples are free-form, you can use any format that you wish or that is supported by your tool.
Alternatively, you can specify the example values in the response schema, as explained above.
Text Examples
Since all response examples are free-form, you can use any format supported by your tool or library. For instance, something like:
See also this post on Stack Overflow for tips on how to write multi-line strings in YAML.
Example Precedence
If there are multiple examples on different levels (property, schema, response), the higher-level example is used by the tool that is processing the spec. That is, the order of precedence is:
- Response example
- Schema example
- Object and array property examples
- Atomic property examples and array item examples
Examples and $ref
OpenAPI 2.0 example
and examples
keywords require inline examples and do not support $ref
. The example values are displayed as is, so $ref
would be displayed as an object property named $ref
.
Referencing examples is supported in OpenAPI 3.0.
Did not find what you were looking for? Ask the community
Found a mistake? Let us know