-
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
Implement OptionalFromRequest
for the Json extractor
#3142
Changes from 1 commit
902fdce
f7a1a6f
29d0dea
f9b42c8
abafff2
47350df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use crate::extract::Request; | ||
use crate::extract::{rejection::*, FromRequest}; | ||
use axum_core::extract::OptionalFromRequest; | ||
use axum_core::response::{IntoResponse, Response}; | ||
use bytes::{BufMut, Bytes, BytesMut}; | ||
use http::{ | ||
|
@@ -112,6 +113,27 @@ where | |
} | ||
} | ||
|
||
impl<T, S> OptionalFromRequest<S> for Json<T> | ||
where | ||
T: DeserializeOwned, | ||
S: Send + Sync, | ||
{ | ||
type Rejection = JsonRejection; | ||
|
||
async fn from_request(req: Request, state: &S) -> Result<Option<Self>, Self::Rejection> { | ||
if json_content_type(req.headers()) { | ||
let bytes = Bytes::from_request(req, state).await?; | ||
if !bytes.is_empty() { | ||
Ok(Some(Self::from_bytes(&bytes)?)) | ||
} else { | ||
Ok(None) | ||
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. if the header says that the content-type is JSON, but the body is empty, shouldn't that be an error? 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. yeah that makes sense, I added a new error, is that the right way to approach this? 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. what rejection is the non-optional extractor using in this case? I assume 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 looked into that one, I am not quite sure what 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 guess we should always call 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. do i need to create a deserializer and then use 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.
do you mean something like this right in the block where we check if the body is empty:? match Self::from_bytes(&bytes) {
Ok(_) => unreachable!("JSON body can only be empty here"),
Err(err) => Err(err),
} |
||
} | ||
} else { | ||
Ok(None) | ||
} | ||
} | ||
} | ||
|
||
fn json_content_type(headers: &HeaderMap) -> bool { | ||
let content_type = if let Some(content_type) = headers.get(header::CONTENT_TYPE) { | ||
content_type | ||
|
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.
this causes a mismatching JSON body to return an error instead of
None
, is that intentional?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.
is this in reference to the previous CR comment? by mismatch do you mean an empty body here?