diff --git a/axum/CHANGELOG.md b/axum/CHANGELOG.md index ceb6f404..e0379c2d 100644 --- a/axum/CHANGELOG.md +++ b/axum/CHANGELOG.md @@ -12,8 +12,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 `serde_json::Error` ([#1371]) - **added**: `JsonRejection` now displays the path at which a deserialization error occurred too ([#1371]) +- **fixed:** Used `400 Bad Request` for `FailedToDeserializeQueryString` + rejections, instead of `422 Unprocessable Entity` ([#1387]) [#1371]: https://github.com/tokio-rs/axum/pull/1371 +[#1387]: https://github.com/tokio-rs/axum/pull/1387 # 0.6.0-rc.2 (10. September, 2022) diff --git a/axum/src/extract/query.rs b/axum/src/extract/query.rs index b4c34f94..a520853a 100644 --- a/axum/src/extract/query.rs +++ b/axum/src/extract/query.rs @@ -75,9 +75,11 @@ impl Deref for Query { #[cfg(test)] mod tests { + use crate::{routing::get, test_helpers::TestClient, Router}; + use super::*; use axum_core::extract::FromRequest; - use http::Request; + use http::{Request, StatusCode}; use serde::Deserialize; use std::fmt::Debug; @@ -124,4 +126,21 @@ mod tests { ) .await; } + + #[tokio::test] + async fn correct_rejection_status_code() { + #[derive(Deserialize)] + #[allow(dead_code)] + struct Params { + n: i32, + } + + async fn handler(_: Query) {} + + let app = Router::new().route("/", get(handler)); + let client = TestClient::new(app); + + let res = client.get("/?n=hi").send().await; + assert_eq!(res.status(), StatusCode::BAD_REQUEST); + } } diff --git a/axum/src/extract/rejection.rs b/axum/src/extract/rejection.rs index 135ad0a2..d6f11c31 100644 --- a/axum/src/extract/rejection.rs +++ b/axum/src/extract/rejection.rs @@ -116,7 +116,7 @@ impl FailedToDeserializeQueryString { impl IntoResponse for FailedToDeserializeQueryString { fn into_response(self) -> Response { - (http::StatusCode::UNPROCESSABLE_ENTITY, self.to_string()).into_response() + (http::StatusCode::BAD_REQUEST, self.to_string()).into_response() } }