Skip to content

feat(core): add the version negotiation headers to every route - #1401

Merged
karinamzalez merged 10 commits into
CORE-v0.6.0from
karina/1386-version-headers
Nov 18, 2026
Merged

karinamzalez merged 10 commits into
CORE-v0.6.0from
karina/1386-version-headers

Conversation

@karinamzalez

@karinamzalez karinamzalez commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Adds protocol version 0.6.0 and the version headers from ADR 0032 (API version negotiation) to the core library. Clients can ask for a version with CG-Version, and every response confirms the version it was served in with CG-Version-Served. The docs mock API now serves 0.6.0 so the website build stays green. The version errors (#1388), the discovery route (#1387, PR #1399), the docs (#1389, #1391) and moving the mock to the version header (#1393) are separate tickets.

Important

This PR and #1399 both add Versions.v0_6 and Types.protocolVersion. Whichever merges second drops its copy. Once both land, the discovery route needs its own 200 response without the version headers, because ADR 0032 says discovery carries neither CG-Version-Served nor Vary.

Changes proposed

  • Protocol version 0.6.0:
    • lib/core/lib/main.tsp: adds v0_6: "0.6.0" to Versions.
    • lib/core/lib/core/types.tsp: adds Types.protocolVersion, a full MAJOR.MINOR.PATCH version with no v prefix, no short forms and no leading zeros.
  • lib/core/lib/core/headers.tsp: new CommonGrants.Headers namespace, not emitted as JSON Schema.
    • VersionRequest: optional CG-Version request header.
    • VersionResponse: required CG-Version-Served, plus optional Vary, Deprecation (RFC 9745 @<timestamp>), Sunset (RFC 8594 HTTP-date) and Link.
    • VersionResponseAuth: the same headers with CG-Version-Served optional, for 401 and 403, because authentication can run before version selection.
    • The namespace example shows ...Headers.VersionRequest on a hand-declared protocol route, since custom routes ignore CG-Version.
  • lib/core/lib/core/index.tsp: imports headers.tsp.
  • Request header on every route: lib/core/lib/core/routes/{opportunities,awards,orgs,competitions,applications,form-responses,forms}.tsp spread ...Headers.VersionRequest as the last parameter of every operation, so adopters inherit it through op x is Router.y.
  • Response headers on every response:
    • lib/core/lib/core/responses/success.tsp: spreads ...Headers.VersionResponse into OkT, PaginatedT, SortedT, FilteredT, CreatedT and AcceptedT. The emitted non-template models are untouched.
    • lib/core/lib/core/responses/error.tsp: NotFound intersects VersionResponse. Unauthorized and Forbidden intersect VersionResponseAuth.
    • lib/core/lib/core/routes/applications.tsp: submitApplication returns a new ApplicationSubmissionErrorResponse wrapper with an explicit @body, so the submission error keeps @error and its body reference while gaining the headers.
  • Generated: website/public/openapi/openapi.0.6.0.yaml and website/public/schemas/yaml/protocolVersion.yaml.
  • Docs mock API serves 0.6.0:
    • website/src/lib/mock/data/fixtures.ts: adds 0.6.0 to SUPPORTED_VERSIONS. It shapes responses the same as 0.5.0, because 0.6.0 adds only headers.
    • website/__tests__/lib/mock/** and website/__tests__/scripts/inject-mock-server.spec.ts: cover 0.6.0, and use 0.7.0 as the unsupported version.
  • lib/python-sdk/tests/schemas/test_award_inventory.py: moves the Core baseline tripwire from 0.5.0 to 0.6.0. 0.6.0 adds no fields to the Award closure.

Context for reviewers

The part worth the closest look is how the headers stay out of the 0.1.0 to 0.5.0 documents and out of the JSON Schemas.

  • Versioning on spreads. Every header property carries its own @added(v0_6). Spreading a model copies its properties without the model's versioning, so a model-level @added alone leaked the headers into older documents.
  • Submission error wrapper. It lives in Routes, not Responses, because Responses is @jsonSchema and would emit the headers as schema fields. Intersecting the headers onto ApplicationSubmissionError dropped @error and moved it into the 200 response. The wrapper's explanation is a // comment because a doc comment replaced the response description in older documents.
  • Vary is optional in OpenAPI. ADR 0032 requires it only on cacheable responses, which OpenAPI can't express, so the rule lives in the header description.
  • CI is green. Adding Versions.v0_6 used to break two checks: the website's mock server guard and the Python SDK's Core baseline tripwire. This PR updates both. The mock still selects the version from the URL path; [Mock API] Move the mock API to the version header #1393 moves it to CG-Version.

To verify:

  1. Run core CI. It should pass.
    pnpm --filter @common-grants/core run ci
  2. Regenerate the website outputs. git status should show no changes under website/public.
    pnpm --filter website run typespec && git status --short website/public
  3. Confirm the older documents are unchanged. The output should be empty.
    git diff --stat origin/CORE-v0.6.0 -- website/public/openapi/openapi.0.{1,2,3,4,5}.0.yaml
  4. Confirm the only JSON Schema change is the new protocolVersion.yaml.
    git diff --stat origin/CORE-v0.6.0 -- website/public/schemas
  5. Count the headers in 0.6.0. Expect 21 request header references, one per operation, and 47 CG-Version-Served headers, one per response.
    grep -c "Headers.VersionRequest'" website/public/openapi/openapi.0.6.0.yaml
    grep -c "CG-Version-Served:" website/public/openapi/openapi.0.6.0.yaml

Additional information

Core CI:

$ tsp compile lib/main.tsp
TypeSpec compiler v1.17.0

✔ Compiling
✔ @typespec/json-schema 68ms tsp-output/@typespec/json-schema/
✔ @typespec/openapi3 397ms tsp-output/@typespec/openapi3/

Compilation completed successfully.

Every response in openapi.0.6.0.yaml, grouped by status code (count, then whether each header is required or optional):

 19  200     | CG-Version-Served:req Vary:opt Deprecation:opt Sunset:opt Link:opt
  1  201     | CG-Version-Served:req Vary:opt Deprecation:opt Sunset:opt Link:opt
  1  202     | CG-Version-Served:req Vary:opt Deprecation:opt Sunset:opt Link:opt
 10  404     | CG-Version-Served:req Vary:opt Deprecation:opt Sunset:opt Link:opt
  1  default | CG-Version-Served:req Vary:opt Deprecation:opt Sunset:opt Link:opt
  9  401     | CG-Version-Served:opt Vary:opt Deprecation:opt Sunset:opt Link:opt
  6  403     | CG-Version-Served:opt Vary:opt Deprecation:opt Sunset:opt Link:opt
Sunset schema: {"type":"string","format":"http-date"}
Deprecation schema: {"type":"string","pattern":"^@-?[0-9]+$"}
CG-Version-Served schema: {"$ref":"#/components/schemas/CommonGrants.Types.protocolVersion"}
CG-Version params: 21 of 21 operations

Every 0.5.0 response compared against 0.6.0 for body, description, pre-existing headers (such as Location on 202) and request bodies:

checked 47 responses, 0 differences
component schemas added in 0.6.0: [ 'CommonGrants.Types.protocolVersion' ]

Adopter quickstarts still compile against the new route interfaces:

✓ Getting Started (First steps) quickstart compiles
✓ Getting Started (after Customization) quickstart compiles
✓ lib/core/README.md quickstart compiles

… scalar

Adds protocol version 0.6.0 to the core library and a `protocolVersion`
scalar for a full published version such as `0.5.0` (no `v` prefix, no short
forms, no leading zeros, ASCII digits only). The version headers in ADR 0032
will use this scalar. The 0.1.0 to 0.5.0 OpenAPI documents are unchanged, and
the new 0.6.0 document differs from 0.5.0 only in `info.version`.

Refs #1386

Files changed:
- lib/core/lib/main.tsp
- lib/core/lib/core/types.tsp
- website/public/openapi/openapi.0.6.0.yaml
- website/public/schemas/yaml/protocolVersion.yaml
…otocol route

Adds a `CommonGrants.Headers.VersionRequest` model with an optional
`CG-Version` header typed as `Types.protocolVersion`, and spreads it as the
last parameter of every operation on the `Routes.*` interfaces, so adopters
inherit it when they declare `op x is Router.y`. The property carries
`@added(v0_6)` because spreading copies properties without the model's
versioning. All 21 operations in the 0.6.0 OpenAPI document now take the
header; the 0.1.0 to 0.5.0 documents and the JSON Schemas are unchanged.

Refs #1386

Files changed:
- lib/core/lib/core/headers.tsp
- lib/core/lib/core/index.tsp
- lib/core/lib/core/routes/applications.tsp
- lib/core/lib/core/routes/awards.tsp
- lib/core/lib/core/routes/competitions.tsp
- lib/core/lib/core/routes/form-responses.tsp
- lib/core/lib/core/routes/forms.tsp
- lib/core/lib/core/routes/opportunities.tsp
- lib/core/lib/core/routes/orgs.tsp
- website/public/openapi/openapi.0.6.0.yaml
…rs on every response

Adds `Headers.VersionResponse` (required `CG-Version-Served`) and
`Headers.VersionResponseAuth` (optional `CG-Version-Served`, for 401 and 403),
both with optional `Vary`, `Deprecation` (RFC 9745), `Sunset` (RFC 8594,
HTTP-date) and `Link`. They are spread into the six success response
templates, intersected into the `NotFound`, `Unauthorized` and `Forbidden`
aliases, and carried by a `Routes.ApplicationSubmissionFailed` wrapper so the
submission error keeps `@error` and its body reference. Each header property
carries `@added(v0_6)`. `Vary` is optional because ADR 0032 requires it only on
cacheable responses. The 0.1.0 to 0.5.0 documents, the JSON Schemas and every
response body are unchanged.

Refs #1386

Files changed:
- lib/core/lib/core/headers.tsp
- lib/core/lib/core/responses/success.tsp
- lib/core/lib/core/responses/error.tsp
- lib/core/lib/core/routes/applications.tsp
- website/public/openapi/openapi.0.6.0.yaml
- Clarify that servers trim whitespace before checking `CG-Version` against
  the `protocolVersion` format, so the prose and the pattern agree.
- Show the version response headers in the `Error` doc example, so adopters
  who copy it get `CG-Version-Served` from 0.6.0.
- Rename `Routes.ApplicationSubmissionFailed` to
  `ApplicationSubmissionErrorResponse`, so the wrapper is easier to tell apart
  from the `ApplicationSubmissionError` body model.

Only the `CG-Version` description changes in the 0.6.0 document. The 0.1.0 to
0.5.0 documents and the JSON Schemas are unchanged.

Refs #1386
@karinamzalez karinamzalez linked an issue Nov 9, 2026 that may be closed by this pull request
4 tasks
@github-actions github-actions Bot added website Issues related to the website core Issues related to @common-grants/core library typescript Issue or PR related to TypeScript tooling labels Nov 12, 2026
The website build refuses to advertise a mock server for a spec version the
mock router can't shape, so emitting openapi.0.6.0.yaml failed the build.
v0.6.0 adds only the version negotiation headers, so it shapes identically
to v0.5.0: add it to SUPPORTED_VERSIONS. The docs page's version selector is
left alone until v0.6.0 is released.

Update the tests that pinned the version list: use 0.7.0 as the unshapeable
version, compare "older than 0.5.0" by ordering instead of by inequality,
add 0.6.0 to the per-version application maps, and keep 0.6.0 out of the
golden corpus (the 3A Worker it is captured from doesn't serve it), adding it
to the two golden error messages as was done for 0.5.0.

Refs #1386

Files changed:
- website/src/lib/mock/data/fixtures.ts
- website/__tests__/lib/mock/__fixtures__/golden-envelopes.json
- website/__tests__/lib/mock/__fixtures__/request-matrix.ts
- website/__tests__/lib/mock/conformance/applications-vs-openapi.spec.ts
- website/__tests__/lib/mock/data/fixtures.spec.ts
- website/__tests__/lib/mock/handlers/applications.spec.ts
- website/__tests__/scripts/inject-mock-server.spec.ts
@github-actions

github-actions Bot commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

🚀 Website Preview Deployed!

Preview your changes at: https://pr-1401-common-grants.agile6.workers.dev

Built from commit 8d592c5

`test_core_baseline_is_v0_5` asserts that no v0.6+ OpenAPI document exists,
so emitting openapi.0.6.0.yaml fails the Python SDK build. Rename it to
`test_core_baseline_is_v0_6` and bump both assertions. The change is
byte-identical to the one in #1399, so the two branches merge cleanly.

Refs #1386

Files changed:
- lib/python-sdk/tests/schemas/test_award_inventory.py
@github-actions github-actions Bot added python Issue or PR related to Python tooling sdk Issue or PR related to our SDKs py-sdk Related to Python SDK labels Nov 16, 2026
Swagger UI pre-fills the CG-Version field from this example, so on the
0.6.0 API docs page Execute asked the mock for 0.5.0. The example now
names the version that introduces the header.

@SnowboardTechie SnowboardTechie left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looked through this against ADR 0032, really nice work. One small suggestion inline. The description's notes on the mock waiting for #1393 (moving the mock to the version header) and the two expected red checks are out of date now that d9d2df9 serves 0.6.0.

Comment thread lib/core/lib/core/headers.tsp Resolved
Custom routes ignore CG-Version and cg check flags them, so the example
now declares GET /common-grants/opportunities by hand.

Files: lib/core/lib/core/headers.tsp
@karinamzalez

Copy link
Copy Markdown
Collaborator Author

@SnowboardTechie thanks! Applied the example suggestion in 8d592c5 and updated the description: the mock now serves 0.6.0, the Python tripwire moved to 0.6.0, and CI is green.

@karinamzalez
karinamzalez merged commit cb2681f into CORE-v0.6.0 Nov 18, 2026
10 checks passed
@karinamzalez
karinamzalez deleted the karina/1386-version-headers branch November 18, 2026 16:35
SnowboardTechie added a commit that referenced this pull request Nov 20, 2026
### Summary

- Fixes #1387
- Time to review: 15 minutes

### Changes proposed

A server built on core 0.6.0 can declare `GET /common-grants/versions`
and publish which protocol versions it serves: its default version, the
versions it serves across routes (the baseline), entries for routes that
differ from that baseline, and, while a change is pending, an upcoming
default and deprecated versions with their retirement dates. Clients can
check support before sending a request, per [ADR
0032](https://github.com/HHS/simpler-grants-protocol/blob/main/website/src/content/docs/governance/adr/0032-api-version-negotiation.md)
(API version negotiation).

- `Routes.ProtocolVersions.read()` returns the new
`Responses.VersionDiscoveryResponse`, wired into the base API at
`/common-grants/versions`, tagged `Versions` and `optional`. The route
is optional; the ADR requires it of servers that serve more than one
version, and the route description carries that and the other server
rules.
- `Responses.VersionDiscoveryResponse` is the standard success envelope
without the version headers. `OkT` spreads `Headers.VersionResponse`
since #1401, and the ADR says discovery carries neither
`CG-Version-Served` nor `Vary: CG-Version`, because clients call it
before they know which version to request.
- New models in `CommonGrants.Models`: `VersionDiscovery`,
`OperationVersions`, `UpcomingDefault`, `VersionRetirement`, with the
two examples from the ADR. These four models accept fields they don't
declare (`...Record<unknown>`, as `FormJsonSchema` and `FormUISchema`
already do), so a server can add discovery fields later without a new
protocol version, which is what the route's "fields are only ever added"
rule promises. Other core models stay closed to undeclared fields, apart
from those free-form form-schema models and the typed filter maps.
- Regenerated `website/public/openapi/openapi.0.6.0.yaml` and added five
JSON Schema files.

### Context for reviewers

**Rebased onto #1401 and #1414.** `Versions.v0_6` and
`Types.protocolVersion` now come from the version headers PR, so the
stubs this PR carried are gone. The field names (`versions`,
`defaultVersion`, `operations`, `upcomingDefault`, `deprecated`) match
the published ADR.

**Why these models are open.** Discovery is the one route a client calls
before it knows a version, so its shape can't be versioned per document;
that's why these models are open while other core models are closed to
undeclared fields (`seal-object-schemas: true` in the website build,
which the SDKs' strict validators follow). Whether the additive rule
should bind other models is for the ADR, not this PR.

**Verified:**

- `pnpm --filter @common-grants/core run ci` passes (lint, format,
types, example dates, build, compile).
- The 0.1.0 through 0.5.0 OpenAPI documents are byte-identical to
`CORE-v0.6.0`: `git diff origin/CORE-v0.6.0 --
website/public/openapi/openapi.0.{1,2,3,4,5}.0.yaml` is empty. No
existing JSON Schema file changed.
- `pnpm --filter website run check:quickstarts`, `build` and `test` pass
(1225 tests).
- In the 0.6.0 document the route's 200 declares no headers, it takes no
`CG-Version` parameter and declares no version errors (the ADR says
discovery never returns one), and it is tagged `Versions` and
`optional`.
- The JSON Schema files reject a `deprecated` key that isn't a protocol
version, such as `v0.3`, at the top level and in route entries.

This PR targets the `CORE-v0.6.0` hold branch (see
[`lib/README.md`](https://github.com/HHS/simpler-grants-protocol/blob/main/lib/README.md)
on hold branches), and GitHub doesn't auto-close issues from a
hold-branch PR. #1387 is closed by hand when the `feat(core)!: v0.6.0`
staging PR merges into `main`, per the hold-branch checklist in that
file.

**For adopters.** Until the CLI bundles the 0.6.0 base spec, `cg check`
reports `/common-grants/versions` as an extra route on a server that
implements it early.

OpenAPI 3.0 shows one of the two `VersionDiscovery` examples (the
lifecycle one); both are in `VersionDiscovery.yaml`. Same emitter
behaviour as the existing multi-example scalars. The blank line inside
the route's `@doc("""…""")` block carries trailing spaces because `tsp
format` writes it that way and `check:format` requires the formatter's
output.

### Additional information

Discovery response for a server whose baseline is 0.4.0 and whose form
list is served only in 0.5.0 (the first example on the model):

```json
{
  "status": 200,
  "message": "Success",
  "data": {
    "versions": ["0.4.0"],
    "defaultVersion": "0.4.0",
    "operations": {
      "GET /common-grants/forms": {
        "versions": ["0.5.0"],
        "defaultVersion": "0.5.0"
      },
      "GET /common-grants/forms/{formId}": {
        "versions": ["0.4.0", "0.5.0"],
        "defaultVersion": "0.4.0"
      }
    }
  }
}
```
karinamzalez added a commit that referenced this pull request Nov 20, 2026
### Summary

- Fixes #1393
- Time to review: 40 minutes

The docs mock API now picks the protocol version from the `CG-Version`
header instead of the path, serves `GET /common-grants/versions`, and
answers version errors per ADR 0032. Retirement behavior (`Deprecation`,
`Sunset`) and SDK support for `CG-Version` are out of scope.

### Changes proposed

- Version resolution:
- `website/src/lib/mock/http/version.ts`: new resolver for `CG-Version`
(default 0.6.0, major.minor matching, `version.malformed` /
`version.conflict` / `version.unsupported`), plus `CG-Version-Served`
and `Vary` helpers
- `website/src/lib/mock/http/envelope.ts`: `versionErrorResponse` builds
the error envelope with the `discovery` URL
- `website/src/lib/mock/data/fixtures.ts`: `hostedVersionFor` maps a
requested version to the served one
- Routing by header:
- `website/src/lib/mock/router.ts`: one resource route without a path
version; resolves the version per route, then dispatches. A path like
`/api/v0.5.0/...` is now a plain route miss
- `website/src/lib/mock/data/availability.ts`: `versionsFrom` lists the
versions a route serves
- `website/src/lib/mock/handlers/*.ts`: take the resolved version
instead of reading it from the path
- `website/src/lib/mock/handlers/versions.ts`: new `GET
/common-grants/versions` handler; it ignores `CG-Version` and lists the
routes added after 0.1.0
- `website/src/lib/mock/http/cors.ts`: allows `CG-Version` on preflights
and exposes `CG-Version-Served`, `Deprecation`, `Sunset` and `Link`
- Docs page and Swagger UI:
- `website/src/lib/mock/docs-wiring.ts`,
`website/src/scripts/inject-mock-server.ts`: every mock spec advertises
one server, `/api`
- `website/src/lib/api-docs-version.ts`: `withVersionHeader` adds
`CG-Version` to mock requests unless the user typed one
- `website/src/pages/protocol/api-docs.astro`: wires that helper into
Swagger's `requestInterceptor` and lists v0.6.0 as Latest
- `website/src/content/docs/governance/adr/0028-mock-api-hosting.md`:
note that ADR 0032 supersedes the path version
- Tests (`website/__tests__/`):
- New: `lib/mock/http/version.spec.ts`,
`lib/mock/version-routing.spec.ts`,
`lib/mock/versions-discovery.spec.ts`, `lib/api-docs-version.spec.ts`
- Migrated to header-based requests through a shared
`lib/mock/__fixtures__/mock-request.ts` helper; the golden corpus sends
the path version as `CG-Version`
- `lib/mock/http/cors.spec.ts`: version headers on preflights and
responses

### Context for reviewers

The closest look belongs in `router.ts` and `handlers/versions.ts`:
version errors come before dispatch, and the discovery route list must
stay in step with the 0.6.0 spec (a conformance test walks
`openapi.0.6.0.yaml` to check it).

To verify:

1. Start the docs site: `pnpm --filter website run dev`
2. Run `curl -si http://localhost:4321/api/common-grants/opportunities
-H 'CG-Version: 0.5.1' | grep -i cg-version`. Expect `cg-version-served:
0.5.0`.
3. Run `curl -s http://localhost:4321/api/common-grants/awards -H
'CG-Version: 0.2.0'`. Expect a 406 with `version.unsupported` and
`scope: "operation"`.
4. Run `curl -s http://localhost:4321/api/common-grants/versions`.
Expect the supported versions and per-route `operations`.
5. Open http://localhost:4321/protocol/api-docs?version=0.1.0, "Try it
out" on `GET /common-grants/opportunities`, and Execute. The request
carries `CG-Version: 0.1.0` and the response `cg-version-served: 0.1.0`.

### Additional information

Preview:
https://pr-1415-common-grants.agile6.workers.dev/protocol/api-docs?version=0.1.0.
The verification steps above work against it by swapping
`http://localhost:4321` for
`https://pr-1415-common-grants.agile6.workers.dev`.

Mock responses, captured by calling `handleMockRequest` directly:

```
no header                          200 served=0.6.0 vary=CG-Version
CG-Version: 0.5.1                  200 served=0.5.0 vary=CG-Version
CG-Version: v0.5                   400 served=- vary=CG-Version version.malformed
CG-Version: 0.4.0, 0.5.0           400 served=- vary=CG-Version version.conflict
CG-Version: 9.0.0                  406 served=- vary=CG-Version version.unsupported
awards, CG-Version: 0.2.0          406 served=- vary=CG-Version version.unsupported
GET /versions, CG-Version: bad     200 served=- vary=- versions=0.1.0,0.2.0,0.3.0,0.4.0,0.5.0,0.6.0
```

Checks and tests:

```
$ pnpm --filter website run checks
CSpell: Files checked: 1494, Issues found: 0 in 0 files.

$ pnpm --filter website exec vitest run __tests__/lib/mock __tests__/scripts __tests__/lib/api-docs-version.spec.ts
 Test Files  25 passed (25)
      Tests  676 passed (676)
```

Swagger UI "Try it out" on the 0.1.0 spec (`GET
/common-grants/opportunities`), run locally. The request goes to `/api`
with `CG-Version: 0.1.0`:

<img width="1740" height="620" alt="swagger-0 1 0-request"
src="https://github.com/user-attachments/assets/9caae3cd-fc74-4372-b611-56b3e44c6dd7"
/>


The response is served in 0.1.0, with `Vary` and the CORS allow and
expose headers:

<img width="1740" height="626" alt="swagger-0 1 0-headers"
src="https://github.com/user-attachments/assets/932f075e-182c-4ec9-8ea4-3647392f7768"
/>

On the 0.6.0 spec the header field pre-fills from the spec example,
which #1401 now sets to 0.6.0.

---------

Co-authored-by: Bryan Thompson <bryan@snowboardtechie.com>
karinamzalez added a commit that referenced this pull request Nov 20, 2026
)

### Summary

- Fixes #1390
- Time to review: 30 minutes

Makes `cg check spec` check the version negotiation headers from ADR
0032 against the 0.6.0 spec, and adds a single clear error for APIs that
put the version in their paths. Checking that multi-version servers
publish the discovery route is out of scope, since route tags can't
express that rule.

> [!IMPORTANT]
> This targets `CORE-v0.6.0`. The bundled 0.6.0 spec is regenerated from
the latest `CORE-v0.6.0`, so it includes the version headers (#1401),
the version errors (#1414), and the versions discovery route (#1399).

### Changes proposed

- Sort protocol versions as version numbers, so 0.10.0 comes after
0.9.0:
- `lib/cli/src/commands/check/check-args.ts`: new `parseBaseVersions`
sorts with `semver` and skips file names that aren't a clean version
- `lib/cli/package.json`, `pnpm-lock.yaml`: add `semver` and
`@types/semver`
- `lib/cli/lib/openapi/openapi.0.6.0.yaml`: bundle the 0.6.0 base spec
(version headers, version errors, and the `/common-grants/versions`
discovery route), which becomes the default when no `--protocol-version`
is passed
- `lib/cli/src/commands/check/utils/check-matching-routes.ts`:
- Report a missing header parameter (such as `CG-Version`) and a missing
required response header (such as `CG-Version-Served`) as errors. Names
match regardless of case, and a header declared once on the path item
counts.
- Check headers on routes tagged `experimental` too, which still skip
the other checks
- Only treat HTTP methods on a path item as routes, so path-level
`parameters` aren't checked as a route
- `lib/cli/src/commands/check/utils/check-extra-routes.ts`: same fix, so
path-level `parameters` or `summary` aren't reported as extra routes
- `lib/cli/src/commands/check/utils/check-path-versioning.ts`: new check
that reports one error when routes put a protocol version like `v0.6`
right before or after `/common-grants/`. It only mentions `CG-Version`
when the base spec declares it, and leaves a host API's own versioning
like `/api/v2/common-grants/` alone.
- `lib/cli/src/commands/check/check-service.ts`: run the path versioning
check first and stop there if it fires, instead of listing every route
as missing
- `lib/cli/src/commands/check/utils/types.ts`, `utils/error-utils.ts`:
new error types and their labels in the output
- `lib/cli/src/commands/check/README.md`: document the header and path
versioning checks
- `lib/cli/src/__tests__/commands/check/`: unit tests for each check,
plus end-to-end tests that edit the bundled specs

### Context for reviewers

Look most closely at `check-matching-routes.ts`: header expectations
come from the base spec rather than a version check in code, which is
why checks against 0.1.0 to 0.5.0 are unchanged.

To verify:

1. Build the CLI and run its tests: `pnpm --filter @common-grants/cli
run ci`. Expect 171 passing tests. The build also regenerates the 0.1.0
to 0.5.0 specs, so run `git checkout lib/cli/lib/openapi` afterwards
(#1417 refreshes 0.4.0 and 0.5.0 separately).
2. From `lib/cli`, check the 0.5.0 spec against 0.5.0: `node
dist/index.js check spec lib/openapi/openapi.0.5.0.yaml
--protocol-version 0.5.0`. Expect `Spec is valid and compliant with base
spec`, the same as today.
3. Check the same spec with no version: `node dist/index.js check spec
lib/openapi/openapi.0.5.0.yaml`. Expect it to fail, with `Header
parameter missing` and `Response header missing` on each route, plus a
warning that the optional `GET /common-grants/versions` route is
missing, because 0.6.0 is now the default.

### Additional information

Output from the built CLI. `missing-headers.yaml` is the 0.6.0 spec with
`CG-Version` removed from one route and `CG-Version-Served` removed from
another. `versioned-paths.yaml` is the 0.6.0 spec with every route under
`/v0.6`.

```
$ cg check spec missing-headers.yaml --protocol-version 0.6.0
Validation error: Spec validation failed:
2 errors
=================================
Route conflicts
  GET /common-grants/opportunities
    1 issues found:
    Header parameter missing
      Message: Missing header parameter [CG-Version]
  GET /common-grants/opportunities/{oppId}
    1 issues found:
    Response header missing
      Status code: 200
      Message: Missing response header [CG-Version-Served] for status code [200]
```

```
$ cg check spec versioned-paths.yaml
Validation error: Spec validation failed:
1 errors
=================================
Version in path
  Routes put the protocol version in the path, such as '/v0.6/common-grants/applications/search'. Serve CommonGrants routes under '/common-grants/' with no version segment and select the version with the 'CG-Version' request header (ADR 0032).
```

```
$ pnpm --filter @common-grants/cli run ci
All matched files use Prettier code style!
Compilation completed successfully.
 Test Files  19 passed (19)
      Tests  171 passed (171)
```

Release note: the FastAPI template's CI runs `cg check spec
openapi.yaml` with no version against the published CLI. Once CLI 0.6.0
is released, that job will fail until the template adds the version
headers or pins `--protocol-version 0.5.0`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Issues related to @common-grants/core library py-sdk Related to Python SDK python Issue or PR related to Python tooling sdk Issue or PR related to our SDKs typescript Issue or PR related to TypeScript tooling website Issues related to the website

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Core] Add the version headers to every route

2 participants