OpenAPI Import
Already have an OpenAPI 3.x spec? Generate a whole mocks/ directory from it in one command instead of hand-writing every file:
mimic import-openapi ./spec.yaml --out ./mocks/generatedJSON and YAML specs are both accepted. The importer writes one live mock file per operation — built from its primary response — plus an inert .json.disabled file for every other documented status. Use --format yaml to write YAML and .yaml.disabled alternatives. The output is plain mock-file data, so it hot reloads like any other mock and can be hand-edited afterwards.
The default output directory, ./mocks/generated, sits inside the ./mocks the server falls back to locally — so mimic import-openapi ./spec.yaml && mimic serves the generated mocks with nothing else to configure. When --out points elsewhere, the importer prints the exact command to start the server against it.
Options
Section titled “Options”| Option | Default | Description |
|---|---|---|
--out <dir> |
./mocks/generated |
Output directory |
--status <code> |
200 |
Status treated as each operation’s primary response; its file is the live one |
--format <json|yaml> |
json |
Write JSON or YAML mock files. yml is an alias for yaml. |
--bundle |
off | Group operations in one array file per path. |
--base-path <path> |
from servers[0] |
Prefix generated paths. Pass --base-path '' to omit the prefix. |
--force |
off | Write into a non-empty output directory, overwriting files of the same name |
--brace-params |
off | Emit path parameters as {id} instead of :id |
-h, --help |
— | Show usage |
Example
Section titled “Example”Given this spec:
openapi: 3.0.3info: { title: Petstore, version: 1.0.0 }paths: /users/{id}: get: responses: '200': content: application/json: example: { "id": 1, "name": "Alice" } '404': content: application/json: schema: $ref: '#/components/schemas/Error'components: schemas: Error: type: object properties: code: { type: integer } message: { type: string }mimic import-openapi spec.yaml --out ./mocks/generated writes:
mocks/generated/get_users_id.json (the primary 200 response — no status suffix):
{ "method": "GET", "path": "/users/:id", "status": 200, "response": { "id": 1, "name": "Alice" }, "consume_body": false}mocks/generated/get_users_id_404.json.disabled (an alternative response — inert until renamed):
{ "method": "GET", "path": "/users/:id", "status": 404, "response": { "code": 0, "message": "string" }, "consume_body": false}Start the server and GET /users/42 returns the 200 body immediately — path parameters are translated to Mimic’s :id syntax, so generated mocks work with the matcher unedited.
To serve the 404 instead, swap which file is live:
mv mocks/generated/get_users_id.json mocks/generated/get_users_id_200.json.disabledmv mocks/generated/get_users_id_404.json.disabled mocks/generated/get_users_id_404.jsonHot reload picks the change up within a couple of seconds — no restart needed.
Bundling
Section titled “Bundling”Use --bundle to group each path’s operations into one array file. Other documented responses for the path go into a separate disabled array.
mimic import-openapi spec.yaml --out ./mocks/generated --bundle --format yamlFor example, GET /users and POST /users share users.yaml, while their alternative statuses go into users_alt.yaml.disabled. Removing .disabled activates the entire array. Keep the desired alternatives and adjust the corresponding live mocks to avoid ambiguous matches.
Where the response body comes from
Section titled “Where the response body comes from”For each response, the importer takes the first of these that the spec provides:
content.<media type>.example- The first entry in
content.<media type>.examples(itsvalue) - A stub built from
content.<media type>.schema {}— for body-less responses like204
JSON media types are preferred when a response offers several.
Schema stubs
Section titled “Schema stubs”When there’s no example, the schema is walked recursively and each field gets a type-appropriate placeholder:
| Schema | Stub |
|---|---|
type: string |
"string" (or a format-appropriate value: date, date-time, uuid, email, uri) |
type: integer / number |
0 / 0.0 |
type: boolean |
false |
type: object |
{} with every property stubbed recursively |
type: array |
one stubbed element from items, or [] if items is absent |
enum / default / example |
the spec’s own value, which always wins over a stub |
allOf subschemas are merged; oneOf / anyOf use the first variant.
Semantics
Section titled “Semantics”- By default, multiple response codes become separate files, and only the primary one is live. Alternatives share a path and method with the primary response and carry no matcher to tell them apart, so leaving them all enabled would make the served response depend on directory read order. They land as
.json.disabledinstead — rename to enable. They are alternative responses, not a call-order sequence, so they are also deliberately not folded into a singlesequencemock. - The primary response is
--statusif the operation declares it, otherwise its lowest declared2xx, otherwise its lowest declared status. So aPOSTdocumenting only201gets a live201, and an operation documenting only errors still gets a live mock rather than an all-disabled route. - OpenAPI’s
defaultkey maps to--statusfor its filename, but never outranks a declared success response — it usually documents an error shape, and serving that by default would be misleading. It becomes the live mock only when it’s all an operation has. $refis resolved for schemas, responses, path items, and named examples. Circular references collapse to{}at the point of recursion rather than looping forever; external (./other.yaml#/...) and unresolvable refs degrade to{}with a warning.- Existing files are protected. A non-empty
--outdirectory is refused unless you pass--force, so a re-import can’t silently clobber mocks you edited by hand. With--force, every overwritten file is named in a warning. - Response keys may be quoted (
'200') or bare (200),default, or wildcards (4XX→400). - Only OpenAPI 3.x is supported. Swagger 2.0 specs are rejected with a clear message — convert first.
- A malformed spec produces an error and a non-zero exit code, never a panic.