Skip to content

fix: return M_BAD_JSON for valid-JSON-wrong-shape request bodies#235

Merged
sufforest merged 1 commit into
mainfrom
wzy/json-errcode-bad-vs-not-json
Jul 3, 2026
Merged

fix: return M_BAD_JSON for valid-JSON-wrong-shape request bodies#235
sufforest merged 1 commit into
mainfrom
wzy/json-errcode-bad-vs-not-json

Conversation

@sufforest

Copy link
Copy Markdown
Owner

The Json<T> extractor returned M_NOT_JSON for every deserialization failure, including bodies that are valid JSON but don't fit a typed endpoint's shape (a missing required key, or a wrong-typed value). The spec reserves M_NOT_JSON for bodies that aren't valid JSON at all and uses M_BAD_JSON for "valid JSON, but malformed in some way, e.g. missing required keys or invalid values."

Classify the serde_json error: Category::DataM_BAD_JSON; Syntax/Eof/IoM_NOT_JSON. Json<Value> handlers are unaffected (any valid JSON deserializes to Value, so Data never fires — they keep returning M_NOT_JSON only for genuinely unparseable bodies, and do their own shape validation with M_BAD_JSON).

Existing M_NOT_JSON cases are preserved: invalid UTF-8 and plain non-JSON bodies classify as Syntax, not Data.

Test covers the four cases (syntax, eof, wrong-type, missing-field).

The Json extractor returned M_NOT_JSON for every deserialization failure,
including bodies that are valid JSON but don't fit a typed endpoint's shape
(a missing key or wrong-typed value) — the spec calls those M_BAD_JSON, and
reserves M_NOT_JSON for bodies that aren't valid JSON at all. Classify the
serde error: Category::Data → M_BAD_JSON, Syntax/Eof → M_NOT_JSON.
@sufforest sufforest merged commit 6762d06 into main Jul 3, 2026
11 checks passed
@sufforest sufforest deleted the wzy/json-errcode-bad-vs-not-json branch July 3, 2026 17:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant