contractdiff
Guides

Code generation

contractdiff gen writes code from a contract, one plugin per target: TypeScript types, validators, JSON Schema, fetch, TanStack Query and SWR clients, MSW mocks, NestJS, Express, Fastify and Hono servers, an MCP server and Markdown, with every file recorded in contractdiff.map.json.

contractdiff gen reads contractdiff.config.json, turns the contract into the model every command shares, and runs one generator per target. Each target names its generator and the folder it writes into.

The config

contractdiff.config.json
{
  "input": "new.yaml",
  "targets": {
    "types": { "generator": "types", "out": "src/api" },
    "client": { "generator": "fetch", "out": "src/client" },
    "mocks": { "generator": "msw", "out": "src/mocks" },
    "server": { "generator": "hono", "out": "src/server" },
    "docs": { "generator": "markdown", "out": "docs/api" }
  }
}
fieldwhat it is
inputthe contract, JSON or YAML, relative to the config file
overlaysOpenAPI Overlays applied to the input, in order
pluginspackage names or relative paths of plugins of your own; nothing else is loaded
targets.<name>generator, out (a folder) and the generator's options
extract, diff, lintthe settings of those commands

The config is data, so it validates against a JSON Schema: configSchema, exported from contractdiff. contractdiff.config.mjs and .ts are read too, in that order after the JSON file.

Output of npx contractdiff gen
write docs/api/getPet.md
write docs/api/index.md
write docs/api/llms.txt
write src/api/types.ts
write src/client/client.ts
write src/mocks/handlers.ts
write src/mocks/scenarios.ts
write src/server/contract-runtime.ts
write src/server/getPet.contract.test.ts
write src/server/handlers.ts
write src/server/routes.ts
write contractdiff.map.json

findings: 5
  mocks  value-synthesised  #/paths/~1pets~1{id}/get/responses/200/content/application~1json: the 200 response of "getPet" has no example, so its mock body is synthesised from the schema
    fix: add an `example` to the media type to mock it with a value you chose
  server  server-example-synthesised  #/paths/~1pets~1{id}/get: `getPet` path parameter "id" has no example: the contract test sends one made from its schema
    fix: add an `example` to the spec, so the contract test sends a value you chose
  server  server-example-synthesised  #/paths/~1pets~1{id}/get: `getPet` query parameter "fields" has no example: the contract test sends one made from its schema
    fix: add an `example` to the spec, so the contract test sends a value you chose
  server  server-example-synthesised  #/paths/~1pets~1{id}/get: `getPet` response 200 has no example: the contract test sends one made from its schema
    fix: add an `example` to the spec, so the contract test sends a value you chose
  server  server-problem-undeclared  #/paths/~1pets~1{id}/get: `getPet` answers a request the contract refuses with 400 Problem Details, which its responses do not declare
    fix: declare a `400` response with `application/problem+json` content

Every file is listed with its owner. A generated file belongs to contractdiff and is rewritten on every run. A user file, such as the Hono target's handlers.ts, is written once, as a starting point, and never touched again. The findings are not errors: each says what in the contract made the generator guess (here, no example to build a mock body or a contract test request from) and how to stop it guessing.

The targets

generatorwrites
typestypes.ts: the paths, webhooks, components and operations interfaces openapi-typescript writes; see Coming from openapi-typescript
validatorvalidators.ts: one compiled guard per payload, and an operations map; see The validator target
json-schemaa JSON Schema document per payload (2020-12 or draft-07), or the subset a model provider accepts
fetcha typed client over the platform fetch: one method per operation, every response a typed result, nothing thrown on a non-2xx
tanstack-query, swrquery options or keys and fetchers for every operation, over the fetch client
mswMSW handlers and a scenario for every declared response, from the contract's examples or a seeded synthesis
nestNestJS: a generated abstract controller per tag that validates requests and responses, a controller you own, a contract test per operation
express, fastify, honoa generated router or plugin that validates requests and responses, handlers you own, a contract test per operation
mcpan MCP server with one tool per operation, whose handlers call the API
markdownone page per operation with every schema resolved, plus index.md and llms.txt

--target <name> runs only that target, and can be repeated.

The map file

contractdiff.map.json records every file and symbol each run wrote, with a hash of what it wrote. That is how the next run tells its own files from yours:

  • a generated file that still matches its hash is rewritten freely;
  • a generated file that was edited by hand is refused, with the file named, unless you pass --force;
  • a generated file the previous run wrote and this one does not (an operation was removed) is deleted, on the same terms: only when it is still what contractdiff wrote;
  • a user file is never rewritten or deleted.
edit.mjs
import { appendFileSync } from "node:fs";

appendFileSync("src/api/types.ts", "// a hand edit\n");
console.log("edited src/api/types.ts");
Output of node edit.mjs
edited src/api/types.ts
Output of npx contractdiff gen --check
error: generated files were edited by hand: src/api/types.ts
fix: move the change to a user file, or pass --force
{"error":{"kind":"runtime","code":1,"message":"generated files were edited by hand: src/api/types.ts"}}

The map is also a usage file: diff --usage contractdiff.map.json treats the generated files as the callers of each operation. See Breaking changes.

In CI: --check and --dry-run

  • --check writes nothing and exits 1 when a file is stale (the contract changed and nobody ran gen) or was edited by hand. It is the step that keeps generated code in step with the contract on every pull request.
  • --dry-run writes nothing and prints the diff a run would write.

Both commands are idempotent in burgee's sense: the same input writes the same bytes, and the result says whether anything changed (changed: false on a rerun).

Code to contract: extract

contractdiff extract goes the other way, and writes the contract from code. The extractors in 1.0.0 are standard-schema (the exports of a module, as one JSON Schema document), openapi-from-schemas (a module of { method, path, input, output } per operation, as OpenAPI 3.1) and ts-rest (a ts-rest router, read as data). The command's help also names NestJS and Hono; in 1.0.0 those two extractors are not built yet.

{
  "extract": {
    "from": "standard-schema",
    "out": "spec.json",
    "options": { "module": "./src/schemas.ts" }
  }
}

standard-schema reads any value that implements Standard JSON Schema (~standard.jsonSchema), such as Zod 4.2 and later. truefit 1.0's standard() implements Standard Schema but not its JSON Schema half, so a truefit schema is reported as a no-standard-json-schema finding rather than written.

On this page