From 3ec8de35b013ecb07e1c1fb62d7b0049f4f96c1b Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Thu, 19 Dec 2024 21:09:15 +0100 Subject: [PATCH] Remove OptionalFromRequestParts impl for Query --- axum-extra/CHANGELOG.md | 4 ++++ axum-extra/src/extract/query.rs | 36 +-------------------------------- axum/CHANGELOG.md | 4 ++++ axum/src/docs/extract.md | 10 +-------- axum/src/extract/query.rs | 36 +-------------------------------- 5 files changed, 11 insertions(+), 79 deletions(-) diff --git a/axum-extra/CHANGELOG.md b/axum-extra/CHANGELOG.md index c7152a20ab5..367bf6055cb 100644 --- a/axum-extra/CHANGELOG.md +++ b/axum-extra/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning]. # Unreleased +- **breaking:** Remove `OptionalFromRequestParts` impl for `Query` ([#3088]) + +[#3088]: https://github.com/tokio-rs/axum/pull/3088 + # 0.10.0 ## rc.1 diff --git a/axum-extra/src/extract/query.rs b/axum-extra/src/extract/query.rs index 1ee5c1cc8e1..66d85dbec4c 100644 --- a/axum-extra/src/extract/query.rs +++ b/axum-extra/src/extract/query.rs @@ -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>` behavior -/// -/// If `Query` 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>`, 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 @@ -109,27 +96,6 @@ where } } -impl OptionalFromRequestParts for Query -where - T: DeserializeOwned, - S: Send + Sync, -{ - type Rejection = QueryRejection; - - async fn from_request_parts( - parts: &mut Parts, - _state: &S, - ) -> Result, 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`]. diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index e896deb3173..231e61c3e00 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 # Unreleased +- **breaking:** Remove `OptionalFromRequestParts` impl for `Query` ([#3088]) + +[#3088]: https://github.com/tokio-rs/axum/pull/3088 + # 0.8.0 ## rc.1 diff --git a/axum/src/docs/extract.md b/axum/src/docs/extract.md index 20dfe46e9f8..ccc67ce0900 100644 --- a/axum/src/docs/extract.md +++ b/axum/src/docs/extract.md @@ -108,18 +108,10 @@ struct Pagination { per_page: usize, } -impl Default for Pagination { - fn default() -> Self { - Self { page: 1, per_page: 30 } - } -} - async fn get_user_things( Path(user_id): Path, - pagination: Option>, + Query(pagination): Query, ) { - let Query(pagination) = pagination.unwrap_or_default(); - // ... } # let _: Router = app; diff --git a/axum/src/extract/query.rs b/axum/src/extract/query.rs index 14473aab043..cc6041df3cb 100644 --- a/axum/src/extract/query.rs +++ b/axum/src/extract/query.rs @@ -1,4 +1,4 @@ -use super::{rejection::*, FromRequestParts, OptionalFromRequestParts}; +use super::{rejection::*, FromRequestParts}; use http::{request::Parts, Uri}; use serde::de::DeserializeOwned; @@ -6,19 +6,6 @@ use serde::de::DeserializeOwned; /// /// `T` is expected to implement [`serde::Deserialize`]. /// -/// # `Option>` behavior -/// -/// If `Query` 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>`, 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. -/// /// # Examples /// /// ```rust,no_run @@ -75,27 +62,6 @@ where } } -impl OptionalFromRequestParts for Query -where - T: DeserializeOwned, - S: Send + Sync, -{ - type Rejection = QueryRejection; - - async fn from_request_parts( - parts: &mut Parts, - _state: &S, - ) -> Result, Self::Rejection> { - if let Some(query) = parts.uri.query() { - let value = serde_urlencoded::from_str(query) - .map_err(FailedToDeserializeQueryString::from_err)?; - Ok(Some(Self(value))) - } else { - Ok(None) - } - } -} - impl Query where T: DeserializeOwned,