refactor(axum): Refactor json module (#3332)

This commit is contained in:
daalfox
2025-04-29 12:12:04 -04:00
committed by GitHub
parent 7b04b174b9
commit 53631b2873
+8 -14
View File
@@ -104,12 +104,12 @@ where
type Rejection = JsonRejection; type Rejection = JsonRejection;
async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> { async fn from_request(req: Request, state: &S) -> Result<Self, Self::Rejection> {
if json_content_type(req.headers()) { if !json_content_type(req.headers()) {
let bytes = Bytes::from_request(req, state).await?; return Err(MissingJsonContentType.into());
Self::from_bytes(&bytes)
} else {
Err(MissingJsonContentType.into())
} }
let bytes = Bytes::from_request(req, state).await?;
Self::from_bytes(&bytes)
} }
} }
@@ -136,21 +136,15 @@ where
} }
fn json_content_type(headers: &HeaderMap) -> bool { fn json_content_type(headers: &HeaderMap) -> bool {
let content_type = if let Some(content_type) = headers.get(header::CONTENT_TYPE) { let Some(content_type) = headers.get(header::CONTENT_TYPE) else {
content_type
} else {
return false; return false;
}; };
let content_type = if let Ok(content_type) = content_type.to_str() { let Ok(content_type) = content_type.to_str() else {
content_type
} else {
return false; return false;
}; };
let mime = if let Ok(mime) = content_type.parse::<mime::Mime>() { let Ok(mime) = content_type.parse::<mime::Mime>() else {
mime
} else {
return false; return false;
}; };