From df55d83f2984306994725b46a461aaa1e354e29e Mon Sep 17 00:00:00 2001 From: daalfox <123469030+daalfox@users.noreply.github.com> Date: Tue, 6 May 2025 15:21:10 +0330 Subject: [PATCH] Further refactor `json_content_type` (#3340) --- axum/src/json.rs | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/axum/src/json.rs b/axum/src/json.rs index c62c94c6..1e662cc6 100644 --- a/axum/src/json.rs +++ b/axum/src/json.rs @@ -136,22 +136,14 @@ where } fn json_content_type(headers: &HeaderMap) -> bool { - let Some(content_type) = headers.get(header::CONTENT_TYPE) else { - return false; - }; - - let Ok(content_type) = content_type.to_str() else { - return false; - }; - - let Ok(mime) = content_type.parse::() else { - return false; - }; - - let is_json_content_type = mime.type_() == "application" - && (mime.subtype() == "json" || mime.suffix().is_some_and(|name| name == "json")); - - is_json_content_type + headers + .get(header::CONTENT_TYPE) + .and_then(|content_type| content_type.to_str().ok()) + .and_then(|content_type| content_type.parse::().ok()) + .is_some_and(|mime| { + mime.type_() == "application" + && (mime.subtype() == "json" || mime.suffix().is_some_and(|name| name == "json")) + }) } axum_core::__impl_deref!(Json);