Skip to content

reflectapi diff: report schema changes that break clients - #256

Draft
akozlev wants to merge 1 commit into
thepartly:mainfrom
akozlev:angel/schema-diff
Draft

akozlev wants to merge 1 commit into
thepartly:mainfrom
akozlev:angel/schema-diff

Conversation

@akozlev

@akozlev akozlev commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Adds reflectapi diff OLD NEW, which reports the changes between two reflectapi.json schemas that break existing clients, so CI can catch a breaking API change before it ships.

The problem

A committed reflectapi.json shows that the contract changed, but not whether the change breaks clients. Running an OpenAPI diff tool such as oasdiff on the generated openapi.json works poorly. The OpenAPI output wraps generics, flattened fields and tagged enums in allOf/oneOf, so a pure restructure reads as removed properties. A description edit can reorder untitled oneOf branches, and a reflectapi upgrade that changes only the generated OpenAPI reads as a break. Real breaks inside a oneOf are reported only as warnings, or not at all.

What it does

Schemas are compared function by function, in both directions:

  • Requests break when the server stops accepting what an old client sends: a required field added, a field becoming required or non-nullable, a variant removed, a narrower number or string.
  • Responses break when an old client can no longer read what the server sends: a required field removed, a field becoming optional or nullable, a new variant on a closed enum, a wider number or a looser string.
  • Functions break when removed, moved to another path, no longer readonly (so no longer accepting GET), dropping a serialization format, or switching between a single response and a stream.

Types are compared by the JSON they read and write, not by name. Generic arguments are substituted, transparent wrappers unwrapped and flattened fields inlined, so renaming or restructuring a type without changing its JSON is compatible. It also follows serde where the schema alone would mislead:

  • Optional request fields: a request may leave out an Option field, which serde reads as None, unless the field has its own deserialize_with.
  • Flattened maps: they collect leftover keys, so they're empty, never missing.
  • Internally tagged unit variants: one is an object holding only its tag, so giving it fields only adds them.
  • Open enums: an #[reflectapi(open)] enum, or one with a #[serde(other)] variant, can gain response variants.
$ reflectapi diff old.json new.json
2 breaking change(s):
  pets.list output body.items[].kind::fish: variant added, old clients can't read it
  pets.list output body.items[].updated_at: required field removed without deprecation

To accept these, add the lines below to an acknowledgement file passed with --acknowledged, …

- pets.list output body.items[].kind::fish [variant-added]: <reason, consumers checked, ticket>
- pets.list output body.items[].updated_at [required-field-removed]: <reason, consumers checked, ticket>

Removing deprecated items

Removing something clients rely on (a function, a required response field, a request variant, or a field of a request struct with #[serde(deny_unknown_fields)], which the schema now records) is compatible once it was deprecated and its sunset has come:

  • --today DATE: the date removals are judged on. Defaults to today in UTC.
  • --min-notice-days N: also requires deprecated_since to be at least N days earlier.
  • --deprecated-in SCHEMA: also requires the item to have been deprecated already in an earlier schema, such as the default branch a week back. Unlike deprecated_since, that can't be backdated in the same change that removes the item. Fields and variants are matched by the type that declares them, so a route added since can still remove them.

For CI

  • Stable IDs: each change has a kind (required-field-removed, variant-added, …) and a key, <function> <location> [<kind>]. A type used in several places is reported at each, so a key doesn't move when another use is added.
  • --acknowledged PATH: files, or directories of .md files, of - <key>: <reason> lines that accept known breaks. A malformed line, or one that still holds the placeholder, is an error; an acknowledgement that matches nothing is a warning.
  • --warn KIND: reports a kind of break as a warning, for a pipeline whose policy allows it, such as new response variants.
  • --include-tags / --exclude-tags: choose functions as codegen does, so a diff can match a published spec.
  • --json: every change with its severity, kind, key and deprecation dates.
  • Exit codes: 0, 1 for an unaccepted break, 2 when it can't diff.

The same check is available as a library, reflectapi_schema::diff (diff, parse_acknowledgements, acknowledge).

Testing

  • Derive-based cases in reflectapi-demo/src/tests/diff.rs: real types with the derives and inline insta snapshots, covering optional and defaulted fields, nullability, flattened structs, enums and maps, tagged and open enums, generics, newtypes, uuid, numbers and deprecation.
  • Library tests in reflectapi-schema/tests/diff.rs for what the derives can't express directly: route changes, bodies and headers, primitive fallbacks and formats, removal rules, acknowledgements, tags and --warn.
  • CLI tests run the binary and check exit codes, warnings, acknowledgements and JSON.
  • Comparison with oasdiff: I compared it with oasdiff on the OpenAPI generated from the same schemas, over a long history of real schema changes. It agreed on the real breaks, had none of oasdiff's false positives (the allOf/oneOf restructures, description-only edits and codegen-only changes above), and caught real breaks oasdiff missed: a struct replacing a tagged enum, a response field becoming optional, and a request field becoming required inside a oneOf.

Caveats

  • Untagged enums are matched by variant name, not by shape.
  • Header names are compared case-sensitively.
  • Not yet: the CLI has no docs page in the book.

🤖 Generated with Claude Code

https://claude.ai/code/session_01B5dwkNALJW4jaXoLBCzp5A

`reflectapi diff OLD NEW` compares two schemas function by function, in
both directions: a request breaks when the server stops accepting what an
old client sends, a response when an old client can no longer read what
the server sends. Types are compared by the JSON they read and write, not
by name: generic arguments are substituted, transparent wrappers unwrapped
and flattened fields inlined, so renaming or restructuring a type without
changing its JSON is compatible. It follows serde where the schema alone
would mislead, such as a request leaving out an `Option`, a flattened map
being empty, or an internally tagged unit variant gaining fields.

Removing a deprecated item is judged by its sunset (`--today`), optionally
by how long it has been deprecated (`--min-notice-days`) and by an earlier
schema in which it must already be deprecated (`--deprecated-in`), which
can't be backdated as `deprecated_since` can.

Each change has a stable kind and key, so CI can accept a known break with
a `- <key>: <reason>` line in a file passed to `--acknowledged`, and
downgrade kinds of break to warnings with `--warn`. The command exits 1 for
an unaccepted break and 2 when it can't diff. The same check is
`reflectapi_schema::diff`.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant