fix: return structured JSON for malformed request bodies#4441
fix: return structured JSON for malformed request bodies#4441dymchenkko wants to merge 1 commit into
Conversation
JSON-body endpoints returned axum's plain-text rejection when a request
body failed to deserialize. Add a Json extractor that renders these
errors in the API's { errorType, description } format and apply it to
all JSON-body endpoints.
Closes cowprotocol#4439
Closes cowprotocol#4440
|
All contributors have signed the CLA ✍️ ✅ |
There was a problem hiding this comment.
Code Review
This pull request introduces a custom Json extractor in crates/orderbook/src/api/extract.rs that wraps Axum's native extractor. When request deserialization fails, it returns a structured JSON error response containing errorType and description instead of plain text. The API endpoints in cancel_order.rs, debug_simulation.rs, post_order.rs, post_quote.rs, and put_app_data.rs have been updated to use this new extractor. Additionally, the OpenAPI specification (openapi.yml) has been updated to document the Error schema for 422 responses. No critical issues were found, and there is no feedback to provide.
|
I have read the CLA Document and I hereby sign the CLA |
Description
JSON-body endpoints returned Axum's plain-text rejection when a request body
failed to deserialize (empty
{}, missing field, invalid token address). Everyother error in this API is structured JSON, so clients that parse all responses
as JSON break. This makes that path consistent.
Reviewer notes: I added a generic
Errorschema (vs. per-endpoint enums) — opento switching. Invalid syntax is
400, already documented per endpoint, so Ionly documented the structured body on
422.Changes
Json<T>extractor (api/extract.rs) wrappingaxum::Jsonthat, on aJsonRejection, returns the API'serror("InvalidJson", …)with therejection's status (422/400). Identical to
axum::Jsonon success.post_quote,post_order,cancel_order,put_app_data,debug_simulation(cancel_orders/orders/by_uidsparse raw bytes, unaffected).Errorschema, referenced from the affected422s.How to test
cargo test -p orderbook --lib api::extract— covers{}→ 422 JSON (#4439),invalid field value → 422 JSON (#4440), invalid syntax → 400 JSON.
Manual:
POST /api/v1/quotewith body{}→ now422+application/json{"errorType":"InvalidJson",…}instead oftext/plain.Related Issues
Fixes #4439
Fixes #4440