Mock Responses using OpenAPI metadata
Last updated: 4 minutes read.
The OpenAPI Specification includes metadata that can be used by automatic documentation generators to produce comprehensive reference guides for APIs. Most objects in the specification include a description
field that can provide additional human-readable information that is fed into such documentation. Alongside the descriptions, some OpenAPI objects can have sample values listed in the OpenAPI Document that further enhance the generated documentation by giving representative content that the upstream service might provide in responses.
The specification provides two different ways for an API developer to provide sample responses for an endpoint:
example
: a sample value that could be returned in a specific field in a response (see below)examples
: a list of key-value pairs comprising of"exampleName":"value"
(see below)
Tyk’s mock response middleware can automatically generate a response using the data provided in the OpenAPI description part of the Tyk OAS API Definition.
If neither example
nor examples
are defined, Tyk can automatically create a mock response if a schema
is defined that describes the format of the response as shown below.
Note
Note that example
and examples
are mutually exclusive within the OpenAPI Document for a field in the responses
object: the developer cannot provide both for the same object.
The content-type (e.g. application/json
, text/plain
) must be declared for each example
or examples
in the API description.
Using example
to generate a mock response
|
|
In this extract from an OpenAPI description, a single example
has been declared for a request to GET /get
- the API developer indicates that such a call could return HTTP 200
and the body value Furkan
in plain text format.
Using examples
to generate a mock response
|
|
In this extract, the API developer also indicates that a call to GET /get
could return HTTP 200
but here provides two example body values Jeff
and Laurentiu
, again in plain text format.
The exampleNames
for these two values have been configured as first-example
and second-example
and can be used to invoke the desired response from a mocked endpoint.
Using schema
to generate a mock response
If there is no example
or examples
defined for an endpoint, Tyk will try to find a schema
for the response. If there is a schema, it will be used to generate a mock response. Tyk can extract values from referenced or nested schema objects when creating the mock response.
Response headers do not have standalone example
or examples
attributes, however they can have a schema
- the Mock Response middleware will include these in the mock response if provided in the OpenAPI description.
The schema properties may have an example
field, in which case they will be used to build a mock response. If there is no example
value in the schema then default values are used to build a response as follows:
string
>"string"
integer
>0
boolean
>true
For example:
|
|
This partial OpenAPI description defines a schema for the GET /get
endpoint that Tyk Gateway could use to generate this mock response:
HTTP/1.1 200 OK
X-Status: Maciej
Content-Type: application/json
{
"lastName": "Petric",
"name": "Andrei",
"id": 0
}