Import an OpenAPI specification
Before you begin
- Plan
- Included in Free, Pro and Team. How plans and limits work
- Role
- Viewer or higher to read the contract and Editor or higher to import a document.
- You need
- A project
- An OpenAPI 3.0, 3.1 or 3.2 document in one JSON or YAML file, or split across files you bundle with the CLI
Import an OpenAPI document to get one mock for each of its operations, with a response for each example you choose, and to keep the document as the project’s OpenAPI contract. An operation with a 200 and two 422 examples imports as one mock with three responses, one of them the default.
-
Open your project’s OpenAPI tab and select Import a document. The import has three steps: Document, Choose responses and Review and apply. Nothing is written until the last.
-
Select Upload a file and choose the document, or paste it into Or paste it. The label says whether it is read as JSON or YAML.
-
To serve the mocks under a path other than the one in the document’s
servers, enter it in Base path (optional), such as/v1. -
Select Continue.
-
Under Choose responses, each operation lists its responses, with the ones the import takes ticked. To import something else, see Choose the responses and the default. Select Review.
-
Read the plan under Creates the contract as revision 1: how many Mocks and Responses it creates, then anything under Worth a look before applying. The mocks are folded under a line such as 11 new mocks; open it to see each mock, such as
GET /orders/{id}, with a line for each response, the default one marked answers by default. -
Select Apply import. You are back on the OpenAPI tab.
Choose the responses and the default
Section titled “Choose the responses and the default”Choose responses has a row for each operation, such as POST /orders, with its responses beneath it. Each is named as it will be imported, such as 201 Order created: the status, then the response’s description in the document, with what tells it from responses of the same status in parentheses, such as the media type or the example’s name.
- Tick the responses to import. A mock gets one response for each, so choosing the
200and two422examples makes one mock with three responses, not three mocks. A response such as2XXordefaultasks for the status to send. - When more than one is ticked, Default marks the response that answers requests that ask for nothing in particular. MockHub starts from the best success: an example in the document before one generated from a schema, and a
2xxbefore an error. Let MockHub choose the default goes back to that. - When only error responses are ticked, None of these is a success. Mark the response that answers by default. appears. Nothing is chosen for you, and the plan cannot be applied until you mark one.
A document of more than 25 operations shows them 25 a page, with Search operations to import, Method, Tag, Selected only and Needs attention above the rows to narrow what you see; your choices stay while you filter or change page. A line such as “3 operations selected with 5 responses” counts the choices, not only what is shown. Needs attention lists the operations with an example that fails its schema, a missing default or too many responses.
Select success responses replaces the choices of the operations your filters show with their 2xx responses. Include error responses adds their 4xx and 5xx responses to the operations already selected, and drops nothing. Each first says how many operations and responses it changes, including those on other pages, and a default you marked stays while its response is ticked. Import contract only imports no mock, and Back to the defaults undoes your choices.
Changing a choice makes the plan out of date, so Review plans the import again before you read it. To change the document or the base path, select Back or the Document step; your choices stay unless the document changes.
Result
Section titled “Result”The OpenAPI tab shows Import applied, which says Applied revision 1. with how many mocks and responses were created and that they are inactive, until you select Dismiss or leave the page. It lists them under Created, each a link to the mock with its number of responses. This project’s contract shows the document’s title and version, and Operations lists every operation with what it imports and the mocks linked to it.
The mocks answer nothing until you activate them, so Activate N inactive mocks comes first, right under Import applied. Tick the mocks to activate, then select Activate N mocks; the plan’s limits and any overlap with existing mocks are listed after the choice. Next then offers these steps for exactly the mocks this import created:
- Review mocks opens the Mocks tab at those still inactive.
- Activate N mocks asks first. It shows how many mocks you picked and how many active mocks the project has after them against what the plan allows, and These answer the same requests: lists mocks with an identical matcher, such as an older mock for the same path. Only identical matchers are listed; two patterns that overlap another way are not detected. One call activates all of them or none of them, and no other mock is deactivated to make room or to settle an overlap. It needs the Editor role.
- Try an endpoint offers a mock to try in the sandbox, which works while it is inactive, or a copied request against the project’s address, which needs the mock to be active and the import to be live.
The mocks serve their examples exactly as written; to use templates in one, set its Response templating to Follow the project. To change which response is the default afterwards, see Choose the response that answers. To add a response you left out, see Add one response from the contract.
If a line starts with Saved. and says active mocks may answer the old way for a few seconds, the import is committed and there is nothing to apply again. A mock that is active may answer the old way for a moment, and the import goes live by itself.
Each mock is tagged with the tags of its operation in the document, such as Pet Store as pet-store, so the Any tag filter on the Mocks tab finds an operation group’s mocks. The operation filter, Any contract link, finds the mocks of one operation. See Organise mocks with tags.
What the preview checks
Section titled “What the preview checks”Each planned mock is checked as the import would leave it, against the operation it is linked to in the document. A mock that meets it has no tag; any other is tagged with the result:
| Tag | Means |
|---|---|
| Breaks the document | At least one does not. The findings below it say where, such as /total and the rule it breaks |
| Not fully checked | Part of it could not be checked, such as a header or body holding a template, which is never run here |
| Not checked | The operation it is linked to is not in this document |
Below a tag, a line names the operation it was checked against. A mock with several responses is checked response by response, and its findings name the response, such as Response oa-422-invalid-email-3a0fd5:.
Open a new mock’s response, such as 422 Invalid email, to see its name, status code, headers and body as the import leaves them, each tagged From the document, Your edit or Undecided. Details of the mock’s shared fields does the same for the name, the request and the templating.
Check recorded responses first
Section titled “Check recorded responses first”When the project already receives requests, such as captures of your real API, you can check what it answered against the document before importing it. This appears on plans that include checking request logs.
-
Go on to Review and apply as above.
-
Open Check recorded responses against this document (optional) and choose the period, such as Last 24 hours, and which logs: Every logged request, Mock answers or Upstream captures.
-
Select Check recent logs.
The newest 20 logs of that period are checked against the document, or the newest 10 beside the active revision when the project has a contract already, with the results as in Check request logs in bulk. The document is named The uploaded document. When the period held more, a line says so, such as The newest 20 of 750 logs in that time.
Nothing is written: no revision, no mock and no latest check. The check counts as one import and one check of the workspace’s limits. Changing the document or the Base path clears the results.
Import a specification split across files
Section titled “Import a specification split across files”A document whose $refs name other files, such as $ref: ./schemas/order.yaml or $ref: '../common.yaml#/components/schemas/Error', is imported by the CLI, which bundles it into one document first:
mockhub openapi import --env staging --file api/openapi.yaml --dry-runTo upload it on the OpenAPI tab instead, write the bundle to a file and choose that file:
mockhub openapi bundle --file api/openapi.yaml --out openapi.bundled.yaml- Each file a reference names is added to the document’s
components, in the section its place takes, such asschemasorresponses. It is named after its file, such asorderfororder.yaml, or after the last part of the reference’s#/…fragment, such asError. A component in your document that is only a reference to a file keeps its own name. - A name already taken gets a number, such as
Error2, and the command lists each one. - The bundle is YAML, with every value written as in its file. Download on the OpenAPI tab and
mockhub openapi pullreturn the bundle, not your files. - Renaming a file renames its component, so the next import reports a schema removed and one added, and values you varied in it are chosen again.
- Files must be in the document’s folder or below it. To reach a shared folder above it, pass
--ref-rootwith a folder that holds both. - A reference to a URL is refused. Download the file and refer to it by its path.
- A document that refers to no other file is sent exactly as it is.
Import part of a large specification
Section titled “Import part of a large specification”A document over your plan’s size or number of operations is refused whole, before you choose any responses, with one of these:
This document is <n> bytes; your plan imports documents up to <n> bytes.This document has <n> operations; your plan imports up to <n> per project.The CLI can import part of it: the operations with a tag, under a path, or both, with only what they refer to.
mockhub openapi import --env staging --file github.json --only-tag repos --dry-run--only-tag reposkeeps the operations taggedrepos.--only-path '/repos/**'keeps those under/repos, written as the document writes its paths, such as/repos/{owner}/{repo}:*matches within one part of the path, and**across any number of parts. Each flag can be given more than once, and an operation is kept when it matches a tag and a path.- The command prints the filters and what it kept, such as
kept 203 of 1,242 operations and 337 of 1,797 components. A filter that keeps nothing is refused, with the document’s tags listed. - Components that no kept operation refers to are left out, and so are the document’s
webhooks. - The part is imported as an ordinary document, in YAML, with every value written as in your file. Download on the OpenAPI tab and
mockhub openapi pullreturn the part, not your file. - Pass the same filters on every later import. Without them the whole document is sent: the preview lists every other operation as added, and the plan may refuse it.
- To upload the part on the OpenAPI tab instead, write it to a file:
mockhub openapi bundle --file github.json --out repos.yaml --only-tag repos. - A document that refers to schemas by
$idor$anchorkeeps all its schemas, and the command says so. - An operation you keep that refers into one you leave out, such as with
$ref: '#/paths/~1owners/get/responses/200', is refused, naming both. Keep both, or neither. - The file may be up to 64 MiB before it is cut. The part sent is still bound by the plan.
If older mocks exist for an operation
Section titled “If older mocks exist for an operation”Earlier imports made one mock for each example of an operation. When the plan finds several mocks imported from one operation, it refuses to add another beside them: Several mocks were imported from the operation lists them, with the reason.
- Choose Keep “GET /orders” (Active) and merge the others into it for the mock to keep. Their responses, with what the import knows of where they came from, move into it and the others are deleted with their version history. Their request logs stay under their old ids, because history is not merged.
- Or tick Stop managing it with imports instead for a mock to leave alone. It keeps its link to the contract, and no later import updates, merges or removes it.
A merge happens only when the mocks agree on what they match, their scenario, their data store use and their templating, and no webhook fires when one is served. When they do not, the plan says what differs and leaves every mock as it is. If an active mock is merged into an inactive one, the survivor becomes active, and the plan says so.
If the plan cannot be applied
Section titled “If the plan cannot be applied”Apply import stays unavailable while the plan has a problem, listed under This plan cannot be applied yet. with what to do. A mock name the project already uses is the most common: rename the existing mock, or select Back and leave that operation out under Choose responses. More than 50 responses chosen for one operation is refused, with the operation named, so leave some out: nothing is cut for you.
When a chosen example does not match its own schema, the example is tagged Fails its schema. Fix the document, leave the example out, or tick Import the examples that fail their schema anyway. A relative link under format: uri, or a date-time with no UTC offset, does not make an example fail; see how formats are read.
When a mock the import leaves is tagged Breaks the document, such as a mock that answers an error on purpose, Apply import waits until you tick Apply although mocks it leaves answer with what this document does not allow, such as an intentional failure. When one is Not fully checked, it waits until you tick Apply although some mocks it leaves could not be checked in full. The apply checks the mocks again. If its own check needs a tick the preview did not ask for, such as a check that ran out of time, nothing is applied and the tab says why.
A document that is not valid OpenAPI is refused with each problem and its line, such as Line 12, column 7:. See OpenAPI document invalid and OpenAPI document unsupported.
If the project changed between the preview and the apply, the page says Nothing was applied. Update the preview and apply again. Select Update the preview, read the plan again and apply it. See OpenAPI conflict.
Limits
Section titled “Limits”- A JSON response without an example is filled from its schema and listed as generated from the schema:
- Values come from the document first. Where it gives none, MockHub uses a placeholder shaped by the field’s
formatwhere it knows it, such as2024-01-01T00:00:00Zfordate-time,USDforcurrencyor1704067200forunix-time. - In the preview, a response holding placeholders is tagged N placeholder value(s), Details of it names where they are, and one note says how many mocks hold them.
- With Use only examples from the specification under Advanced options, such a response is still listed, but becomes an operation’s default only when every value came from the document.
- Where a
discriminatornames schemas, in aoneOforanyOfor throughallOf, the response is filled from the first of them, named in the discriminator’s property. - The filling also meets
if(with itsthenwhere it can, else itselse),not,containswith itsmaxContains,propertyNamesand dependent properties. To meet anot, an object may leave out its optional properties, and a placeholder may take another shape, such asSTRING,0or0.5. - An object whose
minPropertiesasks for more properties than it declares gets extra entries named aspropertyNamesorpatternPropertiesallow, elseadditionalProp1,additionalProp2and so on, with values asadditionalPropertiesorunevaluatedPropertiesdescribe them. - The filled response is checked against the whole schema like an example, so a
oneOfwhose branches overlap reports it as not matching; it imports once you acknowledge that. - Its values are the same on every request until you vary them.
- Values come from the document first. Where it gives none, MockHub uses a placeholder shaped by the field’s
- A response header marked
requiredwithout an example is filled from its schema the same way, whatever the body. An optional one without an example is left out. - An operation with nothing to fill, such as a text or XML response without an example, is listed as No example to import. Write its mock by hand and link it, as in Check a mock against its contract.
- Imported mocks match on the method and the path. A path with parameters, such as
/orders/{orderId}, is written as a Path with parameters that keeps the document’s names, so a response readsrequest.path.orderId. Mocks an earlier import wrote with a Regex path stay as they are, and importing again does not report them as changed; convert one to use the names. Add query, header or body rules yourself. - A mock holds up to 50 responses, so an operation with more examples than that needs some left out.
- Two examples of one status, such as two
422examples, or one status in two media types, are separate responses under separate keys, so editing one never touches the other. - HEAD, TRACE and QUERY operations, and paths MockHub keeps for itself such as
/__admin, are listed but get no mock. - Two paths that differ only in their parameter names, such as
/orders/{id}and/orders/{orderId}, are one path to OpenAPI. The first in the document is imported, and the preview warns that each later one is left out. - The OpenAPI tab reads one file from your computer or the clipboard, and refuses a reference to another file; the CLI bundles such a document. Neither fetches a document or a reference from a URL.
- The document size, the number of operations and the configuration storage are plan limits; see the limits page. The CLI can import part of a larger document. The plan names the active mocks and the storage an import leaves only when it comes near a limit; otherwise it says how much configuration it adds.