1
1
use axum:: {
2
- extract:: FromRequestParts ,
2
+ extract:: { FromRequestParts , OptionalFromRequestParts } ,
3
3
response:: { IntoResponse , Response } ,
4
4
Error ,
5
5
} ;
96
96
}
97
97
}
98
98
99
+ impl < T , S > OptionalFromRequestParts < S > for Query < T >
100
+ where
101
+ T : DeserializeOwned ,
102
+ S : Send + Sync ,
103
+ {
104
+ type Rejection = QueryRejection ;
105
+
106
+ async fn from_request_parts (
107
+ parts : & mut Parts ,
108
+ _state : & S ,
109
+ ) -> Result < Option < Self > , Self :: Rejection > {
110
+ if let Some ( query) = parts. uri . query ( ) {
111
+ let value = serde_html_form:: from_str ( query)
112
+ . map_err ( |err| QueryRejection :: FailedToDeserializeQueryString ( Error :: new ( err) ) ) ?;
113
+ Ok ( Some ( Self ( value) ) )
114
+ } else {
115
+ Ok ( None )
116
+ }
117
+ }
118
+ }
119
+
99
120
axum_core:: __impl_deref!( Query ) ;
100
121
101
122
/// Rejection used for [`Query`].
@@ -182,9 +203,11 @@ impl std::error::Error for QueryRejection {
182
203
///
183
204
/// [example]: https://github.com/tokio-rs/axum/blob/main/examples/query-params-with-empty-strings/src/main.rs
184
205
#[ cfg_attr( docsrs, doc( cfg( feature = "query" ) ) ) ]
206
+ #[ deprecated = "Use Option<Query<_>> instead" ]
185
207
#[ derive( Debug , Clone , Copy , Default ) ]
186
208
pub struct OptionalQuery < T > ( pub Option < T > ) ;
187
209
210
+ #[ allow( deprecated) ]
188
211
impl < T , S > FromRequestParts < S > for OptionalQuery < T >
189
212
where
190
213
T : DeserializeOwned ,
@@ -204,6 +227,7 @@ where
204
227
}
205
228
}
206
229
230
+ #[ allow( deprecated) ]
207
231
impl < T > std:: ops:: Deref for OptionalQuery < T > {
208
232
type Target = Option < T > ;
209
233
@@ -213,6 +237,7 @@ impl<T> std::ops::Deref for OptionalQuery<T> {
213
237
}
214
238
}
215
239
240
+ #[ allow( deprecated) ]
216
241
impl < T > std:: ops:: DerefMut for OptionalQuery < T > {
217
242
#[ inline]
218
243
fn deref_mut ( & mut self ) -> & mut Self :: Target {
@@ -260,6 +285,7 @@ impl std::error::Error for OptionalQueryRejection {
260
285
}
261
286
262
287
#[ cfg( test) ]
288
+ #[ allow( deprecated) ]
263
289
mod tests {
264
290
use super :: * ;
265
291
use crate :: test_helpers:: * ;
0 commit comments