-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a separate trait for optional extractors #2475
Draft
jplatte
wants to merge
4
commits into
main
Choose a base branch
from
jplatte/optional-from-request
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use async_trait::async_trait; | ||
use http::request::Parts; | ||
|
||
use crate::response::IntoResponse; | ||
|
||
use super::{private, FromRequest, FromRequestParts, Request}; | ||
|
||
/// TODO: DOCS | ||
#[async_trait] | ||
pub trait OptionalFromRequestParts<S>: Sized { | ||
/// If the extractor fails it'll use this "rejection" type. A rejection is | ||
/// a kind of error that can be converted into a response. | ||
type Rejection: IntoResponse; | ||
|
||
/// Perform the extraction. | ||
async fn from_request_parts( | ||
parts: &mut Parts, | ||
state: &S, | ||
) -> Result<Option<Self>, Self::Rejection>; | ||
} | ||
|
||
/// TODO: DOCS | ||
#[async_trait] | ||
pub trait OptionalFromRequest<S, M = private::ViaRequest>: Sized { | ||
/// If the extractor fails it'll use this "rejection" type. A rejection is | ||
/// a kind of error that can be converted into a response. | ||
type Rejection: IntoResponse; | ||
|
||
/// Perform the extraction. | ||
async fn from_request(req: Request, state: &S) -> Result<Option<Self>, Self::Rejection>; | ||
} | ||
|
||
#[async_trait] | ||
impl<S, T> FromRequestParts<S> for Option<T> | ||
where | ||
T: OptionalFromRequestParts<S>, | ||
S: Send + Sync, | ||
{ | ||
type Rejection = T::Rejection; | ||
|
||
async fn from_request_parts( | ||
parts: &mut Parts, | ||
state: &S, | ||
) -> Result<Option<T>, Self::Rejection> { | ||
T::from_request_parts(parts, state).await | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<S, T> FromRequest<S> for Option<T> | ||
where | ||
T: OptionalFromRequest<S>, | ||
S: Send + Sync, | ||
{ | ||
type Rejection = T::Rejection; | ||
|
||
async fn from_request(req: Request, state: &S) -> Result<Option<T>, Self::Rejection> { | ||
T::from_request(req, state).await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think that a similar thing can be done here. |
||
#[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::*; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Query instead of Path