description
field that offers additional human-readable information for documentation. Alongside descriptions, some OpenAPI objects can include sample values in the OpenAPI Document, enhancing the generated documentation by providing representative content that the upstream service might return in responses.
Tyk leverages examples from your API documentation (in OpenAPI Spec format) to generate mock responses for the API exposed via the gateway. Based on this data, Tyk adds a new middleware named “Mock Response” and returns various mock responses according to your spec. Refer to the Mock configuration guide to learn how to do this.
The specification provides three methods for Tyk to deduce the mock response: example
, examples
and schema
.
example
: A sample value that could be returned in a specific field in a response (see below)examples
: A map pairing an example name with an Example Object (see below)schema
: JSON schema for the expected response body (see belowexample
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.content-type
(e.g. application/json
, text/plain
), per OpenAPI Specification, must be declared for each example
or examples
in the API description.example
to generate a mock responseexample
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 Response body example
in plain text format.
examples
to generate a mock responseGET /get
could return HTTP 200
but here provides two example body values Response body from first-example
and Response body from second-example
, again in plain text format.
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.
schema
to generate a mock responseexample
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.
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
GET /get
endpoint
firstname
has the value string
since there was no example for it in the OpenAP document so Tyk used the word string
as the value for this field.
x-tyk-api-gateway
. Once added, you need to update or import it to Tyk Dashboard using Tyk Dashboard API or via Tyk Dashboard UI. This is useful when you have already tested your API in dev environments and now you need to deploy it to staging or production in a declarative manner.
The design of the Tyk OAS API Definition takes advantage of the operationId
defined in the OpenAPI Document that declares both the path and method for which the middleware should be added.
The mock response middleware (mockResponse
) can be added to the x-tyk-api-gateway.middleware.operations
section (Tyk OAS Extension) in your Tyk OAS API Definition for the appropriate operationId
(as configured in the paths
section of your OpenAPI Document).
For basic operation, the mockResponse
object has the following configuration:
enabled
: enable the middleware for the endpointcode
: the HTTP status code to be provided with the response (this defaults to 200
if not set)body
: the payload to be returned as the body of the responseheaders
: the headers to inject with the responseGET /example-mock-response1/anything
endpoint:
GET /example-mock-response1/anything
would return:
operationId
defined in the OpenAPI Document that declares both the path and method for which the middleware should be added. Endpoint paths
entries (and the associated operationId
) can contain wildcards in the form of any string bracketed by curly braces, for example /status/{code}
. These wildcards are so they are human-readable and do not translate to variable names. Under the hood, a wildcard translates to the “match everything” regex of: (.*)
.
The mock response middleware (mockResponse
) can be added to the operations
section of the Tyk OAS Extension (x-tyk-api-gateway
) in your Tyk OAS API Definition for the appropriate operationId
(as configured in the paths
section of your OpenAPI Document).
For basic operation, the mockResponse
object has the following configuration:
enabled
: enable the middleware for the endpointfromOASExamples
: an object used to instruct Tyk Gateway to return a response from the OpenAPI descriptionfromOASExamples
object has the following configuration:
enabled
: enable the automatic configuration of mock responsecode
: [optional] identifies which HTTP status code to be provided with the response (defaults to 200
if not set)contentType
: [optional] identifies which response body type to use (defaults to application/json
if not set)exampleName
: [optional] the sample response to be returned from an examples
listcode
, contentType
, exampleName
) are used to identify which sample response should be returned by the mock if multiple sample responses are declared in the OpenAPI description.
In the following example, the OpenAPI description declares three possible responses: two for HTTP 200 and one for HTTP 300. We have configured the Mock Response middleware to return the value defined for HTTP 200 (code) with exampleName
set to “second-example”. The JSON below shows the updated Tyk OAS API definition, after Tyk has generated and added the mock response middleware:
GET /example-mock-response2/anything
would return:
"code":300
in the fromOASExamples
object, a call to GET /example-mock-response2/anything
would instead respond as follows:
examples
are defined in the OpenAPI description but no default exampleName
is set in the middleware configuration fromOASExamples
Tyk will select randomly from the multiple examples
. Yes, that means the response may change with every request. You can control which response will be returned using special headers in the request.Accept
: This standard HTTP header will override the response content type (e.g. application/json
, text/plain
)X-Tyk-Accept-Example-Code
: This will select the HTTP response code for which to return the example response (e.g. 400
)X-Tyk-Accept-Example-Name
: This identifies which example to select from an examples
listcode
, contentType
or exampleName
, an HTTP 404 error will be returned to inform the client that there is no declared example for that configuration.
In the example below, the OpenAPI document declares two possible responses: one for HTTP 200 and one for HTTP 300. We have configured the Mock Response middleware to return the value defined for HTTP 200 for which the body (content) is in JSON format and a custom header X-Status
which will take the default value of true
.
X-Tyk-Accept-Example-Code
: 300Accept
: text/plainX-Status
header set to false
.
The configuration above is a complete and valid Tyk OAS API Definition that you can import into Tyk to try out the mock response middleware.
content-type
for which a mock response is configured in the OpenAPI specification, the API Designer will display the associated response.
content-type
)x-tyk-api-gateway
extension but is automatically generated in response to the particular request received to the endpoint.extended_paths
section of your API definition - white_list
, black_list
or ignored
. The mock response can then be configured within the method_actions
element within the new object.
The white_list
, black_list
and ignored
objects all have the same structure and configuration as follows:
path
: the endpoint pathmethod
: this should be blankignore_case
: if set to true
then the path matching will be case insensitivemethod_actions
: the configuration of the mock responsemethod_actions
object should be configured as follows, with an entry created for each method on the path for which you wish to configure the mock response:
action
: this should be set to reply
code
: the HTTP status code to be provided with the responseheaders
: the headers to inject with the responsedata
: the payload to be returned as the body of the responseGET /anything
endpoint. The allow list middleware has been enabled for this endpoint and is case sensitive, so calls to GET /Anything
will not return the mock response.
A call to GET /anything
would return:
extended_paths
section, e.g. one of allow list (white_list
), block list (black_list
) or ignore authentication (ignore
). The mock response configuration object has the following properties:
/foo
.action
field with a value set to reply
.ignore
) for the GET /foo
endpoint. When a request is made to the endpoint then authentication will be ignored and a mock response is returned with status code 200
and a response body payload of {"foo": "bar"}
. The middleware has been configured to be case sensitive, so calls to GET /Foo
will not ignore authentication.