Skip to content

Commit

Permalink
restore the older query method
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshuawuyts committed May 24, 2020
1 parent aa25e97 commit c4d4732
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/cookies/middleware.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<State: Send + Sync + 'static> Middleware<State> for CookiesMiddleware {
let cookie_data = CookieData::from_request(&ctx);
// no cookie data in ext context, so we try to create it
let content = cookie_data.content.clone();
ctx = ctx.set_ext(cookie_data);
ctx.set_ext(cookie_data);
content
};

Expand Down
12 changes: 10 additions & 2 deletions src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl<State> Request<State> {
}

/// Set a request extension value.
pub fn set_ext<T: Send + Sync + 'static>(mut self, val: T) {
pub fn set_ext<T: Send + Sync + 'static>(&mut self, val: T) -> Option<T> {
self.req.ext_mut().insert(val)
}

Expand Down Expand Up @@ -272,7 +272,15 @@ impl<State> Request<State> {

/// Get the URL querystring.
pub fn query<T: serde::de::DeserializeOwned>(&self) -> crate::Result<T> {
self.req.query()
// Default to an empty query string if no query parameter has been specified.
// This allows successful deserialisation of structs where all fields are optional
// when none of those fields has actually been passed by the caller.
let query = self.url().query().unwrap_or("");
serde_qs::from_str(query).map_err(|e| {
// Return the displayable version of the deserialisation error to the caller
// for easier debugging.
crate::Error::from_str(StatusCode::BadRequest, format!("{}", e))
})
}

/// Reads the entire request body into a byte buffer.
Expand Down
2 changes: 1 addition & 1 deletion tests/querystring.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async fn unsuccessfully_deserialize_query() {
let app = get_server();
let req = http_types::Request::new(Method::Get, Url::parse("http://example.com/").unwrap());
let mut res: http::Response = app.respond(req).await.unwrap();
assert_eq!(res.status(), 400);
assert_eq!(res.status(), 400_u16);

let mut body = String::new();
res.read_to_string(&mut body).await.unwrap();
Expand Down

0 comments on commit c4d4732

Please sign in to comment.