Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Contract rule catalog

rustman contract diff compares two OpenAPI 3.0/3.1 documents and reports every difference with one of the rules below. rustman contract rules prints the same list (--json for a JSON array).

Catalog version: 2. The version is recorded in every diff and changes whenever a rule is added, removed or reclassified.

How differences are classified

Documents are compared after loading them as OpenAPI import does: references bundled, OpenAPI 3.0 schemas rewritten to 3.1 (nullable: true is the same as a null type) and path-item parameters merged into each operation. Operations are matched by method and path template, ignoring parameter names (/users/{id} and /users/{userId} are the same operation, reported as path-parameter-renamed). Path parameters are matched by position, other parameters by name and location (header names ignore case).

Schemas are compared with a direction:

  • Request schemas (parameters and request bodies) break clients when they get stricter: a new required field, a narrowed enum, a tighter constraint, a type that rejects old values.
  • Response schemas break clients when they lose guarantees: a removed field, a field that is no longer required, a type that accepts new kinds of values, a narrowed enum. A widened response enum or a looser response constraint may surprise clients and is unknown.

$refs are followed (each difference points to where the change is, such as /components/schemas/User/properties/email), and recursive schemas are compared once per level. The members of an allOf add up to one object: their properties, required lists and types are compared like those of any object, so moving a field between members, or wrapping a schema in an allOf, is not a change, and a field that disappears with a removed member is. What else an allOf member says (constraints, enums, nested oneOf), and any change inside oneOf, anyOf and not, is not proven equivalent or compatible: it is reported as composition-changed (unknown).

Response headers are compared like parameters, by name (ignoring case), and their schemas in the response direction.

items and additionalProperties are constraints too. Absent, true and an empty schema accept anything, false accepts nothing, and a schema is in between: a move towards false (additionalProperties absent -> {type: integer} -> false, or items appearing) is stricter, a move the other way looser, and two schemas are compared member by member. additionalProperties: false in an allOf member closes the whole object. multipleOf is compared by exact decimal divisibility: 10 -> 5 is looser (every multiple of 10 is a multiple of 5), 5 -> 10 stricter, and 4 -> 6 both, so it is breaking in a request and unknown in a response. Values too large or too precise to compare exactly are constraint-changed.

A removed response status that the new document still covers is not response-status-removed: OpenAPI lets a range (1XX to 5XX) stand for every code of its class that is not declared, and default for every status that is not declared otherwise. So when 200 gives way to 2XX (or, without 2XX, to default), and when a range gives way to default, the old response is compared with the one that now covers it, as response 200 (now 2XX).

Each difference carries a JSON pointer into each document. When an element exists in one document only, the pointer into the other is the closest enclosing element (for a removed operation, its path item or /paths).

Breaking

RuleDescriptionExample
operation-removedAn operation is removed.DELETE /users/{id} is in the old document only
response-status-removedA response status is removed from an operation.GET /users/{id} no longer declares 404
response-media-type-removedA response media type is removed.the 200 response drops application/xml
response-field-removedA response field is removed or renamed.$.email disappears from the 200 body
response-field-became-optionalA response field is no longer required.email leaves the required list of the 200 body
response-field-type-changedA response field’s type changes or accepts more types.$.id goes from integer to string
response-header-removedA response header is removed.the 200 response no longer declares X-Rate-Limit
response-header-became-optionalA response header is no longer required.X-Rate-Limit changes to required: false in the 200 response
response-enum-narrowedValues are removed from a response enum.$.status goes from [active, suspended, deleted] to [active, suspended]
request-required-field-addedA request body field becomes required, or a new required field appears.email is added to the required list of the POST /users body
request-field-removed-from-closed-schemaA request body field is removed from a schema with additionalProperties: false; clients that still send it are rejected.POST /users no longer declares nickname and allows no other properties
request-required-parameter-addedA new required parameter appears.GET /users gains a required query parameter tenant
parameter-became-requiredAn optional parameter becomes required.query parameter limit changes to required: true
request-body-became-requiredA request body becomes required, or a new required body appears.PATCH /users/{id} changes its body to required: true
request-media-type-removedA request body media type is removed.POST /users no longer accepts application/x-www-form-urlencoded
request-type-changedA request field’s type changes to one that rejects old values.$.age goes from [integer, string] to integer
request-enum-narrowedValues are removed from a request enum.query parameter sort goes from [asc, desc] to [asc]
request-constraint-tightenedA request constraint gets stricter (maxLength, maximum, pattern, minItems, additionalProperties, items, …).$.name goes from maxLength: 100 to maxLength: 50
parameter-location-changedA parameter (or an API key) moves to another location.api_key moves from a query parameter to a header
security-requirement-removedEvery security alternative an operation accepted is removed.GET /users accepted basicAuth and now accepts only bearerAuth

Non-breaking

RuleDescriptionExample
operation-addedAn operation is added.GET /health is in the new document only
operation-deprecatedAn operation is marked deprecated.GET /users/{id} gains deprecated: true
path-parameter-renamedA path parameter is renamed; the path template is the same./users/{id} becomes /users/{userId}
response-status-addedA response status is added to an operation.GET /users/{id} declares a new 429
response-media-type-addedA response media type is added.the 200 response adds application/xml
response-field-addedA response field is added to a schema that allows other properties.$.nickname appears in the 200 body
response-field-became-requiredA response field becomes required.nickname joins the required list of the 200 body
response-header-addedA response header is added.the 200 response declares a new X-Request-Id
response-header-became-requiredA response header becomes required.X-Request-Id changes to required: true in the 200 response
response-type-narrowedA response field accepts fewer types.$.nickname goes from [string, null] to string
response-constraint-tightenedA response constraint gets stricter.$.name goes from maxLength: 100 to maxLength: 50
request-optional-field-addedAn optional request body field is added.POST /users accepts an optional nickname
request-optional-parameter-addedAn optional parameter is added.GET /users gains an optional query parameter page
request-field-became-optionalA request body field is no longer required.email leaves the required list of the POST /users body
parameter-became-optionalA required parameter becomes optional.header X-Tenant changes to required: false
request-body-addedAn optional request body is added.DELETE /users/{id} accepts an optional JSON body
request-body-became-optionalA required request body becomes optional.PATCH /users/{id} changes its body to required: false
request-media-type-addedA request body media type is added.POST /users also accepts application/x-www-form-urlencoded
request-type-widenedA request field accepts more types.$.age goes from integer to [integer, string]
request-enum-widenedValues are added to a request enum.query parameter sort goes from [asc] to [asc, desc]
request-constraint-loosenedA request constraint gets looser.$.name goes from maxLength: 50 to maxLength: 100
security-alternative-addedA security alternative is added to an operation.GET /users also accepts apiKey
docs-changedOnly documentation changes (summary, description, title, examples).the GET /users description is reworded

Unknown

RuleDescriptionExample
response-enum-widenedValues are added to a response enum; clients may not handle them.$.status goes from [active] to [active, archived]
response-constraint-loosenedA response constraint gets looser; clients may not handle the new values.$.name goes from maxLength: 50 to maxLength: 100
constraint-changedA constraint changes in a way that cannot be ordered exactly as stricter or looser.$.amount goes from multipleOf: 3 to multipleOf: 1e300
response-field-added-to-closed-schemaA response field is added to a schema with additionalProperties: false.$.nickname appears in a 200 body that allowed no other properties
request-field-removedA request body field is removed; clients may still send it.POST /users no longer declares nickname
parameter-removedA parameter is removed; clients may still send it.GET /users no longer declares the query parameter page
request-body-removedA request body is removed; clients may still send one.DELETE /users/{id} no longer declares a body
security-alternative-removedSome, but not all, security alternatives of an operation are removed.GET /users accepted basicAuth or bearerAuth and now only bearerAuth
composition-changedA oneOf, anyOf, allOf or not composition changes.a 200 body goes from oneOf: [A, B] to oneOf: [A, B, C]
vendor-extension-changedAn x- vendor extension changes.x-rate-limit goes from 100 to 50