Repository navigation
feat(apidocs): Check test responses against the OpenAPI schema - #131077
Conversation
0b2e0e5 to
b02d5c9
Compare
Every response the Django test client gets from a public operation is checked against the schema drf-spectacular generates from the code: its status must be documented (S036) and its JSON body must match that status's schema (S037). The pytest controller generates the schema in a subprocess while xdist workers start, so it never goes stale. Findings are keyed like the API guidelines findings (endpoint file, class and method, status) and recorded in tests/apidocs/openapi-response-baseline.json by `make api-response-baseline`, in the exceptions.json entry shape. CI runs it in report mode: findings that aren't baselined become warning annotations and every shard uploads its report. Strict mode, which fails the test, is opt-in. Refs #131073 Co-Authored-By: BYK <[email protected]>
b02d5c9 to
b31581d
Compare
|
No findings on the latest commit. |
📊 Type Coverage Diff
🔍 21 new type safety issues introduced
Non-null assertions (
Type assertions (
| This is informational only and does not block the PR. |
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit b31581d. Configure here.
A request through an alias route, such as /organizations/{org}/groups/{id}/ or
/issues/{id}/ without the organization, matched no schema path and was skipped.
When the request path matches no template, map the resolved route to its schema
path with the enumerator and _canonical_issue_path the schema build uses.
Refs #131073
Co-Authored-By: BYK <[email protected]>
|
Ported the route-alias mapping from the closed #131098 in be2aca8. Until now, a request through an alias route found no schema path and was skipped without a report. That covers One behavior change: I added regression tests to Overhead measured on #131098, which used the same per-operation validator caching: on a 452-test endpoint slice run serially, the check took about 0.9 ms per checked response (0.51–0.53 s in total for 567 responses, with the schema load). That is roughly 0.35% of the run, and smaller than the run-to-run noise in wall time. |

Refs #131073
Our OpenAPI docs drift from what endpoints actually return, and nothing catches it. This compares the responses tests get from public operations with the schema drf-spectacular generates from the code:
responses304, 429 and 5xx are exempt: a 304 only answers a conditional request, the rate limiter can return a 429 from any endpoint, and a 5xx is a bug rather than part of the contract. S035 is taken by #130924.
This stands alone and doesn't touch the API guidelines work in #131117 → #131088 → #131135 → #131407. Findings use the same signature (endpoint file,
EndpointClass.method,status:<code>, owner) and entry shape as that stack'sexceptions.json, which is where the baseline goes once the stack lands.The pytest plugin is opt-in, with two modes. Report mode records findings and annotates endpoints whose findings aren't baselined. Strict mode also fails the test that got one. The schema is generated while xdist workers start, so it always matches the code and doesn't need node.
A new baseline tool adds a run's findings to a baseline file. It only touches the endpoint methods that run called, and keeps reviewed statuses.
Backend CI runs in report mode, so nothing blocks merges. CI moves to strict mode together with the baseline.
via @BYK.
--
View Session [Sentry]