docs(openapi): declare the 429 and 500 every route can answer with - #297
Merged
Conversation
openapi.json contained zero 429 responses while seventeen routes carry a per-flow
limiter and every route sits behind the global one, and the 500 from the
top-level error handler was declared only where a handler happened to mention it.
Both are produced by middleware, and defineRoute builds documented responses from
schemas.response alone, so neither reached the registry. src/generated/api.ts is
emitted from the spec and committed, so a consumer got a response union that
could not see either case.
defineRoute now adds both to every route it registers, the way it already adds
the 400 validation response, and a route that declares one itself keeps its own.
Both are the canonical { error } body the limiters and the error handler send.
Documentation only: these feed the OpenAPI registry, not the runtime response
validation, which still reads schemas.response alone.
Closes #245
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #245.
What
openapi.jsoncontained zero429responses. Seventeen route definitions attach a per-flow limiter and every route sits behinddynamicRateLimit, so a429was reachable on effectively every endpoint and documented on none. The500from the top-level error handler had the same cause and was declared only on the 37 routes whose handlers happened to mention it.src/generated/api.tsis emitted from the spec and committed, so a consumer reading it, or generating their own client, got a response union with neither case in it and would write code that cannot see them.How
defineRouteadds both to every route it registers, which is the precedent the issue points at: it already injects the400ValidationErrorSchemafor any route that validates a request. A route that declares one of them itself keeps its own, the same rule the400injection follows.Both are declared as
ErrorSchema, and that is true rather than aspirational: the limiters sendTOO_MANY_REQUESTS_BODY, which is{ error }, and the error handler sends{ error: 'Internal server error' }.All 90 operations now carry both.
openapi.jsonandsrc/generated/api.tsare regenerated withnpm run generate:api, which is most of the diff.No runtime change
These responses feed
registry.registerPathonly. Runtime response validation inwrappedHandlerreadsschemas.responsedirectly, which this does not touch, so no route starts or stops validating anything, and no body changes.The CORS 403, deliberately left out
The issue also suggests the CORS
403in the same pass. It is excluded, because declaring it would document something untrue: that handler answersres.status(403).json({ message: 'CORS policy does not allow this origin.' }), with noerrorfield, so it does not match the canonical shape thatdocs/security-posture.mdandtests/unit/routes/errorShapeCoverage.spec.tsdescribe as invariant. That test only inspects declared responses, which is why it never caught it.Fixing it means changing a response body rather than documenting one, which is a behavior change and wants its own review. Say the word and I will file it.
Tests
The existing
defineRoutespec asserted the exact default response object, so it now asserts the200alongside the two injected statuses. One test added for a route declaring its own429, which must survive untouched.tests/unit/openapi/generatedContract.spec.tspasses, so the committed spec and the route definitions agree.Checks
npm run typecheck,npm run lint,npm run format:check,npm run build: cleannpm run test:run: 114 files, 1362 passed, 1 skipped, 1 todonpm run coverage: 98.81% statements, 96.08% branches, 98.97% functions, 99.01% lines, all above threshold