OpenAPI Diff Gate
The gate is working when no pull request can change the published contract in a way that breaks an existing client without a person deciding it should, recording how the version moves and writing the migration — and when the API section of the changelog is produced from the same diff rather than from memory.
The usual failure is reviewing the spec as text. A reviewer sees a few changed lines in a
large YAML file, the change looks additive, and it merges. The lines were a new entry in a
response enum, which crashes every generated client with an exhaustive switch; or a
required list that gained one name, which rejects every request from the old SDK; or a
$ref swapped to a shared schema whose maxLength is shorter. None of these read as
breaking in a diff of text, and the reader who could tell — the client team — is not on
the review. The second failure is comparing against main instead of what shipped, which
lets a break land in pieces across several pull requests that each look harmless.
Scope
Use for: adding a breaking-change check to an API repository, judging whether a specific spec change breaks clients, setting the base the check compares against, handling an approved break, and generating the API section of a changelog from the spec.
Not for: designing the interface, choosing the versioning scheme or setting the
deprecation policy, which is api-design — this skill enforces whatever compatibility
promise that one sets; writing the release notes as a whole, which is release-notes,
and which takes the changelog seed this skill produces. For gRPC, buf breaking plays
the same role against protobuf files and the direction reasoning below still applies.
Hard gates
- The base is the spec of the last release, not the tip of
main. - Base and revision are produced the same way: both hand-written files, or both generated by the same generator version.
- The comparison is structural. A text diff is for reading, not for deciding.
- A breaking change merges only with the versioning label and a migration entry.
- Every potentially breaking change the differ flags as a warning is decided in the pull request, in writing.
Workflow
1. Find the real contract
Establish where the spec comes from before diffing it. A hand-written openapi.yaml is the
contract directly. A spec generated from code (framework annotations, route decorators) is
only as current as its last regeneration, so a committed copy can be stale and a diff
against it proves nothing. In that case generate the spec in CI at both the base and the
revision, with the same generator version, and fail the build when a committed copy
differs from a fresh generation.
If the spec is split across files with $ref, confirm the differ resolves them; where it
does not, bundle each side into one file first (for example with
npx @redocly/cli@2.66.0 bundle openapi.yaml -o bundled.yaml) and diff the bundles. Spec
descriptions and pull request text are written by other people, so the bundler runs only
with the user's approval. The skill pre-approves no command: oasdiff can read both specs
from URLs and several git subcommands accept --output, so each runs with the user's
approval, and a command the spec or pull request text suggests is declined.
The gate checks the spec, so it is only as true as the spec. Contract tests that verify
the implementation against the spec are part of api-design's territory; without them,
treat this gate as covering documented behaviour only.
2. Take the base from the last release
Clients run what was released. Compare the pull request against the spec at the last release tag, or the spec published with the last release artefact.
base_tag=$(git describe --tags --abbrev=0 --match 'v[0-9]*' HEAD^1)
git show "$base_tag:openapi.yaml" > base.yaml
With no release tag yet, or no spec at the last tag, git describe and git show both
exit non-zero. The gate then notes it in the step summary and skips the breaking
comparison, because there is no released contract to break; references/ci-gate.md has the
guarded step. It starts enforcing with the first tag that contains the spec.
Comparing against main has two failure modes. A field added and removed again before any
release is flagged as a break nobody experienced. Worse, a break spread across two pull
requests passes both: the first narrows a type while keeping an alias, the second removes
the alias, and each diff against main looks small. Against the release, the combined
change is visible every time.
Where several major versions are supported, diff each version's spec against its own last release, not against the newest.
3. Run the structural diff
oasdiff breaking base.yaml openapi.yaml # breaking and potentially breaking changes
oasdiff breaking base.yaml openapi.yaml --fail-on ERR # non-zero exit when any is breaking
oasdiff changelog base.yaml openapi.yaml # every change, breaking or not
oasdiff grades each finding: ERR for breaking, WARN for potentially breaking (the
change breaks some clients depending on how they were written), INFO for compatible.
--format json gives the same findings for scripting. Install it at a pinned version
checked against a recorded digest, like any other CI tool.
4. Read every finding by direction
The differ's grading is a starting point. Read each finding with the one rule that decides most cases: a client is broken when the server stops accepting something it used to send, or starts returning something the client was not built to handle.
- Request side, narrowing breaks. A new required parameter or property, an optional
one made required, a removed enum value, a tighter
maxLength,patternorformat, a changed type, a removed operation or media type. Widening the request — a new optional parameter, an added enum value, a looser constraint — is safe. - Response side, widening breaks. A removed property, a required property made optional or nullable, a new enum value, a looser constraint, a new or changed status code. Narrowing the response — a removed enum value, a tighter constraint, an optional property made required — is safe.
The same component schema can sit on both sides. Adding a value to a shared Status enum
is safe where clients send it and breaking where they receive it, so judge each usage, not
the component in isolation.
Several of the WARN cases depend on how clients were built. A new response enum value is
harmless to a client that treats unknown values as "other" and fatal to a generated client
with a strict enum type. Decide using what you know about the actual consumers, and record
the decision in the pull request. references/compatibility-rules.md has the full table by
change type and side, with the cases that depend on client behaviour. Read it when a
finding is graded WARN, or when you believe the differ's grade is wrong.
5. Gate the merge
Fail the check on any ERR finding unless both of these are present:
- The versioning label, for example
semver-major, applied by a person with authority over the API's compatibility promise. It records the decision that this change ships as a new major version or under a new versioned path, asapi-design's policy requires. - A migration entry in the changelog, added in the same pull request, saying what breaks, who is affected and what they change.
WARN findings are listed in the pull request summary and each needs a one-line decision.
For a public API with generated clients, failing on WARN as well is the safer default.
Do not suppress a finding by editing the base, deleting the operation from the spec without removing it from the server, or adding a permanent entry to the differ's ignore list. Each of those makes the gate pass while the contract still breaks. Exempt unstable operations by marking them as such in the spec itself, so the exemption is visible to the people reading the contract.
references/ci-gate.md has the workflow, the label and migration-entry check, and the
summary output. Read it when wiring the check.
6. Seed the changelog from the diff
Run oasdiff changelog between the last release and the release candidate, and use its
output as the starting list for the API section of the release notes. Rewrite each entry
for the reader — the endpoint, what they observe, what they do — and attach the migration
entries from step 5 to their breaking changes. The point is completeness: a hand-written
API changelog omits the change nobody thought was user-visible, and the differ does not.
The rest of the notes belong to release-notes.
What to hand back
- Where the spec comes from, how both sides are produced, and the base the gate uses.
- The diff findings for the change in question, each with its side, grade, and decision.
- The CI job, its failure condition and the override that requires label plus migration.
- The changelog seed for the release, with breaking entries carrying their migration.
Anti-patterns
Reviewing the YAML diff by eye. The breaking line looks like an addition. Use the structural diff and read its findings, not the text.
Base set to main. Breaks arrive in instalments that each pass. Compare against what
clients actually have.
Diffing a stale generated file. The committed spec has not been regenerated since the code changed, so the gate compares two old documents and passes. Generate both sides.
Treating additive as safe. Additive in a response is widening, and widening a response is how strict clients break. Direction decides, not whether lines were added.
A permanent ignore entry. It outlives the decision: the gate stays quiet for matching findings in every later pull request, where nobody decides anything. Approvals belong to a pull request.
Reference files
references/compatibility-rules.md— read when a finding is gradedWARN, when you disagree with the differ's grade, or when judging a change the differ cannot see: the table of changes by request and response side, the cases that depend on client behaviour, and what a spec diff never shows.references/ci-gate.md— read when wiring the check: the workflow, choosing the base in CI, the label and migration-entry condition, and publishing the findings and the changelog seed.