fix(lit-api-server): autogenerate 429 in OpenAPI spec via CpuAvailable guard (CPL-202)#435
Open
GTC6244 wants to merge 1 commit into
Open
Conversation
…e guard (CPL-202) The autogenerated OpenAPI spec only showed `default` responses because explicit status codes were never declared at their source. The 429 on `/lit_action` comes from the `CpuAvailable` request guard, but its `OpenApiFromRequest` impl declared no responses, so the 429 had to be manually string-patched into the spec in the `openapi_spec` binary. Override `CpuAvailable::get_responses()` to declare the 429. rocket_okapi's codegen merges each guard's `get_responses()` into every operation that uses the guard, so the 429 is now genuinely autogenerated and appears on any route behind the guard, not just `/lit_action`. Remove the manual post-processing patch from the binary. Spec output is byte-identical to before, so the checked-in k6 client needs no regeneration. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR moves the 429 Too Many Requests OpenAPI documentation for CPU overload shedding from a brittle, endpoint-specific post-processing patch into the actual source of the behavior: the CpuAvailable Rocket request guard. This ensures any route protected by CpuAvailable automatically and consistently advertises the 429 response in the generated OpenAPI spec.
Changes:
- Added
OpenApiFromRequest::get_responses()toCpuAvailableto declare a429response (with retry guidance) in the generated OpenAPI spec. - Removed the manual
/lit_action-specific429injection logic from theopenapi_specbinary. - Simplified
openapi_spec.rsimports after removing the patching code.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
lit-api-server/src/core/v1/guards/cpu_overload.rs |
Adds guard-level OpenAPI response metadata so 429 is autogenerated wherever CpuAvailable is used. |
lit-api-server/src/bin/openapi_spec.rs |
Removes manual spec mutation; binary now only generates and prints the spec. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Summary
The autogenerated OpenAPI spec only showed
defaultresponses because explicit status codes were never declared at their source. The429on/lit_actioncomes from theCpuAvailablerequest guard, but itsOpenApiFromRequestimpl declared no responses — so commit #256 had to manually string-patch the429into the spec inside theopenapi_specbinary. Fragile, and it didn't scale to other routes using the same guard.This PR declares the
429at its actual source:cpu_overload.rs— overrideCpuAvailable::get_responses()to declare the429.rocket_okapi's codegen macro merges each guard'sget_responses()into every operation that uses the guard, so the429now appears automatically on any route behindCpuAvailable, not just/lit_action.openapi_spec.rs— delete the manual post-processing patch; the binary now just generates and prints.get_responses()is the idiomatic hook for this — its rocket_okapi docstring cites declaring guard responses like "401 Unauthorized" as the intended use case.Resolves CPL-202.
Verification
main(diffed both generated specs) — so the checked-in k6 client (k6/litApiServer.ts) needs no regeneration and theopenapi-spec-checkCI gate stays green. The generated/lit_actionresponses are["default", "429"]with the retry-backoff description intact.cargo +1.91 fmt --checkclean.cargo +1.91 clippyclean (no warnings).cpu_overloadguard tests pass, includingguard_returns_429_when_overloaded.This is a net refactor: same spec, but the
429is now genuinely autogenerated and self-documenting at the guard rather than hardcoded in a one-off patch.Test plan
cargo +1.91 test -p lit-api-server --lib cpu_overload— 4 passedcargo +1.91 fmt --check— cleancargo +1.91 clippy --bin openapi_spec --lib— cleanmain— identical🤖 Generated with Claude Code