Skip to content

Commit

Permalink
support multi-value items in Query
Browse files Browse the repository at this point in the history
  • Loading branch information
robjtede committed Apr 23, 2022
1 parent 3233ad1 commit b36d53b
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 4 deletions.
1 change: 1 addition & 0 deletions actix-web-lab/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased - 2022-xx-xx
- `Query` extractor now supports collecting multi-value items into a `Vec`.


## 0.16.0 - 2022-04-11
Expand Down
2 changes: 1 addition & 1 deletion actix-web-lab/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ once_cell = "1.8"
pin-project-lite = "0.2.7"
serde = "1"
serde_json = "1"
serde_urlencoded = "0.7"
serde_html_form = "0.1"
subtle = "2.4"
tokio = { version = "1.13.1", features = ["sync", "macros"] }
tracing = { version = "0.1.30", features = ["log"] }
Expand Down
2 changes: 1 addition & 1 deletion actix-web-lab/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
- `LocalData`: app data/state that uses an `Rc` internally, avoiding atomic overhead (alternative to `Data<RwLock<T>>`) [(docs)](https://docs.rs/actix-web-lab/0.16.0/actix_web_lab/extract/struct.DataSwap.html)
- `Json`: simplified JSON extractor with const-generic limits [(docs)](https://docs.rs/actix-web-lab/0.16.0/actix_web_lab/extract/struct.Json.html)
- `Path`: simplified path parameter extractor that supports destructuring [(docs)](https://docs.rs/actix-web-lab/0.16.0/actix_web_lab/extract/struct.Path.html)
- `Query`: simplified query-string extractor [(docs)](https://docs.rs/actix-web-lab/0.16.0/actix_web_lab/extract/struct.Query.html)
- `Query`: simplified query-string extractor that can also collect multi-value items [(docs)](https://docs.rs/actix-web-lab/0.16.0/actix_web_lab/extract/struct.Query.html)
- `RequestSignature`: wraps an extractor and calculates a request signature alongside [(docs)](https://docs.rs/actix-web-lab/0.16.0/actix_web_lab/extract/struct.RequestSignature.html)

### Macros
Expand Down
19 changes: 17 additions & 2 deletions actix-web-lab/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ impl<T: DeserializeOwned> Query<T> {
/// assert!(numbers.get("three").is_none());
/// ```
pub fn from_query(query_str: &str) -> Result<Self, QueryPayloadError> {
serde_urlencoded::from_str::<T>(query_str)
serde_html_form::from_str::<T>(query_str)
.map(Self)
.map_err(QueryPayloadError::Deserialize)
}
Expand Down Expand Up @@ -118,7 +118,7 @@ impl<T: DeserializeOwned> FromRequest for Query<T> {

#[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
serde_urlencoded::from_str::<T>(req.query_string())
serde_html_form::from_str::<T>(req.query_string())
.map(|val| ready(Ok(Query(val))))
.unwrap_or_else(move |e| {
let err = QueryPayloadError::Deserialize(e);
Expand Down Expand Up @@ -166,6 +166,21 @@ mod tests {
assert_eq!(s.id, "test1");
}

#[actix_web::test]
async fn extract_array() {
#[derive(Debug, Deserialize)]
struct Test {
#[serde(rename = "user")]
users: Vec<String>,
}

let req = TestRequest::with_uri("/?user=foo&user=bar").to_srv_request();
let s = Query::<Test>::from_query(req.query_string()).unwrap();

assert_eq!(s.users[0], "foo");
assert_eq!(s.users[1], "bar");
}

#[actix_web::test]
async fn test_request_extract() {
let req = TestRequest::with_uri("/name/user1/").to_srv_request();
Expand Down

0 comments on commit b36d53b

Please sign in to comment.