Skip to content

Commit c4d4732

Browse files
committed
restore the older query method
1 parent aa25e97 commit c4d4732

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

src/cookies/middleware.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ impl<State: Send + Sync + 'static> Middleware<State> for CookiesMiddleware {
4545
let cookie_data = CookieData::from_request(&ctx);
4646
// no cookie data in ext context, so we try to create it
4747
let content = cookie_data.content.clone();
48-
ctx = ctx.set_ext(cookie_data);
48+
ctx.set_ext(cookie_data);
4949
content
5050
};
5151

src/request.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl<State> Request<State> {
235235
}
236236

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

@@ -272,7 +272,15 @@ impl<State> Request<State> {
272272

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

278286
/// Reads the entire request body into a byte buffer.

tests/querystring.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ async fn unsuccessfully_deserialize_query() {
5757
let app = get_server();
5858
let req = http_types::Request::new(Method::Get, Url::parse("http://example.com/").unwrap());
5959
let mut res: http::Response = app.respond(req).await.unwrap();
60-
assert_eq!(res.status(), 400);
60+
assert_eq!(res.status(), 400_u16);
6161

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

0 commit comments

Comments
 (0)