Skip to main content

openapi

The published contract: how the API's surface becomes its OpenAPI document.

API_VERSION is the contract's version, and the comment above it is the record of what each version changed — the two are meant to be read together, and the version is bumped by hand when the surface changes.

build_openapi assembles the document FastAPI generates into what is published: the criterion families, which no route returns and so would otherwise be absent, are added as their own schemas, and every operation's description is trimmed to its caller-facing prose. dump_openapi_yaml renders it for the committed copy beside the package, which dev-scripts/dump_patient_api_openapi.py writes.

Module​

Functions​

build_openapi​

def build_openapi(app: FastAPI) ‑> dict[str, typing.Any]:

Build the OpenAPI document, adding what the route signatures cannot express.

Route docstrings are also trimmed to their caller-facing prose (see _endpoint_description), so the handler's own Args/Returns/Raises sections stay in the source without being published as contract.

Two additions on top of FastAPI's generation:

  • the EligibilityCriterion family schemas. They are real Pydantic models (CRITERION_VARIANTS) but no route returns one, so nothing would otherwise pull them into components. They are advertised on the base schema as x-variants rather than as a oneOf: several overlap, and a family added later must still validate against the base.
  • ScanImageManifest and ScanSegmentationIndex, which describe files inside the scan-image archive rather than any response body.

Neither is reachable from a path by ordinary means, so both are pointed at by $ref objects — the variants from x-variants/x-family-variants here, the two archive members from the scan-image route's x-archive-members. A bare reference string in an extension is invisible to a tool that walks $refs, which is how a bundler or client generator decides a schema is dead and prunes it.

Existing schemas are never overwritten, so FastAPI stays authoritative for everything it already generated.

Arguments

  • app: The application whose routes the document is built from.

Returns The OpenAPI document.

dump_openapi_yaml​

def dump_openapi_yaml(document: dict[str, Any]) ‑> str:

Render an OpenAPI document as YAML.

Shared by the /openapi.yaml route and by dev-scripts/dump_patient_api_openapi.py, so the served document and the committed copy cannot differ in rendering — only a content difference should ever show up in a diff of the two.

Arguments

  • document: The document to render (app.openapi()).

Returns The YAML text, with every multi-line description as a literal block.