From ec779422ed83b16ff130bcef27d4360c7348cd34 Mon Sep 17 00:00:00 2001 From: hubertshelley Date: Fri, 8 Nov 2024 15:50:47 +0800 Subject: [PATCH] feat: support vec in query by serde_html_form --- silent/Cargo.toml | 2 +- silent/src/core/request.rs | 33 ++++++++++++++++++++++++++++++--- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/silent/Cargo.toml b/silent/Cargo.toml index cadd4d9..3fd8ddb 100644 --- a/silent/Cargo.toml +++ b/silent/Cargo.toml @@ -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"] } diff --git a/silent/src/core/request.rs b/silent/src/core/request.rs index b8a674c..1ca2ac7 100644 --- a/silent/src/core/request.rs +++ b/silent/src/core/request.rs @@ -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) } @@ -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(); @@ -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), @@ -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, + } + + #[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::().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::().unwrap(); + } +}