fix: incorrect json response content type
This commit is contained in:
10
Cargo.toml
10
Cargo.toml
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "axum-valid"
|
name = "axum-valid"
|
||||||
version = "0.4.1"
|
version = "0.4.2"
|
||||||
description = "Validation tools for axum using the validator library."
|
description = "Validation tools for axum using the validator library."
|
||||||
authors = ["GengTeng <me@gteng.org>"]
|
authors = ["GengTeng <me@gteng.org>"]
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
@@ -23,11 +23,6 @@ edition = "2021"
|
|||||||
axum = { version = "0.6.18", default-features = false }
|
axum = { version = "0.6.18", default-features = false }
|
||||||
validator = "0.16.0"
|
validator = "0.16.0"
|
||||||
|
|
||||||
[dependencies.serde_json]
|
|
||||||
package = "serde_json"
|
|
||||||
version = "1.0.103"
|
|
||||||
optional = true
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
anyhow = "1.0.71"
|
anyhow = "1.0.71"
|
||||||
axum = { version = "0.6.18" }
|
axum = { version = "0.6.18" }
|
||||||
@@ -37,11 +32,12 @@ reqwest = { version = "0.11.18", features = ["json"] }
|
|||||||
serde = { version = "1.0.163", features = ["derive"] }
|
serde = { version = "1.0.163", features = ["derive"] }
|
||||||
validator = { version = "0.16.0", features = ["derive"] }
|
validator = { version = "0.16.0", features = ["derive"] }
|
||||||
serde_json = "1.0.103"
|
serde_json = "1.0.103"
|
||||||
|
mime = "0.3.17"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = ["json", "form", "query"]
|
default = ["json", "form", "query"]
|
||||||
json = ["axum/json"]
|
json = ["axum/json"]
|
||||||
form = ["axum/form"]
|
form = ["axum/form"]
|
||||||
query = ["axum/query"]
|
query = ["axum/query"]
|
||||||
into_json = ["serde_json"]
|
into_json = ["json"]
|
||||||
422 = []
|
422 = []
|
||||||
|
|||||||
14
src/lib.rs
14
src/lib.rs
@@ -47,16 +47,14 @@ impl<E: IntoResponse> IntoResponse for ValidRejection<E> {
|
|||||||
match self {
|
match self {
|
||||||
ValidRejection::Valid(validate_error) => {
|
ValidRejection::Valid(validate_error) => {
|
||||||
#[cfg(feature = "into_json")]
|
#[cfg(feature = "into_json")]
|
||||||
match serde_json::to_string(&validate_error) {
|
{
|
||||||
Ok(json) => (VALIDATION_ERROR_STATUS, json),
|
(VALIDATION_ERROR_STATUS, axum::Json(validate_error)).into_response()
|
||||||
Err(error) => (
|
|
||||||
StatusCode::INTERNAL_SERVER_ERROR,
|
|
||||||
format!("Failed to serialize validation error into JSON ({validate_error}): {error}"),
|
|
||||||
),
|
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "into_json"))]
|
#[cfg(not(feature = "into_json"))]
|
||||||
(VALIDATION_ERROR_STATUS, validate_error.to_string())
|
{
|
||||||
}.into_response(),
|
(VALIDATION_ERROR_STATUS, validate_error.to_string()).into_response()
|
||||||
|
}
|
||||||
|
}
|
||||||
ValidRejection::Inner(json_error) => json_error.into_response(),
|
ValidRejection::Inner(json_error) => json_error.into_response(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,6 +15,8 @@ use serde_json::json;
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
|
||||||
|
mod utils;
|
||||||
|
|
||||||
mod route {
|
mod route {
|
||||||
pub const PATH: &str = "/path/:v0/:v1";
|
pub const PATH: &str = "/path/:v0/:v1";
|
||||||
pub const QUERY: &str = "/query";
|
pub const QUERY: &str = "/query";
|
||||||
@@ -78,10 +80,7 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
.await?;
|
.await?;
|
||||||
assert_eq!(invalid_path_response.status(), VALIDATION_ERROR_STATUS);
|
assert_eq!(invalid_path_response.status(), VALIDATION_ERROR_STATUS);
|
||||||
#[cfg(feature = "into_json")]
|
#[cfg(feature = "into_json")]
|
||||||
assert!(invalid_path_response
|
utils::check_json(invalid_path_response).await;
|
||||||
.json::<serde_json::Value>()
|
|
||||||
.await
|
|
||||||
.is_ok());
|
|
||||||
println!("Valid<Path<...>> works.");
|
println!("Valid<Path<...>> works.");
|
||||||
|
|
||||||
// Valid<Query<...>>
|
// Valid<Query<...>>
|
||||||
@@ -107,10 +106,7 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
.await?;
|
.await?;
|
||||||
assert_eq!(invalid_query_response.status(), VALIDATION_ERROR_STATUS);
|
assert_eq!(invalid_query_response.status(), VALIDATION_ERROR_STATUS);
|
||||||
#[cfg(feature = "into_json")]
|
#[cfg(feature = "into_json")]
|
||||||
assert!(invalid_query_response
|
utils::check_json(invalid_query_response).await;
|
||||||
.json::<serde_json::Value>()
|
|
||||||
.await
|
|
||||||
.is_ok());
|
|
||||||
println!("Valid<Query<...>> works.");
|
println!("Valid<Query<...>> works.");
|
||||||
|
|
||||||
// Valid<Form<...>>
|
// Valid<Form<...>>
|
||||||
@@ -139,10 +135,7 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
.await?;
|
.await?;
|
||||||
assert_eq!(invalid_form_response.status(), VALIDATION_ERROR_STATUS);
|
assert_eq!(invalid_form_response.status(), VALIDATION_ERROR_STATUS);
|
||||||
#[cfg(feature = "into_json")]
|
#[cfg(feature = "into_json")]
|
||||||
assert!(invalid_form_response
|
utils::check_json(invalid_form_response).await;
|
||||||
.json::<serde_json::Value>()
|
|
||||||
.await
|
|
||||||
.is_ok());
|
|
||||||
println!("Valid<Form<...>> works.");
|
println!("Valid<Form<...>> works.");
|
||||||
|
|
||||||
// Valid<Json<...>>
|
// Valid<Json<...>>
|
||||||
@@ -171,10 +164,7 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
.await?;
|
.await?;
|
||||||
assert_eq!(invalid_json_response.status(), VALIDATION_ERROR_STATUS);
|
assert_eq!(invalid_json_response.status(), VALIDATION_ERROR_STATUS);
|
||||||
#[cfg(feature = "into_json")]
|
#[cfg(feature = "into_json")]
|
||||||
assert!(invalid_json_response
|
utils::check_json(invalid_json_response).await;
|
||||||
.json::<serde_json::Value>()
|
|
||||||
.await
|
|
||||||
.is_ok());
|
|
||||||
println!("Valid<Json<...>> works.");
|
println!("Valid<Json<...>> works.");
|
||||||
|
|
||||||
drop(server_guard);
|
drop(server_guard);
|
||||||
|
|||||||
@@ -12,6 +12,8 @@ use serde::{Deserialize, Serialize};
|
|||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use validator::Validate;
|
use validator::Validate;
|
||||||
|
|
||||||
|
mod utils;
|
||||||
|
|
||||||
const MY_DATA_HEADER: &str = "My-Data";
|
const MY_DATA_HEADER: &str = "My-Data";
|
||||||
|
|
||||||
// 1. Implement your own extractor.
|
// 1. Implement your own extractor.
|
||||||
@@ -124,10 +126,7 @@ async fn main() -> anyhow::Result<()> {
|
|||||||
.await?;
|
.await?;
|
||||||
assert_eq!(invalid_my_data_response.status(), VALIDATION_ERROR_STATUS);
|
assert_eq!(invalid_my_data_response.status(), VALIDATION_ERROR_STATUS);
|
||||||
#[cfg(feature = "into_json")]
|
#[cfg(feature = "into_json")]
|
||||||
assert!(invalid_my_data_response
|
utils::check_json(invalid_my_data_response).await;
|
||||||
.json::<serde_json::Value>()
|
|
||||||
.await
|
|
||||||
.is_ok());
|
|
||||||
println!("Valid<MyData> works.");
|
println!("Valid<MyData> works.");
|
||||||
|
|
||||||
drop(server_guard);
|
drop(server_guard);
|
||||||
|
|||||||
9
tests/utils.rs
Normal file
9
tests/utils.rs
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
/// Check if the response is a json response
|
||||||
|
#[cfg(feature = "into_json")]
|
||||||
|
pub async fn check_json(response: reqwest::Response) {
|
||||||
|
assert_eq!(
|
||||||
|
response.headers()[axum::http::header::CONTENT_TYPE],
|
||||||
|
axum::http::HeaderValue::from_static(mime::APPLICATION_JSON.as_ref())
|
||||||
|
);
|
||||||
|
assert!(response.json::<serde_json::Value>().await.is_ok());
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user