Skip to content
gmarchaPublic

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

apidiff

Detect value-level behavioral changes between two versions of an API — the kind that types, schemas, and mocks miss — then find and verify their impact in real consumer code.

Proof-of-concept. The worked example is Elasticsearch 6.8 → 7.17, but the approach applies to any versioned HTTP API (e.g. Stripe's dated API versions).

The problem

When an API provider ships a new version, consumers break in ways that a type checker or an OpenAPI diff will never catch — because the shape of a value changed, not the presence of a field.

The canonical example: in Elasticsearch 7.0, a search response's hits.total changed from a number to an object { value, relation }.

// works on 6.x, silently broken on 7.x — the field still exists, the request still 200s
const count = response.hits.total;   // was 42, now { value: 42, relation: "eq" }

Types don't help (the client types are generic), the endpoint is unchanged, and mocks encode the old behavior. The break only shows up against a real new-version response.

How it works

apidiff has two halves joined by a catalog.

  PRODUCER  (once per version pair)              APPLICATOR  (per repo)
  ┌───────────────────────────┐                 ┌──────────────────────────────┐
  │ replay an operation-surface│    catalog      │ CodeQL: which response fields │
  │ corpus against old + new   │ ──────────────► │ this repo reads (through      │
  │ → diff → classify          │  (cached JSON)  │ wrappers) ∩ catalog → impact  │
  └───────────────────────────┘                 │ → replay red→green → fix      │
                                                 └──────────────────────────────┘
  1. Producer — replays a corpus of representative requests (built from the API's operation surface, not from the changes we expect to find) against both versions, diffs the responses, and classifies each value-level change. The result is a generated catalog of behavioral changes. Two mechanisms keep it honest:

    • a self-calibrating volatility mask — each request is replayed K times against one version; anything that varies (timestamps, took, shard counts) is masked as non-deterministic;
    • a semantic diff that is array-schema aware and collapses a scalar→object change (like hits.total) into a single entry instead of a pile of add/removes.
  2. Analyzer — uses CodeQL to find which response fields a codebase actually reads, resolved interprocedurally — the read is caught even when it lives in a different function/file than the search() call, reached through both await and .then(cb).

  3. Impact — intersects the catalog with the fields a repo reads. A change matters to a repo only if the repo reads the path that changed. This turns a generic version-diff into the specific, small set of call sites that will actually break.

  4. Verifier — replays the same request against both versions and shows the break (red) and that the mapped fix restores the old shape (green), against live clusters — proof, not a hopeful edit.

Results on the Elasticsearch 6.8 → 7.17 example

Generated catalog (discovered blind — no change was hand-listed):

change endpoint path fix
type-changed POST /{index}/_search hits.total hits.total.value
type-changed POST /{index}/_search hits.max_score guard for null
type-changed GET /{index}/_mapping {index}.mappings._doc mappings (no type nesting)
  • Recall: 3/3 of the replay-discoverable known changes, rediscovered blind. The remaining known changes are an explicit coverage boundary: track_total_hits (needs >10k documents) and _all (a mapping-time removal).
  • Determinism: 0 false positives replaying each version against itself across 13 operations.
  • A folklore correction: the answer key initially listed _type removal as a 6→7 change. Replay didn't flag it; checking the live clusters, _type: "_doc" is present in both 6.8 and 7.17 — the removal is actually a 7→8 change. The generated result was right; the hand-written expectation was wrong.

Impact + verification on real repos:

  • The analyzer resolves hits.total through a wrapper library (a search() call in one file, the field read on a parameter in another).
  • Verifier, against live ES: hits.total = 3 [number] on 6.8 → { "value": 3 } [object] on 7.17 (red) → hits.total.value = 3 [number] (green).

Quickstart

Requirements: Node ≥ 20, Docker, and the CodeQL CLI (for the analyzer step).

# 1. bring up both Elasticsearch versions (localhost:9268 = 6.8, localhost:9271 = 7.17)
docker compose up -d

# 2. generate the catalog and score it
node bin/produce.mjs

# 3. sanity: the determinism check must report 0 false positives
node bin/determinism.mjs

# 4. red -> green proof against the live clusters
node bin/verify.mjs

Analyzer + impact on a repo (needs the CodeQL CLI on your PATH or at tools/codeql/):

# build a CodeQL database for a JS/TS repo
codeql database create dbs/<repo> --language=javascript --source-root=path/to/repo

# extract the fields it reads, then intersect with the catalog
node src/analyze.mjs <repo> dbs/<repo>
node bin/impact.mjs <repo>

If your clusters run elsewhere, set ES_HOST (e.g. ES_HOST=192.168.1.10 node bin/produce.mjs).

Layout

src/
  engine.mjs          differential-replay engine (mask + semantic diff)
  corpus/             per-API operation-surface corpus + version-equivalent setup
  oracle/             answer key used only to score recall (never an input)
  changelog.mjs       optional human-readable fix mappings (decoration only)
  producer.mjs        generates the catalog
  analyze.mjs         runs the CodeQL query -> UsageSet
  impact.mjs          catalog ∩ UsageSet
  verify.mjs          red -> green check
analyzer/
  EsResponseFields.ql CodeQL query: response fields read, through wrappers
bin/                  CLI entry points
catalogs/             generated catalogs (checked in as example output)

Scope and limitations

This is a proof-of-concept, deliberately narrow:

  • Reads only. It replays read requests; mutating operations are out of scope.
  • One example pair. Elasticsearch 6.8 → 7.17, JavaScript/TypeScript consumers.
  • Corpus-bounded catalog. The catalog contains the behaviors the corpus exercises; input-conditional changes are an explicit boundary, caught per-repo by replaying that repo's own requests.
  • Not a schema diff or a codemod. Those are commodities; the point here is generated behavioral changes intersected with actual field reads and verified fixes.

License

MIT — see LICENSE.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages