1
1
use axum:: {
2
2
async_trait,
3
- extract:: FromRequestParts ,
3
+ extract:: { FromRequestParts , OptionalFromRequestParts } ,
4
4
response:: { IntoResponse , Response } ,
5
5
Error ,
6
6
} ;
71
71
}
72
72
}
73
73
74
+ #[ async_trait]
75
+ impl < T , S > OptionalFromRequestParts < S > for Query < T >
76
+ where
77
+ T : DeserializeOwned ,
78
+ S : Send + Sync ,
79
+ {
80
+ type Rejection = QueryRejection ;
81
+
82
+ async fn from_request_parts (
83
+ parts : & mut Parts ,
84
+ _state : & S ,
85
+ ) -> Result < Option < Self > , Self :: Rejection > {
86
+ if let Some ( query) = parts. uri . query ( ) {
87
+ let value = serde_html_form:: from_str ( query)
88
+ . map_err ( |err| QueryRejection :: FailedToDeserializeQueryString ( Error :: new ( err) ) ) ?;
89
+ Ok ( Some ( Self ( value) ) )
90
+ } else {
91
+ Ok ( None )
92
+ }
93
+ }
94
+ }
95
+
74
96
axum_core:: __impl_deref!( Query ) ;
75
97
76
98
/// Rejection used for [`Query`].
@@ -152,9 +174,11 @@ impl std::error::Error for QueryRejection {
152
174
///
153
175
/// [example]: https://github.com/tokio-rs/axum/blob/main/examples/query-params-with-empty-strings/src/main.rs
154
176
#[ cfg_attr( docsrs, doc( cfg( feature = "query" ) ) ) ]
177
+ #[ deprecated = "Use Option<Path<_>> instead" ]
155
178
#[ derive( Debug , Clone , Copy , Default ) ]
156
179
pub struct OptionalQuery < T > ( pub Option < T > ) ;
157
180
181
+ #[ allow( deprecated) ]
158
182
#[ async_trait]
159
183
impl < T , S > FromRequestParts < S > for OptionalQuery < T >
160
184
where
@@ -175,6 +199,7 @@ where
175
199
}
176
200
}
177
201
202
+ #[ allow( deprecated) ]
178
203
impl < T > std:: ops:: Deref for OptionalQuery < T > {
179
204
type Target = Option < T > ;
180
205
@@ -184,6 +209,7 @@ impl<T> std::ops::Deref for OptionalQuery<T> {
184
209
}
185
210
}
186
211
212
+ #[ allow( deprecated) ]
187
213
impl < T > std:: ops:: DerefMut for OptionalQuery < T > {
188
214
#[ inline]
189
215
fn deref_mut ( & mut self ) -> & mut Self :: Target {
@@ -231,6 +257,7 @@ impl std::error::Error for OptionalQueryRejection {
231
257
}
232
258
233
259
#[ cfg( test) ]
260
+ #[ allow( deprecated) ]
234
261
mod tests {
235
262
use super :: * ;
236
263
use crate :: test_helpers:: * ;
0 commit comments