Your contract tests pass, the deploy goes out, and every customer sees a balance a hundred times too high.
Consumer-driven contract testing, with tools like Pact, is one of the best things a team can do for independent deployments. Each consumer records what it expects from a provider, and the provider verifies those expectations before it ships.
But contract tests can only check what the contract says. When the contract is built from loose primitive types, a whole class of breaking changes passes straight through. The build is green, and production is broken.
Primitive Obsession at the API Boundary
Primitive obsession means representing domain concepts with raw primitives (string, number) instead of types that carry and enforce their meaning. In code it makes things messy. In an API contract it creates a silent integration trap.
Imagine a billing team that exposes an account balance as a plain number and the currency as a plain string. The checkout team, which consumes the API, writes a Pact test with type matchers:
// Checkout team's consumer test (loose matching)
currency: like('USD'),
balance: like(99.99),
Later, the billing team decides to avoid floating-point rounding by switching the balance from dollars to integer cents: 99.99 becomes 9999.
The billing team runs provider verification against the checkout team’s contract, and it passes. like only checks that the value has the same type as the example, and 9999 is a number, just like 99.99. The change deploys, and every checkout page shows a balance of $9,999.00. The same blind spot would let the currency drift from "USD" to "usd", or to the numeric ISO code "840", without a single failing test.
Type matching does catch structural changes, like a field that is removed or turned into an object. The danger is changes in meaning and format that keep the type intact. Those are exactly the changes that break consumers silently.
The Cure: Semantic Types at the Boundary
The fix is to make the contract say what the data means, not just what type it is. Three practices work together.
1. Use semantic formats in OpenAPI. Don’t declare identifiers and codes as bare strings:
properties:
customerId:
type: string
format: uuid
sku:
type: string
pattern: '^[A-Z]{3}-\d{4}-[A-Z]{2}$'
billingEmail:
type: string
format: email
2. Parse into value objects at the edge. As soon as data crosses into your service, in the controller or gateway, turn raw fields into types like EmailAddress or Sku whose constructors validate the format. Invalid input is rejected right there with a 400 Bad Request and never reaches your business logic.
3. Use semantic matchers in contract tests. Instead of like, use matchers that check format:
// Checkout team's consumer test (strict matching)
customerId: uuid('3f2c6b1e-8a4d-4c1b-9e7f-2d5a6c8b9e01'),
sku: regex('^[A-Z]{3}-\\d{4}-[A-Z]{2}$', 'ELC-1024-US'),
balance: regex('^\\d+\\.\\d{2}$', '99.99'),
Notice that the balance now travels as a string decimal, "99.99". That is what closes the gap. A switch to "9999" no longer matches the pattern, so verification fails before the change ships. Representing money as a validated string, never as a floating-point number, makes a change in meaning visible as a change in shape.
Strict matchers still let the provider evolve. It can add new fields freely, because the consumer never asked for them. What it can no longer do is rename, remove, or quietly reformat the fields consumers depend on.
Wrapping Up
Contract testing gives you confidence only in proportion to how precise your contracts are. Loose primitive types produce loose contracts, and loose contracts produce green builds that hide breaking changes until they reach customers. Model meaning at the boundary: semantic formats in the specification, value objects in the code, and strict matchers in the tests. Then a green build means what you think it means.
This article is adapted from Chapter 6 of my upcoming book, Vertical Velocity: How to Break Silos, Ship Faster, and Reclaim Engineering Joy. The chapter goes further: a central schema registry of reusable types, Spectral rules that enforce them at lint time, and a complete CI/CD pipeline that ties OpenAPI and Pact together. The tested example code is in the book’s companion repository. Follow this blog to hear when it’s out.

No comments:
Post a Comment