Skip to content

Lesson 3 of 9 · 27m

Compatibility Rules and Contract Tests That Catch Drift

Define additive-change rules, tolerant-reader expectations, enum policy, serialization guarantees, and automated contract tests before migration pressure arrives.

Most teams discover their compatibility rules after breaking them. A deploy goes out, a partner pager fires, and someone says the field rename was "internal." APIs do not have internal fields once clients can observe them. You need written compatibility rules and executable contract tests that make accidental change visible before release.

Write rules clients can rely on

A compact policy often looks like this:

Compatible: optional request fields, additive response fields, documented extensible enum values, new endpoints and error codes

Breaking: renamed or removed fields; changed types, units, time zones, or meaning; incompatible validation, pagination, filtering, sorting, or idempotency semantics; reused enum values or error codes

Tolerant readers need explicit support

Client SDKs and examples should teach tolerant reading: ignore unknown fields, handle unknown enum values, and avoid depending on ordering unless the contract guarantees it.

type InvoiceStatus = "draft" | "open" | "paid" | "void" | (string & {});
const known = new Set(["draft", "open", "paid", "void"]);
const normalized = known.has(status) ? status : "unknown";

Contract tests belong in CI

Generate snapshots or examples from the public schema and test the implementation against them. For OpenAPI-based services, schema validation catches accidental type drift:

docker run --rm -v "$PWD:/spec" openapitools/openapi-generator:v7.8.0 validate -i /spec/openapi.yaml
pytest tests/test_contract_examples.py

A useful test verifies a documented example response against serializer output:

def test_invoice_detail_matches_contract(client, invoice):
    response = client.get(f"/v1/invoices/{invoice.id}")
    assert response.status_code == 200
    validate(instance=response.json(), schema=invoice_detail_schema)

Schema correctness is necessary but not sufficient. Add semantic tests: cursor stability, default sort order, idempotent replay, error shape, and exact behavior when optional fields are omitted.

For high-value consumers, capture the fields and behavior they truly use with a consumer-driven contract:

{"required":["id","status","total","currency"],"properties":{"total":{"type":"integer"},"currency":{"pattern":"^[A-Z]{3}$"}}}

Diff every release

Automated schema diffs should classify additive and incompatible changes. Human review still matters because type-compatible changes can be semantic breaks. A renamed permission requirement will not appear in a JSON schema diff, but it will absolutely appear in customer production.

Field notes

  • Strict contract tests that forbid all additive fields teach teams to bypass the tests; test the promise, not every byte.
  • Examples rot fastest. Generate them from real serializers whenever possible.
  • A nullable field is a promise. Changing null behavior is often a breaking change.
  • Keep contract tests version-scoped so /v1 cannot silently absorb /v2 behavior.

Was this lesson helpful?

Sign in to rate