Validate the projected examples against the schema the projection publishes (#638)

Merged
JE
4mo ago
+1124-238

Closes #638.

Stacked on #648. This branch is based on feat/upcoming-schedule-projection, not on main, and cannot merge before it. #648's BareResponseExampleMapper is what makes this gate's central claim expressible at all — see Why this depends on #648. Review the top three commits; the base is #648's.

spec/smithy-build.json's jsonAdd can append to a schema's required array in the OpenAPI projection. smithy validate checks @examples against the Smithy model, where the member is still natively optional — the requiredness only exists downstream of it. Nothing compared the projected examples to the projected schema, so a projection-added required field could be missing from a published example with every gate green.

That is not a hypothesis. #637 shipped both GetTodolistOrGroup response examples without color while Todolist.required declared it, and a bot reviewer caught it. This is the gate that would have.

What it checks

Every example openapi.json publishes, against the sibling schema, through the same composition-aware walk the fixture guard uses:

==> Projected examples validate — 37 checked (10 response, 5 request-body, 22 parameter)

Nothing is skipped. examples maps hold Example Objects (value under value, $refs into components/examples resolved, externalValue reported as skipped rather than fetched — an offline gate that reaches the network is a gate that skips when the network is down); the singular example is the value. Conflating those would silently unwrap any payload with a field named value.

Red proof

The #637 defect reproduced in today's projection — color deleted from both GetTodolistOrGroup examples, Todolist.required still declaring it:

Projected-example validation failed:
  - GET /{accountId}/todolists/{id} (GetTodolistOrGroup) responses/200/application/json/examples/GetTodolistOrGroup_example1: missing required field `color`
  - GET /{accountId}/todolists/{id} (GetTodolistOrGroup) responses/200/application/json/examples/GetTodolistOrGroup_example2: missing required field `color`

These examples are published in openapi.json but contradict the schema they sit under.
A projection-added `required` entry (spec/smithy-build.json `jsonAdd` .../required/-) is not
checked by `smithy validate`, which only sees the pre-projection Smithy model — so the example
has to be updated in the same place the requiredness came from. See #638.
REAL_EXIT=1

Two errors, both naming color, nothing else. Unmodified tree: REAL_EXIT=0. Both exit codes were written into the captured log by the runner and grepped back out, not read off a terminal.

Why not the historical commits. An earlier revision of this PR proved the gate against f9b9c92a5 directly and bisected it green at 12b02cda8. That proof no longer isolates anything and I am not quoting it: those trees predate BareResponseExampleMapper, so under this gate their wrapped examples are themselves the fault — 47 errors at f9b9c92a5 and 47 at 12b02cda8 alike, color named in both but buried. A bisect that is red on both sides of the fix is not evidence. The mutation above is the same defect stated in the projection this gate actually guards.

Why this depends on #648

The bare-response mappers rewrite a single-property *ResponseContent schema into the bare payload — BC3 returns bare bodies, Smithy's restJson1 needs a wrapper. Until #644 they rewrote the schema only, so every published response example still carried the wrapper:

schema:  {"$ref": ".../Todolist"}
example: {"result": { ...todolist... }}     <- before #644

#648's BareResponseExampleMapper mirrors the unwrapping onto examples, so the two now agree and this gate compares them directly.

It deliberately does not unwrap anything itself. A gate that also accepted the wrapped shape could not tell a correct example from a regression in that mapper. Comparing exactly what is published against exactly the schema it is published under is the whole claim — and it means this gate now guards BareResponseExampleMapper as well. Self-test case 7 puts the wrapper back on one example and asserts the run goes red.

A count I had wrong

An earlier revision of this PR reported "2 validated response examples". That number was not measuring what it sounded like. It counted examples that passed because this gate unwrapped them first — the published documents were still self-contradictory, and the gate was validating a payload the spec did not publish.

Post-#648 the number means what it says: 10 of 10 response examples validate as published, no unwrapping, no skips. The correction came from the GetUpcomingSchedule lane; I confirmed it before changing anything by running a no-unwrap build of this gate against both trees — red on main (47 errors), green on #648 (37 checked, 0 failures).

Corrections to the issue

1. GetTodolistOrGroup is not the only operation with a response example. The issue says the other four are input-only. Four operations carry responses/200/.../examples: GetTodolistOrGroup, ListRecordings, UpdateProjectAccess and UpdateSubscription. Only TrashRecording is parameters-only. What was true one level up is that only GetTodolistOrGroup declared an output — which is precisely what #644 fixed.

2. conformance/runner/ruby/schema-walker.rb is not reused. The issue suggests it for resolving an operation's response schema. This walk finds examples by descending paths itself, so the schema is already in hand at every example — and find_response_schema(operationId) returns only the first 2xx schema for an operation, not the one attached to the response the example sits under. Less precise, not more. instance_errors — the reuse that matters — is reused.

Reuse

instance_errors and the merged_constraints walk beneath it were top-level methods inside scripts/check-fixture-coverage.rb, which runs its whole check on load — so nothing could require them without also running the fixture guard. The first commit moves them verbatim into scripts/schema_instance_validator.rb, in the scripts/bc3_route_normalizer.rb style: one definition, two callers, no parallel validator to drift. module_function plus a top-level include leaves every existing call site unchanged; the fixture guard's own self-test (1 positive + 8 negative + 20 synthetic cases) is what says the move was behaviour-preserving.

Self-test

1 positive + 14 negative/skip cases through the real checker via PROJECTED_EXAMPLES_OPENAPI, each a deep copy of the real openapi.json with one mutation.

Non-vacuity is measured, not asserted — ten guards removed one at a time from scratch copies via PROJECTED_EXAMPLES_CHECKER. Every guard turns specific cases red; every case is red under at least one:

guard removedcases that go red
response-example walkcontrol, 1, 2, 3, 4, 7, 8, 9, 11, 12, 13, 14
request-body-example walk5
parameter-example walk6
liveness floor (>= 1 response example)10
components/examples $ref resolution11
externalValue skip12
value-less Example Object rejection13
Example Object type check14
triage keeps :error distinct from :skip11, 13, 14
instance_errors result discarded1, 2, 3, 4, 5, 6, 7, 8, 9

The last row matters most: it unwires the validator while leaving every walk intact, so a gate that visits all 37 examples and concludes nothing still goes red. Coverage and judgement are pinned separately — a gate that iterates correctly and asserts nothing is the failure a green suite hides best.

The gate also refuses to pass vacuously at runtime: validating zero response examples is a failure, not a green run. Case 10 strips every response example in the document and asserts exactly that.

Wiring

Both places, because membership in one is not membership in the other — spec-gates enumerates its targets rather than invoking make check, which is the #580 gap:

  • check-targets: (the list check: sub-makes, since #631 wrapped it in a lockfile snapshot) — 42 → 43, re-derived from the line at wiring time and written down nowhere.
  • A spec-gates step, placed ahead of that job's lockfile diagnostics so those stay last by design. Ruby and openapi.json are the gate's only inputs, so it needs nothing that job does not already have. Run under LC_ALL=C, matching the fixture-coverage job: the reads are pinned to UTF-8, and exercising the non-UTF-8-locale path in CI is what keeps them that way.

Verification

Full make check on this branch, exit code written into the captured log by the runner and grepped back out:

Lockfiles unchanged by the checks (9 files, byte-identical).
==> All checks passed
REAL_EXIT=0

HEAD captured either side of the run, so the tree provably did not move underneath it:

PRE_SHA =b7f86d66eea351dff7c8a085295332f95d2d1c25
POST_SHA=b7f86d66eea351dff7c8a085295332f95d2d1c25

And the gate demonstrably ran inside that invocation rather than being silently skipped — a no-op gate being the exact failure this one exists to prevent:

6544:==> Projected examples validate — 37 checked (10 response, 5 request-body, 22 parameter)
6545:==> projected-example self-test (checker: scripts/check-projected-examples.rb)
6561:==> projected-example self-test passed — 1 positive + 14 negative/skip cases

Working tree clean afterwards. make lint-actions (actionlint + zizmor) clean on the workflow change. The gate and its self-test both pass under LC_ALL=C as well as UTF-8; it runs in ~0.2s and is cwd-independent.


Summary by cubic

Adds a gate that validates every projected example in openapi.json against its sibling schema to catch projection-added required mismatches and other contradictions. It also rejects root-null examples when the schema is not nullable and treats unresolvable schema $ref as errors. Closes #638.

  • New Features

    • Added scripts/check-projected-examples.rb to validate response, request-body, and parameter examples against their sibling schema. Resolves #/components/examples refs, reports externalValue as skipped, compares examples exactly as published (no unwrapping), and fails if zero response examples are validated. Added check-projected-examples make target, wired into check-targets, and added a CI step (runs under LC_ALL=C). Self-test covers 2 positive + 18 negative/skip cases.
  • Refactors

    • Extracted the composition-aware validator to scripts/schema_instance_validator.rb and reused it in check-fixture-coverage.rb. Handles $ref (incl. siblings), allOf, and anyOf/oneOf as at-least-one; checks required fields, types, and nullability. Root nulls are now judged against schema nullability, and an unresolvable schema $ref is a hard error.