Skip to content

Commit

Permalink
Bump serde_qs to 0.13
Browse files Browse the repository at this point in the history
  • Loading branch information
rlebran committed May 2, 2024
1 parent 74c8a4a commit 30f22fb
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "Actix-web garde wrapper"
readme = "README.md"
keywords = ["garde", "actix", "actix-web", "validation"]
categories = ["web-programming"]
version = "0.6.0"
version = "0.7.0"

authors = ["Netwo <oss@netwo.com>"]
edition = "2021"
Expand All @@ -29,7 +29,7 @@ thiserror = "1.0"

actix-web-lab = { version = "0.20", optional = true }

serde_qs = { version = "0.12", optional = true }
serde_qs = { version = "0.13", optional = true }

[features]
lab_query = ["dep:actix-web-lab"]
Expand Down
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Actix-web wrapper for [garde](https://github.com/jprochazk/garde), a Rust valida
```toml
[dependencies]
garde = "0.18"
garde-actix-web = "0.5.1"
garde-actix-web = "0.7"
```

### Usage example
Expand Down Expand Up @@ -69,13 +69,14 @@ Context needs to be provided through actix's `data` or `app_data`, if not found

### Compatibility matrix

| garde version | garde-actix-web-version |
|---------------|-------------------------|
| `0.14` | `0.1.x` |
| `0.15` | `0.2.x` |
| `0.16` | `0.3.x` |
| `0.17` | `0.4.x` |
| `0.18` | `0.5.x`, `0.6.x` |
| garde version | serde_qs version | garde-actix-web-version |
|---------------|------------------|-------------------------|
| `0.14` | `0.12` | `0.1.x` |
| `0.15` | `0.12` | `0.2.x` |
| `0.16` | `0.12` | `0.3.x` |
| `0.17` | `0.12` | `0.4.x` |
| `0.18` | `0.12` | `0.5.x`, `0.6.x` |
| `0.18` | `0.13` | `0.7.x` |

### About us

Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
//! ```toml
//! [dependencies]
//! garde = "0.18"
//! garde-actix-web = "0.5.1"
//! garde-actix-web = "0.7"
//! ```
//!
//! # Usage example
Expand Down Expand Up @@ -45,13 +45,13 @@
//!
//! # Compatibility matrix
//!
//! | garde version | garde-actix-web-version |
//! |---------------|-------------------------|
//! | `0.14` | `0.1.x` |
//! | `0.15` | `0.2.x` |
//! | `0.16` | `0.3.x` |
//! | `0.17` | `0.4.x` |
//! | `0.18` | `0.5.x`, `0.6.x` |
//! | garde version | serde_qs version | garde-actix-web-version |
//! |---------------|------------------|-------------------------|
//! | `0.14` | `0.12` | `0.1.x` |
//! | `0.15` | `0.12` | `0.2.x` |
//! | `0.16` | `0.12` | `0.3.x` |
//! | `0.17` | `0.12` | `0.4.x` |
//! | `0.18` | `0.12` | `0.5.x`, `0.6.x` |

#![forbid(unsafe_code)]

Expand Down
4 changes: 2 additions & 2 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ mod json;
mod lab_query;
mod path;
#[cfg(feature = "serde_qs")]
mod qs_query;
mod qs;
mod query;

pub use either::Either;
Expand All @@ -18,5 +18,5 @@ pub use json::{Json, JsonConfig};
pub use lab_query::Query as LabQuery;
pub use path::{Path, PathConfig};
#[cfg(feature = "serde_qs")]
pub use qs_query::{QsQuery, QsQueryConfig};
pub use qs::{QsQuery, QsQueryConfig, QsForm};
pub use query::{Query, QueryConfig};
189 changes: 181 additions & 8 deletions src/web/qs_query.rs → src/web/qs.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use crate::validate_for_request;
use std::sync::Arc;

use actix_web::dev::Payload;
use actix_web::error::QueryPayloadError;
use actix_web::{Error, FromRequest, HttpRequest};
use actix_web::{web, Error, FromRequest, HttpRequest};
use derive_more::{AsRef, Deref, DerefMut, Display, From};
use futures::future::{err, ok, Ready};
use futures::future::{err, ok, LocalBoxFuture, Ready};
use futures::{FutureExt, StreamExt};
use garde::Validate;
use serde::de::DeserializeOwned;
use serde_qs::Config;
use std::sync::Arc;

use crate::validate_for_request;

/// Drop in replacement for [serde_qs::actix::QsQuery](https://docs.rs/serde_qs/latest/serde_qs/actix/struct.QsQuery.html)
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Deref, DerefMut, AsRef, Display, From)]
Expand Down Expand Up @@ -71,12 +74,70 @@ where
}
}

/// Drop in replacement for [serde_qs::actix::QsForm](https://docs.rs/serde_qs/latest/serde_qs/actix/struct.QsForm.html)
#[derive(PartialEq, Eq, PartialOrd, Ord, Debug, Deref, DerefMut, AsRef, Display, From)]
pub struct QsForm<T>(T);

impl<T> QsForm<T> {
pub fn into_inner(self) -> T {
self.0
}
}

impl<T> FromRequest for QsForm<T>
where
T: DeserializeOwned + Validate + 'static,
T::Context: Default,
{
type Error = Error;
type Future = LocalBoxFuture<'static, Result<Self, Self::Error>>;

fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let mut stream = payload.take();
let req_copy = req.clone();
let req_copy2 = req.clone();
let query_config: QsQueryConfig = req
.app_data::<QsQueryConfig>()
.unwrap_or(&QsQueryConfig::default())
.clone();

async move {
let mut bytes = web::BytesMut::new();

while let Some(item) = stream.next().await {
bytes.extend_from_slice(&item.unwrap());
}

query_config
.qs_config
.deserialize_bytes::<T>(&bytes)
.map_err(Into::into)
.and_then(|data: T| {
let req = req_copy;
validate_for_request(data, &req)
})
.map(|val| Ok(QsForm(val)))
.unwrap_or_else(|e| {
let e = if let Some(error_handler) = &query_config.err_handler {
(error_handler)(e, &req_copy2)
} else {
e.into()
};

Err(e)
})
}
.boxed_local()
}
}

type ActixErrorHandler = Option<Arc<dyn Fn(crate::error::Error, &HttpRequest) -> Error + Send + Sync>>;

/// Replacement for [serde_qs::actix::QsQueryConfig](https://docs.rs/serde_qs/latest/serde_qs/actix/struct.QsQueryConfig.html)
/// Error handler must map from an `garde_actix_web::error::Error`
#[derive(Default)]
#[derive(Clone, Default)]
pub struct QsQueryConfig {
#[allow(clippy::type_complexity)]
err_handler: Option<Arc<dyn Fn(crate::error::Error, &HttpRequest) -> Error + Send + Sync>>,
err_handler: ActixErrorHandler,
qs_config: Config,
}

Expand All @@ -97,7 +158,6 @@ impl QsQueryConfig {

#[cfg(test)]
mod test {
use crate::web::{QsQuery, QsQueryConfig};
use actix_http::StatusCode;
use actix_web::error::InternalError;
use actix_web::test::{call_service, init_service, TestRequest};
Expand All @@ -106,6 +166,8 @@ mod test {
use garde::Validate;
use serde::{Deserialize, Serialize};

use crate::web::{Form, FormConfig, QsQuery, QsQueryConfig};

#[derive(Debug, PartialEq, Validate, Serialize, Deserialize)]
struct QueryData {
#[garde(range(min = 18, max = 28))]
Expand All @@ -119,6 +181,19 @@ mod test {
age: u8,
}

#[derive(Debug, PartialEq, Validate, Serialize, Deserialize)]
struct FormData {
#[garde(range(min = 18, max = 28))]
age: u8,
}

#[derive(Debug, PartialEq, Validate, Serialize, Deserialize)]
#[garde(context(NumberContext))]
struct FormDataWithContext {
#[garde(custom(is_big_enough))]
age: u8,
}

#[derive(Default, Debug)]
struct NumberContext {
min: u8,
Expand All @@ -135,10 +210,18 @@ mod test {
HttpResponse::Ok().finish()
}

async fn test_form_handler(_query: Form<FormData>) -> HttpResponse {
HttpResponse::Ok().finish()
}

async fn test_handler_with_context(_query: QsQuery<QueryDataWithContext>) -> HttpResponse {
HttpResponse::Ok().finish()
}

async fn test_form_handler_with_context(_query: Form<FormDataWithContext>) -> HttpResponse {
HttpResponse::Ok().finish()
}

#[tokio::test]
async fn test_simple_query_validation() {
let app = init_service(App::new().service(resource("/").route(post().to(test_handler)))).await;
Expand Down Expand Up @@ -204,4 +287,94 @@ mod test {
let resp = call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::OK);
}

#[tokio::test]
async fn test_simple_form_validation() {
let app = init_service(App::new().service(resource("/").route(post().to(test_form_handler)))).await;

let req = TestRequest::post()
.uri("/")
.set_form(&FormData { age: 24 })
.to_request();
let resp = call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::OK);

let req = TestRequest::post()
.uri("/")
.set_form(&FormData { age: 30 })
.to_request();
let resp = call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}

#[tokio::test]
async fn test_form_validation_custom_config() {
let app = init_service(
App::new()
.app_data(
FormConfig::default()
.error_handler(|err, _req| InternalError::from_response(err, HttpResponse::Conflict().finish()).into()),
)
.service(resource("/").route(post().to(test_form_handler))),
)
.await;

let req = TestRequest::post()
.uri("/")
.set_form(&FormData { age: 24 })
.to_request();
let resp = call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::OK);

let req = TestRequest::post()
.uri("/")
.set_form(&FormData { age: 30 })
.to_request();
let resp = call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::CONFLICT);
}

#[tokio::test]
async fn test_form_validation_with_context() {
let number_context = NumberContext { min: 25 };
let app = init_service(
App::new()
.app_data(number_context)
.service(resource("/").route(post().to(test_form_handler_with_context))),
)
.await;

let req = TestRequest::post()
.uri("/")
.set_form(&FormData { age: 24 })
.to_request();
let resp = call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);

let req = TestRequest::post()
.uri("/")
.set_form(&FormData { age: 30 })
.to_request();
let resp = call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::OK);
}

#[tokio::test]
async fn test_form_validation_with_missing_context() {
let app = init_service(App::new().service(resource("/").route(post().to(test_form_handler_with_context)))).await;

let req = TestRequest::post()
.uri("/")
.set_form(&FormData { age: 24 })
.to_request();
let resp = call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::OK);

let req = TestRequest::post()
.uri("/")
.set_form(&FormData { age: 30 })
.to_request();
let resp = call_service(&app, req).await;
assert_eq!(resp.status(), StatusCode::OK);
}
}

0 comments on commit 30f22fb

Please sign in to comment.