- Automatic generation/amendment of a mock API from an existing OpenAPI doc as it evolves,
- API prototyping - defining API behaviour via stubs and auto-generating OpenAPI + documentation.
Getting started
From the app’s home screen, create a new mock API and choose the OpenAPI type: When the new mock API is created an extra item will be present on the left-hand nav bar, taking you to the OpenAPI editor page: Navigating to the Settings tab on the same page, toggling on “Enable public API documentation” and clicking the link underneath will show the public API documentation (which will be initially empty apart from header information since there are no paths defined in the OpenAPI doc).Generating stubs from OpenAPI
Stubs will be created or updated whenever changes are saved to the OpenAPI doc. Add a new path entry and click Save: Then navigate to the Stubs page and see that two new stubs have been created - one with specific request parameters required and one “default” i.e. will match regardless of specific parameter values provided the method and URL path are correct. Stubs will be generated following the stub generation rules.Updating an OpenAPI doc
When an OpenAPI doc is updated, for everypath-method-status-contentType, existing stubs will be updated if any of the following apply:
- The existing stub was generated from an example and the example hasn’t changed its name.
- If there is one example within the given
path-method-status-contentTypewhich shares the response body with the existing stub. - If the
path-method-status-contentTypeonly provides a single example. - If the
path-method-status-contentTypedoesn’t provide examples at all.
/orders to /v1/orders the path
will be classed as a new path and a new stub will be created. The old stub will not be deleted.
If you are modeling new data scenarios and you add new stubs to your Mock API after generating stubs from an OpenAPI
specification, these stubs will not always be updated when you update your OpenAPI specification. If those new stubs
do not match an example in your OpenAPI specification, they will not be updated when you update your OpenAPI specification
(adding a new parameter for example) and you will need to update those manually.
Stub generation rules
When updating an OpenAPI doc, the resulting stubs from new OpenAPI elements will be added. Stub generation will be based on the following rules:304response:- Request header matcher
If-None-Matchwith specific value12345.
- Request header matcher
422response:- Only one stub will be generated, with a request body matcher not matching the schema or missing.
- If more than one response example provided, it will pick one randomly as the response body.
- If no response example provided, the response body will be autogenerated based on the schema.
400response:- Only one stub will be generated, with neither request parameters nor body present or matching the schema.
- If more than one response example provided, it will pick one randomly as the response body.
- If no response example provided, the response body will be autogenerated based on the schema.
- Any other response status:
- If no example is provided, it will generate a stub with autogenerated request parameters and response, based on schema.
- If at least one example is provided:
- It will generate one stub per example, using specific request parameter matchers and taking the example as the response body.
- The request parameter matchers will be autogenerated based on the schema, unless the extension
x-parameter-valuesis provided (as explained here), in which case it will be used to generate the expected values of the parameter matchers.
Controlling generated parameter values in your stubs
If an OpenAPI element has a parameter (header for example) that is set torequired: true then the stub will be generated
or updated with that parameter. WireMock Cloud adds a value for that parameter to match on. You can control the value
generated in your stubs using various OpenAPI elements:
If no min or max length are provided in the schema, defaults of a minimum of 3 and a maximum of
200 is used. Therefore, an OpenAPI specification snippet like the following:
tripId equalsTo matcher with the following value - gtpq1fggnuolb31tya6rrc1tye1am5bkzw5kjxxeyscx9lb3zhla
Adding a minLength and a maxLength to the schema will control the size of the random string. The snippet below:
tripId equalsTo matcher with the following value - aspp
You can force a value to be used in the matcher by creating an enum with only one value. This is effectively the same as
generating a constant:
tripId equalsTo matcher with the following value - trip-id-68975013.
Optional minLength and maxLength elements can be used to further control the generated value:
tripId equalsTo matcher with the following value - trip-id-6.