Architecture / Decision records / ADR-HIS-045
ADR-HIS-045 Contract tooling: formats, envelope, shared types, compatibility and contract tests¶
| Status | Accepted |
| Decided | 2026-11-24 by session FN-S04 |
| Record | Drafted 2026-11-24; approved 2026-11-25 by the Enterprise Architecture Board |
| Where the rule lives | contracts/common/ (its README.md, types/, envelope/, openapi/, src/); the agent instruction file CLAUDE.md, section 3 |
Context¶
Card FN-S04 asks for the conventions and tooling of API and event contracts. These are the file layout, the event envelope, registration in the schema registry, the compatibility check, stub generation and contract tests on both sides. The card also asks for the shared type library of architecture 6.6, aligned with FHIR R4, and for the facility identifier and denial cases of architecture 7.5.
Every contract session of every slice writes contracts on this tooling, in its own domain folder, side by side with others (session rule 10). Every build session builds and tests against those contracts and their stubs before the provider exists (session rule 12). The specifications fix the envelope's fields (6.3, IF-ISE-002), the API conventions (6.2, IF-ISE-001) and the rule "backward compatible within a major version". They do not fix formats, tools, the wire form or how compatibility is decided. The service template (FN-S03, ADR-HIS-044) already writes events in a provisional CloudEvents mapping.
Options considered¶
- API contract format: OpenAPI 3.0, OpenAPI 3.1, or a code-first contract generated from the Java units.
- Event contract format: Avro, Protobuf, or JSON Schema 2020-12. For the event index, AsyncAPI 3, or a small index file of our own.
- Envelope wire form: CloudEvents binary mode (attributes in Kafka headers), or structured mode (one JSON document). In either case the template's extension names can be kept or changed.
- Compatibility: the schema registry's own rule (Apicurio BACKWARD), an off-the-shelf diff tool (oasdiff, json-schema-diff), or a check of our own over both formats.
- Contract tests: consumer-driven pacts (Pact with a broker), or provider verification from the contract with schema-validating stubs.
- Tool language: Java inside the units, or TypeScript as a package of its own.
Decision¶
- OpenAPI 3.1 for APIs and JSON Schema 2020-12 for event payloads. They share one schema dialect, so the shared types are written once and referenced from both. A domain's folder
contracts/<domain>/holds: contract.yaml, the index of the domain's APIs and events, with the topic, aggregate, facility scope and version of each event;api/<name>.v<major>.openapi.yaml;events/<EventName>.v<major>.schema.json;- the generated
stubs/.$refs are relative files in the repository; remote references are refused. - The envelope is the template's CloudEvents structured-mode mapping, fixed as written. The fixed form is
envelope/envelope.v1.json: extension attributes in lowercase,schemaversionas<major>.<minor>(a bare major means.0), anddatathe payload.datarefis added for "payload or reference". The rules JSON Schema cannot state are checked by the tooling: - sequence is required for sequenced events;
- facility is required for facility-owned aggregates and absent for enterprise-scoped ones;
- only the owning domain is the source;
- the major version is known.
- Shared types in FHIR R4 field names (
types/types.v1.json): Identifierusessystem,value,type,periodandassigner;CodingandCodeableConcept;Quantityis fixed to UCUM;Moneyusesvalueandcurrency;Instant,ZonedInstantandPeriod;Referenceand its person, encounter and facility forms;ProvenanceandActor;StatusWithReason;FacilityCode;Problem, with the error classes of IF-ISE-001.
Decimals are strings of digits. An enum that consumers must accept growing is marked x-his-extensible-enum.
- Conventions checked by a lint:
- event, topic and subject names follow architecture 6.3, and an event is in the event catalogue with its domain as producer;
- event payloads stay open;
- every operation declares the common headers and error answers and says x-his-facility: owned | enterprise. An owned one requires X-Facility;
- an owned operation that addresses a record by identifier says x-his-cross-facility: hidden | policy:
- hidden answers another facility's record as not found, and documents 404. It is for the administrative and operational functions limited to permitted facilities;
- policy leaves the answer to the policy decision point, for the clinical access that architecture 7.5 lets cross facilities;
- the owning specification decides which applies; the tooling does not;
- examples match their schemas.
- A compatibility check of our own, aware of direction and of major versions. Requests (input) must accept everything they accepted. Responses and event payloads (output) must not allow anything consumers did not expect. Shared types and the envelope must satisfy both. A change of meaning must raise the minor version. A breaking change is a new major version beside the old one. The check compares with a git revision (by default origin/main), and what it cannot prove compatible it reports as breaking.
- Registration in Apicurio, compatibility decided by the check.
- One artifact per event type, his.<domain>.<EventName> in the group default, so that the Confluent-compatible API sees the same subject.
- Versions are <major>.<minor> and immutable.
- Each artifact has the VALIDITY and INTEGRITY rules. The COMPATIBILITY rule is not set, because Apicurio applies it across every version of an artifact, including a new major that may break.
- Contract tests from the contract, on both sides, with no broker.
- The generated stub serves every operation from its examples, and refuses and records requests that break the contract. A consumer's test asserts that it recorded no violation.
- The stub keeps records per facility, so cross-facility reads answer 404.
- Sample events are generated for event consumers.
- The provider harness (his-contract verify) runs the cases the contract implies against a running provider. These are the documented request, no credential, a body that breaks the contract, and the facility denial cases of architecture 7.5: no X-Facility, a facility not permitted, and, only where the operation is declared hidden, a record read from another facility. Every error answer is checked to be a Problem with the right code.
- TypeScript, run by Node.js 22.18 or later with type stripping, as one package @his/contract-tools in contracts/common with its own lockfile. Its dependencies are Ajv, ajv-formats and yaml; Vitest and TypeScript are used for development only. A domain's folder takes it through a pnpm link: dependency, so no lockfile points outside the repository.
Rationale¶
OpenAPI 3.1 and JSON Schema 2020-12 are one dialect. A field defined once is the same in an answer and in an event, which architecture 6.6 (point 4: one name per concept) needs. JSON also matches the envelope the template already writes and the FHIR facade's JSON. Avro or Protobuf would need a second definition of every shared type and a code generation step in every unit. A code-first contract would put the contract after the code, which is the reverse of rule 3 and of 6.6 point 3.
Keeping the template's envelope means no unit, and not the template, has to change. Structured mode keeps the envelope readable in a dead-letter topic and in the evidence of a gate.
Apicurio's compatibility rule does not know major versions. With it set, a new major version could not be registered. Without it, nothing is checked. A check of our own runs on the pull request, before anything is published. It knows the direction each schema is read in, which the generic diff tools do not, and it covers events and APIs with one rule set.
Consumer-driven pacts need every consumer to exist and a broker to hold the pacts. Here the provider's contract is fixed first, by the contract session, and is approved by the domain leads (6.6 point 5). Consumers are then built against it, often before the provider. Verifying both sides against that contract fits the plan's order. It also keeps "contract tests include denial across facilities" (7.5) in one harness every provider runs.
TypeScript keeps the tooling one small package that runs the same on every folder: a contract folder, services/pex-portal-bff and the web workspaces. A Java unit runs it as a command against its started application. Running TypeScript directly, without a build step, means a domain folder needs nothing compiled to use it.
Consequences¶
- A contract session copies the layout of
contracts/common/example/into its domain folder and adds thepackage.jsonof the README. It then runshis-contract stub .andhis-contract check .. Its domain folder'spnpm testis the check againstorigin/main. - The pipeline (FN-S05) runs
pnpm install --frozen-lockfile,pnpm build,pnpm testandpnpm checkincontracts/common, andpnpm testin each domain folder that has apackage.json, on any change undercontracts/. It needsorigin/mainfetched. The pipeline, or the platform unit event-backbone, registers schemas on merge withhis-contract register. - Topics come from the contract.
contract.yamlnames each event's topic, andhis-contract topicslists them. The platform unit event-backbone (MOD-ISE-004) creates topics in shared environments from that list. In the local environment, the kit'shis.events.kafka.create-topicsstays. - The template's
EnvelopeCodecalready matches the envelope, and its tests pass unchanged. It writesschemaversionas"1", which the envelope accepts. A contract with minor versions needs the module to pass"1.1". - Java units call
his-contract verifyfrom a contract test against the application they started, andvalidate-eventon the events they published. A JUnit helper that does this belongs in the service template, as a follow-up of FN-S03's README ("the harness comes from FN-S04"). - A shared type changes only additively within version 1;
check-commonenforces it. A breaking change to a shared type is atypes.v2.jsonbeside the first.