Executable API Contracts and Compatibility Testing
Define API behavior with OpenAPI, verify consumer expectations against providers in CI, and gate releases on compatibility evidence before deployment.
An OpenAPI description records the HTTP interface, while consumer-driven tests capture the interactions clients actually depend on. This guide shows how to validate both sides in CI and check known provider-consumer versions before deployment.
Executable API Contracts and Compatibility Testing
Introduction
When two services release independently, an endpoint’s request and response shape becomes a shared promise. A field removal, changed status code, or new authorization requirement can break a consumer that the provider team never runs locally.
An OpenAPI description makes the HTTP interface reviewable and machine-readable. Contract tests check selected consumer-provider interactions against real implementations. Together, they catch many compatibility problems before deployment; neither one replaces tests for business behavior across the whole system.
What the checks cover
The OpenAPI Specification describes an HTTP API in a format that people and tools can inspect. A parser or linter can check that the description follows the specification. A runtime validator can also compare requests or responses with the schema when it is wired into an application test. The document alone does not prove that the service implements it.
Consumer-driven contract tests answer a narrower question: does the provider still support the requests and responses a consumer actually uses? Pact recommends writing those tests around the consumer’s API client and keeping expectations as loose as the consumer permits. That makes a contract useful without turning every provider detail into a permanent promise. See the Pact consumer testing guide.
These checks have different jobs:
| Check | What it can catch | What it does not establish |
|---|---|---|
| OpenAPI lint or validation | Missing or malformed interface details in the description | That the running service behaves as described |
| Consumer contract verification | A mismatch in an interaction the consumer recorded | Every business rule or every possible request |
| Provider integration tests | Provider behavior with its own dependencies | That all consumers can use the provider |
| End-to-end tests | Selected flows across deployed components | Exhaustive compatibility for every version pair |
Keep the layers that answer your risks. A contract check is not a cheap substitute for the rest of the test suite; it gives a fast, focused signal about an API boundary.
Describe one operation with OpenAPI
This OpenAPI 3.2.1 excerpt describes reading an order. The openapi field identifies the description format. info.version is the version label for this API description, distinct from the OpenAPI Specification version and from the running implementation version, as defined by the specification.
openapi: 3.2.1
info:
title: Orders API
version: 1.4.0
paths:
/orders/{orderId}:
get:
operationId: getOrder
parameters:
- name: orderId
in: path
required: true
schema:
type: string
responses:
"200":
description: Order found
content:
application/json:
schema:
$ref: "#/components/schemas/Order"
"404":
description: No order exists with this ID
components:
schemas:
Order:
type: object
required: [id, status, totalCents]
properties:
id:
type: string
status:
type: string
totalCents:
type: integer
minimum: 0
Keep this description next to the provider code and validate it in the provider’s CI. Review the required fields, error responses, authentication rules, and any headers consumers need. A generated client or mock can help teams work from the same description, but generation by itself does not verify the running endpoint.
The example does not make every response property required. That leaves room for the provider to add data without claiming every client handles every possible addition. A consumer contract test should assert only the fields and behavior its client actually uses.
Provider and consumer checks in CI
For a consumer-driven workflow, the consumer tests its API client against a mock provider and publishes the resulting contract. The provider then fetches the contracts it needs to support and verifies them against a local instance in its own CI. Pact recommends this local verification; a shared deployed provider makes tests harder to control and can slow feedback. The provider publishes verification results, and a deployment check compares the candidate version with the application versions recorded in the target environment.
flowchart TD
C[Consumer CI: test API client against mock] -->|publish versioned contract| B[Pact Broker]
B -->|fetch contracts for known consumers| P[Provider CI: start provider locally with test states]
P --> V[Verify recorded interactions]
V -->|publish verification result| B
B --> D[Check candidate against versions in target environment]
D -->|compatible| R[Allow deployment]
D -->|incompatible or unknown| H[Hold deployment and review]
Keep the consumer test close to the client boundary. It should send the request the client really constructs and check the response details the client reads. Avoid asserting provider internals or unrelated business rules. On the provider side, use controlled provider states and stub downstream services where needed, while exercising the request parsing and response code covered by the contract. Pact’s provider verification guidance recommends running verification against a local provider instance in CI and publishing the result.
The Broker is a registry for contracts and verification results in this example. It can track which application versions have been verified together. Pact’s can-i-deploy guidance describes using that matrix to decide whether a candidate is compatible with the versions recorded in an environment. Treat an unknown combination as unverified, not as a pass. This check covers known contracts and recorded deployments; it cannot account for an unregistered consumer.
Review changes through compatibility tests
Do not classify every schema edit as safe or breaking by slogan. Ask which requests consumers send, what responses they read, and which behavior they rely on. Then run the corresponding contracts.
| Provider change | Consumer risk to check |
|---|---|
| Remove or rename a response field | A client that reads that field may fail or lose data |
| Change a field’s type or meaning | Deserialization may fail, or a client may interpret a valid value differently |
| Add a response field | Often tolerated by clients, but strict parsers or generated clients may reject it |
| Change a status code, error body, or auth requirement | Client branching, error handling, or permissions may no longer match |
| Add an optional request field | Existing clients may continue working, but provider defaults and validation still need tests |
These are risk prompts, not a universal compatibility oracle. Static schema-diff tools can flag changes for review, while consumer contracts show whether specific recorded interactions still match. Add provider integration tests for behavior the contract does not cover.
Keep versioning and deprecation in context
Version labels help organize incompatible changes, but a new version number does not make a rollout safe by itself. Run compatibility checks for the versions that will coexist, and give consumers a migration path when behavior must change. For versioning approaches and their routing tradeoffs, continue to API Versioning Strategies.
When an older operation or version needs to go away, identify its consumers, communicate a timeline, and verify migration before removal. The focused guide to API Deprecation, Migration, and Change Communication covers that lifecycle.
A practical CI checklist
- Keep the OpenAPI description with the provider code and validate it on each change.
- Test consumer API clients against only the interactions they depend on.
- Publish versioned contracts so provider CI can retrieve the right expectations.
- Verify contracts against a controlled local provider with explicit test states.
- Publish verification results and gate deployment against versions in the target environment.
- Keep integration and end-to-end checks for behavior that crosses multiple boundaries.
- Treat missing verification data as unknown; investigate before relying on it.
Common failure cases
The contract is green, but the endpoint is wrong. A static OpenAPI linter only checks the description. Add runtime request/response validation or provider tests that exercise the implementation.
The consumer test asserts fields the consumer never reads. That makes harmless provider changes fail verification. Keep assertions tied to client behavior, then add a contract only when the consumer depends on another field.
A provider passes against one contract but deploys beside an untested consumer version. Check the compatibility matrix for the target environment and record deployments accurately. A passing result for one pair is not evidence for every pair.
A team removes a version because the replacement passed tests. That says nothing about whether all consumers migrated. Track usage and follow the migration and communication steps in the deprecation guide before removal.
Interview questions
Conclusion
OpenAPI documents the interface; consumer and provider tests check selected interactions; deployment compatibility checks use the versions that will actually coexist. Keep each check connected to its evidence, and use the dedicated versioning and deprecation guides when a contract must evolve or retire.
Category
Related Posts
Consumer-Driven Contract Testing for Backend Services
Learn how consumers define API expectations and providers verify them in CI, with practical workflows, failure modes, and deployment safeguards.
OpenAPI and Machine-Readable API Specifications
Learn how OpenAPI turns API behavior into a reviewable contract, supports tooling, and helps teams catch breaking changes before clients encounter them.
GraphQL Federation: Subgraphs, Routing, and Trade-offs
Learn how GraphQL federation composes subgraphs, routes operations, and affects service ownership, query planning, latency, failure handling, and authorization.