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

Json: add from_bytes method, use that in 'impl FromRequest' #2244

Merged
merged 7 commits into from
Nov 18, 2023
Merged
Changes from 2 commits
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
67 changes: 42 additions & 25 deletions axum/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,31 +103,7 @@ where
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
if json_content_type(req.headers()) {
let bytes = Bytes::from_request(req, state).await?;
let deserializer = &mut serde_json::Deserializer::from_slice(&bytes);

let value = match serde_path_to_error::deserialize(deserializer) {
Ok(value) => value,
Err(err) => {
let rejection = match err.inner().classify() {
serde_json::error::Category::Data => JsonDataError::from_err(err).into(),
serde_json::error::Category::Syntax | serde_json::error::Category::Eof => {
JsonSyntaxError::from_err(err).into()
}
serde_json::error::Category::Io => {
if cfg!(debug_assertions) {
// we don't use `serde_json::from_reader` and instead always buffer
// bodies first, so we shouldn't encounter any IO errors
unreachable!()
} else {
JsonSyntaxError::from_err(err).into()
}
}
};
return Err(rejection);
}
};

Ok(Json(value))
Self::from_bytes(&bytes)
} else {
Err(MissingJsonContentType.into())
}
Expand Down Expand Up @@ -167,6 +143,47 @@ impl<T> From<T> for Json<T> {
}
}

impl<T> Json<T>
where
T: DeserializeOwned,
{
/// Construct a `Json<T>` from `&Bytes`. Most users should prefer to use the `FromRequest` impl
waynr marked this conversation as resolved.
Show resolved Hide resolved
/// but special cases may require first extracting a `Request` into `Bytes` then optionally
/// constructing a `Json<T>`.
///
/// An example where this would be useful would be one in which a service needs to pass through
/// or otherwise preserve the exact byte representation of the request while also interrogating
/// it for metadata that may be useful in determining exactly what to do with the preserved
/// bytes.
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure but I kinda feel like we should remove this paragraph. I worry it might be confusing or potentially misleading.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay, fair enough.

pub fn from_bytes(bytes: &Bytes) -> Result<Self, JsonRejection> {
waynr marked this conversation as resolved.
Show resolved Hide resolved
let deserializer = &mut serde_json::Deserializer::from_slice(&bytes);
waynr marked this conversation as resolved.
Show resolved Hide resolved

let value = match serde_path_to_error::deserialize(deserializer) {
Ok(value) => value,
Err(err) => {
let rejection = match err.inner().classify() {
serde_json::error::Category::Data => JsonDataError::from_err(err).into(),
serde_json::error::Category::Syntax | serde_json::error::Category::Eof => {
JsonSyntaxError::from_err(err).into()
}
serde_json::error::Category::Io => {
if cfg!(debug_assertions) {
// we don't use `serde_json::from_reader` and instead always buffer
// bodies first, so we shouldn't encounter any IO errors
unreachable!()
} else {
JsonSyntaxError::from_err(err).into()
}
}
};
return Err(rejection);
}
};

Ok(Json(value))
}
}

impl<T> IntoResponse for Json<T>
where
T: Serialize,
Expand Down
Loading