Repository navigation
Generate an OpenAPI 3.1 document from the wire contract - #1239
Conversation
Adds scripts/generate-openapi.ts, which builds an OpenAPI 3.1 document from the shared/contract/ Zod schemas via @asteasolutions/zod-to-openapi, and the resulting static shared/openapi.json artifact. No contract schema is edited; generation is descriptive only (issue #937 item 2).
Mounts backend/src/routes/openapi.ts next to the existing /schemas route, serving the static shared/openapi.json generated by scripts/generate-openapi.ts. Static and D1-free, matching the /schemas precedent (backend/src/routes/schemas.ts).
backend/test/openapi-document.test.ts: drift guard against shared/openapi.json, OpenAPI 3.1 structural validity, public catalog route coverage derived from the real Hono router, and a round-trip of a real GET /datasets and GET /datasets/:id response against the generated JSON Schema components.
📦 Package PreviewYour package is ready to test! bun install -g nemar-cli@PR1239Version: 🤖 Updated on each push. |
|
Review addressed. No Critical and no Important findings; the one Suggestion and two of the three Nice-to-haves are fixed, and the third is declined with a reason. Suggestion, undeclared 401 responses. Fixed in 2f07cdf. Both handlers were read before writing the descriptions, and the two paths turn out to fail differently, which the descriptions now say. On Nice-to-have, no script alias. Fixed in 10c8a47: Nice-to-have, the media-type comment. Fixed in 0ec936d. The comment claimed Nice-to-have, The drift guard is the thing worth restating, because it is what makes a committed generated artifact safe: Gates after the fixes: lint clean, typecheck clean on root and backend, |
Summary
Issue #937 item 2 only (item 1, extracting shared/contract into an npm package, and item 4, the contract-live conformance test, are out of scope for this PR).
Generates an OpenAPI 3.1 document from the Zod schemas in shared/contract/ using @asteasolutions/zod-to-openapi (the zod 3.x-compatible 7.x major, matching this repo's zod ^3.23), and serves it read-only at GET /openapi.json.
Chose shape (b) from the issue: build-time generation into a checked-in static artifact (shared/openapi.json), imported by backend/src/routes/openapi.ts the same way backend/src/routes/schemas.ts imports shared/zarr-index.schema.json ("One file, two readers"). This matches the repo's existing precedent for serving a contract document, keeps generation cost off the Worker's critical path, and lets a test regenerate the document and diff it against the committed artifact so the two cannot silently drift apart.
The document is descriptive only. Every response schema handed to the registry in scripts/generate-openapi.ts is the exact object exported from shared/contract/.ts, registered as-is; shared/contract/.ts itself is not touched by this change (no .describe()/.openapi() annotations added there). No shape, requiredness, type, or refinement was changed anywhere in the contract.
One non-cosmetic generator-level fix was needed: @asteasolutions/zod-to-openapi 7.3.4 renders a nullable enum (e.g. zarr_status, attestation_deposit_type) as
{type: ["string", "null"], enum: [...]}without widening the enum array to include null, so a schema that claims to allow null actually rejects it under JSON Schema's enum keyword. This was caught by the round-trip test below against a real nullable zarr_status row. Fixed with a small post-processing pass over the generated document's OUTPUT (not the input contract) that adds null to any enum array whose sibling type allows it.Routes documented
Plus a reusable VersionTag component from shared/contract/version.ts (the canonical vX.Y.Z tag shape).
Query parameters are read from the real handlers (backend/src/routes/datasets/catalog.ts): the nine legacy bespoke filters (modality, author, task, has_doi, has_hed, has_zarr, has_zarr_verified, data_complete, recent, license, include_unknown) plus all 20 declared facets from shared/facets.ts (built programmatically from that table, so a new facet is documented automatically), plus mine/status/limit/offset/owner/sort/search for the list route and q/limit/offset/min_score for search. GET /datasets/facets takes no query parameters, matching its handler. GET /datasets/:id takes the id path parameter.
Test plan
backend/test/openapi-document.test.ts (unit-pure tier: no network, no live backend):
Proved each assertion category actually fails on a one-line mutation (reverted before committing): a stale generator literal without regenerating failed the drift guard; removing one entry from the documented-routes map failed the coverage test; the nullable-enum bug above was caught live by the round-trip test before being fixed.
Gate results:
bun run lint(repo default only checks src/): clean. Additionally ranbunx biome check --fixdirectly against the new/changed files (scripts/generate-openapi.ts, backend/src/routes/openapi.ts, backend/test/openapi-document.test.ts, backend/src/index.ts): clean.bun run typecheck: clean (tsc --noEmit at root and -p backend both pass). scripts/ is outside both tsconfigincludeglobs, matching this repo's existing convention for other scripts/*.ts files; ran a standalonetsc --noEmitagainst scripts/generate-openapi.ts with equivalent compiler options as an extra check: clean.bun test backend/test/openapi-document.test.ts: 9 pass, 0 fail.bun test backend/test/: 1656 pass, 0 fail (121 files) -- no regressions from this change.test/*.test.ts+backend/test/*.test.ts, minus the same exclusions test.yml uses): 3899 pass, 20 skip, 0 fail (258 files, 3919 tests) -- pre-existing skips, no failures.Part of #937
Part of epic nemarOrg/website#284