Skip to content

Commit

Permalink
Merge pull request #71 from hubertshelley/features/2.0/query_vec_support
Browse files Browse the repository at this point in the history
feat: support vec in query by serde_html_form
  • Loading branch information
hubertshelley authored Nov 8, 2024
2 parents d099dd5 + ec77942 commit a65f2c8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
2 changes: 1 addition & 1 deletion silent/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ serde = { version = "1.0.210", features = ["derive"] }
serde_json = "1.0.128"
uuid = "1.10.0"
url = "2.5.2"
serde_urlencoded = "0.7.1"
serde_html_form = "0.2.6"
mime = "0.3.17"
futures-util = "0.3.31"
chrono = { version = "0.4.38", default-features = false, features = ["clock"] }
Expand Down
33 changes: 30 additions & 3 deletions silent/src/core/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ impl Request {
for<'de> T: Deserialize<'de>,
{
let query = self.uri().query().unwrap_or("");
let params = serde_urlencoded::from_str(query)?;
let params = serde_html_form::from_str(query)?;
Ok(params)
}

Expand Down Expand Up @@ -357,7 +357,7 @@ impl Request {
match content_type.subtype() {
mime::WWW_FORM_URLENCODED => {
let bytes = body.collect().await.unwrap().to_bytes();
serde_urlencoded::from_bytes(&bytes).map_err(SilentError::from)
serde_html_form::from_bytes(&bytes).map_err(SilentError::from)
}
mime::JSON => {
let bytes = body.collect().await.unwrap().to_bytes();
Expand All @@ -371,7 +371,7 @@ impl Request {
}
ReqBody::Once(bytes) => match content_type.subtype() {
mime::WWW_FORM_URLENCODED => {
serde_urlencoded::from_bytes(&bytes).map_err(SilentError::from)
serde_html_form::from_bytes(&bytes).map_err(SilentError::from)
}
mime::JSON => serde_json::from_slice(&bytes).map_err(|e| e.into()),
_ => Err(SilentError::JsonEmpty),
Expand Down Expand Up @@ -413,3 +413,30 @@ impl Request {
(self, url)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[derive(Deserialize, Debug, PartialEq)]
struct TestStruct {
a: i32,
b: String,
#[serde(default, alias = "c[]")]
c: Vec<String>,
}

#[test]
fn test_query_parse_alias() {
let mut req = Request::empty();
*req.uri_mut() = Uri::from_static("http://localhost:8080/test?a=1&b=2&c[]=3&c[]=4");
let _ = req.params_parse::<TestStruct>().unwrap();
}

#[test]
fn test_query_parse() {
let mut req = Request::empty();
*req.uri_mut() = Uri::from_static("http://localhost:8080/test?a=1&b=2&c=3&c=4");
let _ = req.params_parse::<TestStruct>().unwrap();
}
}

0 comments on commit a65f2c8

Please sign in to comment.