File tree 3 files changed +12
-4
lines changed 3 files changed +12
-4
lines changed Original file line number Diff line number Diff line change @@ -45,7 +45,7 @@ impl<State: Send + Sync + 'static> Middleware<State> for CookiesMiddleware {
45
45
let cookie_data = CookieData :: from_request ( & ctx) ;
46
46
// no cookie data in ext context, so we try to create it
47
47
let content = cookie_data. content . clone ( ) ;
48
- ctx = ctx . set_ext ( cookie_data) ;
48
+ ctx. set_ext ( cookie_data) ;
49
49
content
50
50
} ;
51
51
Original file line number Diff line number Diff line change @@ -235,7 +235,7 @@ impl<State> Request<State> {
235
235
}
236
236
237
237
/// 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 > {
239
239
self . req . ext_mut ( ) . insert ( val)
240
240
}
241
241
@@ -272,7 +272,15 @@ impl<State> Request<State> {
272
272
273
273
/// Get the URL querystring.
274
274
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
+ } )
276
284
}
277
285
278
286
/// Reads the entire request body into a byte buffer.
Original file line number Diff line number Diff line change @@ -57,7 +57,7 @@ async fn unsuccessfully_deserialize_query() {
57
57
let app = get_server ( ) ;
58
58
let req = http_types:: Request :: new ( Method :: Get , Url :: parse ( "http://example.com/" ) . unwrap ( ) ) ;
59
59
let mut res: http:: Response = app. respond ( req) . await . unwrap ( ) ;
60
- assert_eq ! ( res. status( ) , 400 ) ;
60
+ assert_eq ! ( res. status( ) , 400_u16 ) ;
61
61
62
62
let mut body = String :: new ( ) ;
63
63
res. read_to_string ( & mut body) . await . unwrap ( ) ;
You can’t perform that action at this time.
0 commit comments