Skip to content

Commit

Permalink
axum/extract/query: Use serde_path_to_error to report key that fail…
Browse files Browse the repository at this point in the history
…ed to parse
  • Loading branch information
Turbo87 committed Dec 18, 2024
1 parent fb68828 commit 39f91b5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
3 changes: 2 additions & 1 deletion axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ macros = ["dep:axum-macros"]
matched-path = []
multipart = ["dep:multer"]
original-uri = []
query = ["dep:serde_urlencoded"]
query = ["dep:form_urlencoded", "dep:serde_urlencoded", "dep:serde_path_to_error"]
tokio = ["dep:hyper-util", "dep:tokio", "tokio/net", "tokio/rt", "tower/make", "tokio/macros"]
tower-log = ["tower/log"]
tracing = ["dep:tracing", "axum-core/tracing"]
Expand Down Expand Up @@ -68,6 +68,7 @@ tower-service = "0.3"
# optional dependencies
axum-macros = { path = "../axum-macros", version = "0.5.0-rc.1", optional = true }
base64 = { version = "0.22.1", optional = true }
form_urlencoded = { version = "1.1.0", optional = true }
hyper = { version = "1.1.0", optional = true }
hyper-util = { version = "0.1.3", features = ["tokio", "server", "service"], optional = true }
multer = { version = "3.0.0", optional = true }
Expand Down
8 changes: 5 additions & 3 deletions axum/src/extract/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ where
_state: &S,
) -> Result<Option<Self>, Self::Rejection> {
if let Some(query) = parts.uri.query() {
let value = serde_urlencoded::from_str(query)
let deserializer = serde_urlencoded::Deserializer::new(form_urlencoded::parse(query.as_bytes()));
let value = serde_path_to_error::deserialize(deserializer)
.map_err(FailedToDeserializeQueryString::from_err)?;
Ok(Some(Self(value)))
} else {
Expand Down Expand Up @@ -121,8 +122,9 @@ where
/// ```
pub fn try_from_uri(value: &Uri) -> Result<Self, QueryRejection> {
let query = value.query().unwrap_or_default();
let deserializer = serde_urlencoded::Deserializer::new(form_urlencoded::parse(query.as_bytes()));
let params =
serde_urlencoded::from_str(query).map_err(FailedToDeserializeQueryString::from_err)?;
serde_path_to_error::deserialize(deserializer).map_err(FailedToDeserializeQueryString::from_err)?;
Ok(Query(params))
}
}
Expand Down Expand Up @@ -201,7 +203,7 @@ mod tests {

let res = client.get("/?n=hi").await;
assert_eq!(res.status(), StatusCode::BAD_REQUEST);
assert_eq!(res.text().await, "Failed to deserialize query string: invalid digit found in string");
assert_eq!(res.text().await, "Failed to deserialize query string: n: invalid digit found in string");
}

#[test]
Expand Down

0 comments on commit 39f91b5

Please sign in to comment.