OpenAPI contracts
Before you begin
- Plan
- Included in Free, Pro and Team. How plans and limits work
- Role
- Viewer or higher to read a contract and check a response and Editor or higher to import, update or delete a contract.
A project can keep one OpenAPI contract: an OpenAPI 3.0, 3.1 or 3.2 document describing the API its mocks stand in for. MockHub turns the document’s examples into mocks, updates those mocks when a later version of the document arrives, and checks a mock’s rendered response against the operation it answers for. You meet it on the project’s OpenAPI tab, in the mock editor’s Contract operation section, and in Try it.
Revisions
Section titled “Revisions”Each document you import becomes a numbered revision.
The newest is the active one: imports and links always refer to it, and so does a check unless you choose an earlier revision. The Operations list, its coverage and a check can read any revision the project keeps.
The Revisions list keeps earlier ones for as many versions as your plan keeps of a mock’s history, and each can be downloaded: the active one exactly as you uploaded it, an older one as JSON. Importing the same document again, even reformatted, makes no new revision. Revision numbers are never reused in a project, even after its contract is deleted, so a number always names one document.
An earlier revision can be compared with the active one, operation by operation, and restored. A restore goes through the same preview as an update and becomes a new revision, so your edits to imported mocks stay. It brings back only the document: mocks imported after that revision stay until you delete them, and mocks deleted since are not recreated unless you choose them. See Go back to an earlier revision.
Operations and examples
Section titled “Operations and examples”Every method on every path of the document is an operation, listed under Operations with the mocks linked to it. An import turns examples into mocks, without AI and without inventing values:
- A named example, the media type’s
example, or the schema’s own example becomes the response body. JSON keeps its values as written:{},[],1.0and"4299"stay what they are. - Response headers with an example are sent; the others are left out.
- An operation without a usable example stays in the catalogue as No example to import. You can still write a mock for it by hand and link it.
By default an operation contributes one mock: its lowest success status that has an example, preferring JSON. Choose responses picks other examples, other responses such as a 404, or the contract alone.
Mocks an import makes
Section titled “Mocks an import makes”Imported mocks are ordinary mocks, named after their operation and status, such as GET /orders/{id} 200:
- They start Inactive. Nothing answers your callers until you activate them.
- Their Response templating is Serve as written, so an example containing
{{is sent as text, not run as a template. - They match on the method and the path only. A path parameter such as
{id}matches any one path segment, soGET /orders/{id}answers/orders/42. Query parameters, headers and request bodies are not matched. - They are served under the path of the operation’s server, such as
/v1forhttps://api.example.com/v1, or under the Base path you give. The server’s host is ignored and never called. - An update or a restore starts from the Base path the active revision’s mocks are served under, so they stay where they are unless you change it.
An exact path always answers before a pattern, so GET /users/me answers its own requests even when GET /users/{id} was imported first.
Your edits and later versions
Section titled “Your edits and later versions”MockHub remembers what each imported mock, and each of its responses, looked like when it generated it. When you import a later version of the document, it compares three versions of the mock’s shared fields, and of each of its responses: that remembered version, what is there now, and what the new document generates.
| You changed it | The document changed it | The update |
|---|---|---|
| No | No | Leaves it |
| No | Yes | Takes the document’s version |
| Yes | No | Keeps your edit |
| Yes | Yes | Stops and asks: Keep my value or Use specification value |
The preview also shows what changed in the document itself, operation by operation, and how likely each change is to break a caller. A renamed path parameter, such as {id} to {orderId}, is the same operation, so its mocks and links stay.
A response whose example is gone from the new document stays as it is unless you delete it in the preview, and so does a mock whose operation is gone. The default response is never moved by an update, and a response you added by hand to an imported mock is never changed. A mock you wrote by hand and linked to an operation is never changed or deleted by an import.
The preview checks every mock the import leaves linked to an operation against the new document, as the apply would leave it: those it creates or updates, the edits it keeps, and the mocks it does not touch. A mock that breaks the document, such as one answering an error on purpose, or one that could not be checked in full, such as a templated response, is applied only once you say so. Changes to an operation’s security requirements are listed with the other changes; the contract describes authentication, and nothing enforces it on requests to your mocks.
Which example answers
Section titled “Which example answers”An operation can have several examples, such as a 200 and two 422s. An import makes one mock of the operation and one response in it for each example you choose, with one of them the default. They all answer the same requests, and the default answers unless a request asks for another with X-MockHub-Response. Make default in the operation’s Responses changes the default in one step, without touching the mock’s activation or any other mock, and Copy request copies a curl command with the header that selects the response when MockHub can build a request that matches the mock. When it can’t build the command, it copies the X-MockHub-Response header line for you to add to your own request. A mock you linked to the operation by hand is a mock like any other. See Choose the response that answers.
A response variant is not an OpenAPI response status: two 422 examples are two responses, each under a key of its own, and a 200 in two media types is two as well. See the glossary.
Checking a response
Section titled “Checking a response”Preview and validate in Try it renders the response you are editing and checks the response against its linked operation: the status, the content type, the declared headers and the body against its schema. It reports Valid, Invalid with each finding located in the body and in the document, Incomplete when part of the response could not be checked, or Not checked when the request or response does not belong to the operation.
A result is about one response, to one request, at one revision. It says nothing about other requests, other steps of a sequence, or what a template renders next time.
A schema is read by the rules of the OpenAPI version the document declares, never as a neighbouring version. In a 3.0 document, nullable: true adds null to the type written beside it and nothing more: an enum that does not list null still refuses it, and nullable without a type, or beside a $ref or an allOf alone, has no effect, as 3.0.3 says. In a 3.1 or 3.2 document, null is allowed by listing "null" in type; nullable there is a leftover from 3.0 that allows nothing, and the import preview and every check say so. A discriminator guides which schema a response is filled from, and nothing else: a response is held to oneOf as JSON Schema reads it, so a response that fits two overlapping branches fails whatever "kind" says. A property marked writeOnly may be missing from a response even where it is required. additionalProperties: false closes exactly the schema that declares it: in an allOf of closed parts, a property one part declares is refused by the other, and a response must satisfy every part as written. A keyword JSON Schema does not define is ignored, as JSON Schema says, so a misspelt one such as maxLenght is not caught. A 3.0 schema written with a keyword only later versions define, such as const, a list of types or a boolean schema, is still imported, and a check that reaches the keyword is Incomplete, with the keyword named.
Schemas are read in JSON Schema 2020-12 and the OpenAPI dialects built on it. A schema whose $schema, or a document whose jsonSchemaDialect, names an earlier draft (draft-04, -06, -07 or 2019-09) is imported, and every check that reaches it is Incomplete, naming the dialect, because MockHub checks schemas by the 2020-12 rules only. Forms that older drafts and Swagger 2.0 used do not gain their old meaning: dependencies, additionalItems and x-nullable assert nothing in 2020-12 and are reported as legacy keywords; a list under items (a draft-07 tuple, written as prefixItems since 2020-12), exclusiveMinimum: true in a 3.1 document, required: true on a property and type: file are malformed forms of known keywords, reported where they are, and a check that reaches one is Incomplete rather than a guess. In a 3.0 document, exclusiveMinimum: true beside minimum is 3.0’s own form and is read as such. A few named formats keep a tolerant reading, such as a date-time with no UTC offset, reported as a tolerated value. Nothing else is read leniently.
Check against the contract on a request log checks a response that already went out: a mock’s, or your upstream’s in a capture, which shows whether your real API still keeps the contract. The operation is the one the answering mock is linked to, or else the one whose path matches the request, and you can choose another. The log keeps the response as logging stored it, so a body cut short or redacted, or a header redacted, is never passed; see Check a request log against the contract.
Many logs can be checked at once, up to 500 from a period of traffic, against one revision or against two to see how each verdict moves from one to the other; see Check request logs in bulk. Before you import a document, the preview can check your newest logs against it, beside the active revision, without writing anything.
Background checks
Section titled “Background checks”An Admin can have MockHub check a small sample of the project’s new responses against the active revision every 15 minutes, from your upstream, your mocks or both. The findings are the project’s contract health on Insights, and an alert rule can count them. They are a sample, never a rate over all traffic; see Watch for contract drift in the background.
Coverage
Section titled “Coverage”Each check of a request log is kept as that log’s latest check against its revision, for as long as both are. The Coverage view of Operations reads them: for each operation of a revision, the mocks linked to it now, the latest check of a mock’s answer and of an upstream capture, and each example with the mocks imported from it. A check made by an older checker, or of a mock edited since, says so. See See what your checks cover.
Limits
Section titled “Limits”- One document per project, in one self-contained file. Swagger 2.0, references to other files or URLs, and examples given by
externalValueare refused or left out. - Imports and checks need a plan that includes OpenAPI. The document size and the number of operations are plan limits, and revisions count towards the workspace’s configuration storage; see the limits page. After a downgrade, an existing contract stays readable.
- Responses are checked when you ask, in Try it or from a request log, and in a sample by background checks when an Admin turns them on. Requests are checked as they arrive only where Request checks is set to Report or Reject.
- XML bodies, schemas for other media types, and headers without a single type are reported as Incomplete, never Valid.
- A configuration document and a reusable pack carry the contract, each mock’s link and its import history, so a copied project’s next import keeps what you customized. Applying a changed contract through a document creates the next revision without regenerating any mock.
- AI assistants connected over MCP can preview and apply an import, check request logs against one or two revisions, check requests they are given, and read coverage and contract health. They cannot check a document you have not imported or a response that was not logged.
- A latest check is the newest check of one logged request. Coverage shows where traffic was checked, not that every request, example or status of an operation was.