mirror of
https://github.com/tokio-rs/axum.git
synced 2025-04-01 04:01:48 +02:00
Implement OptionalFromRequestParts for axum-extra's Query<T>
… and deprecate OptionalQuery.
This commit is contained in:
parent
a654c89200
commit
b8f499e774
2 changed files with 32 additions and 2 deletions
|
@ -36,7 +36,10 @@ pub use self::cookie::SignedCookieJar;
|
|||
pub use self::form::{Form, FormRejection};
|
||||
|
||||
#[cfg(feature = "query")]
|
||||
pub use self::query::{OptionalQuery, OptionalQueryRejection, Query, QueryRejection};
|
||||
#[allow(deprecated)]
|
||||
pub use self::query::OptionalQuery;
|
||||
#[cfg(feature = "query")]
|
||||
pub use self::query::{OptionalQueryRejection, Query, QueryRejection};
|
||||
|
||||
#[cfg(feature = "multipart")]
|
||||
pub use self::multipart::Multipart;
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
use axum::{
|
||||
async_trait,
|
||||
extract::FromRequestParts,
|
||||
extract::{FromRequestParts, OptionalFromRequestParts},
|
||||
response::{IntoResponse, Response},
|
||||
Error,
|
||||
};
|
||||
|
@ -71,6 +71,28 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<T, S> OptionalFromRequestParts<S> for Query<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
S: Send + Sync,
|
||||
{
|
||||
type Rejection = QueryRejection;
|
||||
|
||||
async fn from_request_parts(
|
||||
parts: &mut Parts,
|
||||
_state: &S,
|
||||
) -> Result<Option<Self>, Self::Rejection> {
|
||||
if let Some(query) = parts.uri.query() {
|
||||
let value = serde_html_form::from_str(query)
|
||||
.map_err(|err| QueryRejection::FailedToDeserializeQueryString(Error::new(err)))?;
|
||||
Ok(Some(Self(value)))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
axum_core::__impl_deref!(Query);
|
||||
|
||||
/// Rejection used for [`Query`].
|
||||
|
@ -152,9 +174,11 @@ impl std::error::Error for QueryRejection {
|
|||
///
|
||||
/// [example]: https://github.com/tokio-rs/axum/blob/main/examples/query-params-with-empty-strings/src/main.rs
|
||||
#[cfg_attr(docsrs, doc(cfg(feature = "query")))]
|
||||
#[deprecated = "Use Option<Path<_>> instead"]
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct OptionalQuery<T>(pub Option<T>);
|
||||
|
||||
#[allow(deprecated)]
|
||||
#[async_trait]
|
||||
impl<T, S> FromRequestParts<S> for OptionalQuery<T>
|
||||
where
|
||||
|
@ -175,6 +199,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
impl<T> std::ops::Deref for OptionalQuery<T> {
|
||||
type Target = Option<T>;
|
||||
|
||||
|
@ -184,6 +209,7 @@ impl<T> std::ops::Deref for OptionalQuery<T> {
|
|||
}
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
impl<T> std::ops::DerefMut for OptionalQuery<T> {
|
||||
#[inline]
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
|
@ -231,6 +257,7 @@ impl std::error::Error for OptionalQueryRejection {
|
|||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(deprecated)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::test_helpers::*;
|
||||
|
|
Loading…
Add table
Reference in a new issue