Skip to content
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

Merged
merged 6 commits into from
Jan 3, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions axum/src/json.rs
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::{
Expand Down Expand Up @@ -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)?))
Copy link
Collaborator

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?

Copy link
Contributor Author

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?

} else {
Ok(None)
Copy link
Collaborator

Choose a reason for hiding this comment

The 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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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?

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 JsonSyntaxError? would it be possible to reuse that rejection?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into that one, I am not quite sure what Error is supposed to be here in JsonSyntaxError(Error) ? Is it really just specific to serde?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should always call Self::from_bytes() even if bytes.is_empty(), because if the header was set then we expect valid JSON and an empty body is not valid JSON 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do i need to create a deserializer and then use serde_path_to_error::deserialize to use that error variant?

Copy link
Contributor Author

@infiniteregrets infiniteregrets Jan 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should always call Self::from_bytes() even if bytes.is_empty(), because if the header was set then we expect valid JSON and an empty body is not valid JSON 🤔

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
Expand Down