Skip to content

Commit

Permalink
axum-extra/extract/form: Use serde_path_to_error to report key that…
Browse files Browse the repository at this point in the history
… failed to parse
  • Loading branch information
Turbo87 committed Dec 20, 2024
1 parent 7e567c3 commit 08864bd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion axum-extra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ cookie-private = ["cookie", "cookie?/private"]
cookie-signed = ["cookie", "cookie?/signed"]
cookie-key-expansion = ["cookie", "cookie?/key-expansion"]
erased-json = ["dep:serde_json", "dep:typed-json"]
form = ["dep:serde_html_form"]
form = ["dep:form_urlencoded", "dep:serde_html_form", "dep:serde_path_to_error"]
json-deserializer = ["dep:serde_json", "dep:serde_path_to_error"]
json-lines = [
"dep:serde_json",
Expand Down
8 changes: 5 additions & 3 deletions axum-extra/src/extract/form.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ where
.await
.map_err(FormRejection::RawFormRejection)?;

serde_html_form::from_bytes::<T>(&bytes)
let deserializer = serde_html_form::Deserializer::new(form_urlencoded::parse(&bytes));

serde_path_to_error::deserialize::<_, T>(deserializer)
.map(Self)
.map_err(|err| FormRejection::FailedToDeserializeForm(Error::new(err)))
}
Expand Down Expand Up @@ -168,7 +170,7 @@ mod tests {
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
assert_eq!(
res.text().await,
"Failed to deserialize form: invalid digit found in string"
"Failed to deserialize form: a: invalid digit found in string"
);

let res = client
Expand All @@ -179,7 +181,7 @@ mod tests {
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
assert_eq!(
res.text().await,
"Failed to deserialize form: invalid digit found in string"
"Failed to deserialize form: a: invalid digit found in string"
);
}
}

0 comments on commit 08864bd

Please sign in to comment.