mirror of
https://github.com/tokio-rs/axum.git
synced 2026-08-27 00:00:24 +02:00
Remove OptionalFromRequestParts impl for Query (#3088)
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
use axum::{
|
||||
extract::{FromRequestParts, OptionalFromRequestParts},
|
||||
extract::FromRequestParts,
|
||||
response::{IntoResponse, Response},
|
||||
Error,
|
||||
};
|
||||
@@ -18,19 +18,6 @@ use std::fmt;
|
||||
/// with the `multiple` attribute. Those values can be collected into a `Vec` or other sequential
|
||||
/// container.
|
||||
///
|
||||
/// # `Option<Query<T>>` behavior
|
||||
///
|
||||
/// If `Query<T>` itself is used as an extractor and there is no query string in
|
||||
/// the request URL, `T`'s `Deserialize` implementation is called on an empty
|
||||
/// string instead.
|
||||
///
|
||||
/// You can avoid this by using `Option<Query<T>>`, which gives you `None` in
|
||||
/// the case that there is no query string in the request URL.
|
||||
///
|
||||
/// Note that an empty query string is not the same as no query string, that is
|
||||
/// `https://example.org/` and `https://example.org/?` are not treated the same
|
||||
/// in this case.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust,no_run
|
||||
@@ -111,29 +98,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
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 deserializer =
|
||||
serde_html_form::Deserializer::new(form_urlencoded::parse(query.as_bytes()));
|
||||
let value = serde_path_to_error::deserialize(deserializer)
|
||||
.map_err(|err| QueryRejection::FailedToDeserializeQueryString(Error::new(err)))?;
|
||||
Ok(Some(Self(value)))
|
||||
} else {
|
||||
Ok(None)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
axum_core::__impl_deref!(Query);
|
||||
|
||||
/// Rejection used for [`Query`].
|
||||
@@ -181,8 +145,8 @@ impl std::error::Error for QueryRejection {
|
||||
}
|
||||
|
||||
/// Extractor that deserializes query strings into `None` if no query parameters are present.
|
||||
/// Otherwise behaviour is identical to [`Query`]
|
||||
///
|
||||
/// Otherwise behaviour is identical to [`Query`].
|
||||
/// `T` is expected to implement [`serde::Deserialize`].
|
||||
///
|
||||
/// # Example
|
||||
@@ -220,11 +184,9 @@ 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<Query<_>> instead"]
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct OptionalQuery<T>(pub Option<T>);
|
||||
|
||||
#[allow(deprecated)]
|
||||
impl<T, S> FromRequestParts<S> for OptionalQuery<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
@@ -246,7 +208,6 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(deprecated)]
|
||||
impl<T> std::ops::Deref for OptionalQuery<T> {
|
||||
type Target = Option<T>;
|
||||
|
||||
@@ -256,7 +217,6 @@ 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 {
|
||||
@@ -304,7 +264,6 @@ impl std::error::Error for OptionalQueryRejection {
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[allow(deprecated)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::test_helpers::*;
|
||||
|
||||
Reference in New Issue
Block a user