Unknown fields and new enum values: test that your API can evolve

Most "breaking changes" that break production aren't renamed fields. They're additions: a new status value that a client's switch statement doesn't handle, or a new required-looking field that a strict client rejects. Additions are supposed to be safe. Making them safe takes a couple of decisions, and tests that hold you to them.

Enums in responses

An order has a status. Today it's one of pending, paid, shipped, cancelled. Pin that down with a schema assertion on the request's Tests tab (source JSON body, path $, matches JSON schema):

JSON
{
  "type": "object",
  "required": ["status"],
  "properties": {
    "status": { "enum": ["pending", "paid", "shipped", "cancelled"] }
  }
}

When someone adds refunded, this test fails. That's the point: it's the moment to check the API docs, the changelog and the clients, before the value shows up in production. Update the schema in the same pull request as the new value.

For lists, check the enum on every item:

JSON
{
  "type": "object",
  "properties": {
    "data": { "type": "array", "items": { "properties": { "status": { "enum": ["pending", "paid", "shipped", "cancelled"] } } } }
  }
}

The enum is the same everywhere

The same field often appears in several endpoints: the order, the order list, the customer's orders, a webhook payload. Put the schema on the folder that holds them, so every request inherits it. A folder's assertions apply to every HTTP and GraphQL request inside it, subfolders included, and the request's Tests tab shows them read-only with their source.

Unknown fields in requests

Send a body with a field the API doesn't know:

HTTP
POST {{baseUrl}}/orders
Content-Type: application/json

{"productId": "{{productId}}", "quantity": 1, "giftWrap": true}

There are two defensible behaviours:

  • Ignore it: 201, and giftWrap is not stored or echoed. Lenient, friendly to clients that are newer than the server.
  • Reject it: 400 naming giftWrap. Strict, catches client bugs early.
JavaScript
// If your API ignores unknown fields
pm.test('unknown field is accepted', () => pm.response.to.have.status(201));
pm.test('unknown field is not stored', () => {
  pm.expect(pm.response.json()).to.not.have.property('giftWrap');
});
JavaScript
// If your API rejects them
pm.test('unknown field is rejected', () => pm.response.to.have.status(400));
pm.test('error names the field', () => pm.expect(pm.response.text()).to.include('giftWrap'));

The behaviour to catch is the third one: the field is stored in a JSON column and comes back later, so clients start depending on something that was never part of the API.

Typos

Lenient APIs have one sharp edge. A client sends {"quantitiy": 5}, the typo is ignored, and the default quantity of 1 is used. The order succeeds with the wrong quantity.

HTTP
POST {{baseUrl}}/orders
Content-Type: application/json

{"productId": "{{productId}}", "quantitiy": 5}

If quantity is required, this must be a 400 ("quantity is required"). If it has a default, consider rejecting unknown fields on this endpoint at least.

Unknown enum values in requests

HTTP
PATCH {{baseUrl}}/orders/{{orderId}}
Content-Type: application/json

{"status": "teleported"}

That's a 400 listing the allowed values, never a 500 from a database constraint, and never a 200 that stores it.

Clients: test that you tolerate additions

If you maintain a client (an SDK, a mobile app, a service that calls another team's API), point it at a mock server whose saved example has an extra field and a new enum value. A client that crashes on either will crash the day the real API adds one.