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 |
|
This pull request has been marked as stale because it has been inactive a while. Please update this pull request or it will be automatically closed. |
squadgazzz
left a comment
There was a problem hiding this comment.
This is a breaking change for the public API. If any of the clients are parsing errors (which is most likely the case, including our front-end), this change would lead to errors on their end.
|
@dymchenkko , thank you for your contribution! Please fix the PR description formatting. AI tends to generate in the wrong way. |
|
@squadgazzz Hey, thanks for the review! I want to make sure I understand the concern correctly. Before this change, when a request had malformed JSON, Axum returned a plain-text error instead of JSON. Every other error in these endpoints already returns {errorType, description} JSON, that's actually why #4439 and #4440 were filed as bugs. Could you point to a specific client that depends on the plain-text format? That would help me understand what exactly would break. |
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