Skip to content

Commit

Permalink
Remove redundant config, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samscott89 committed Apr 7, 2024
1 parent 1914ad7 commit a7de616
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 117 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yml
Expand Up @@ -43,7 +43,6 @@ jobs:
- ""
- actix4
- actix3
- actix2
- warp
- axum
steps:
Expand Down
136 changes: 22 additions & 114 deletions src/actix.rs
Expand Up @@ -13,11 +13,11 @@ use actix_web4 as actix_web;
use actix_web::dev::Payload;
#[cfg(feature = "actix3")]
use actix_web::HttpResponse;
use actix_web::{Error as ActixError, FromRequest, HttpRequest, ResponseError, web};
use futures::future::{ready, Ready, LocalBoxFuture, FutureExt};
use actix_web::{web, Error as ActixError, FromRequest, HttpRequest, ResponseError};
use futures::future::{ready, FutureExt, LocalBoxFuture, Ready};
use futures::StreamExt;
use serde::de::DeserializeOwned;
use serde::de;
use serde::de::DeserializeOwned;
use std::fmt;
use std::fmt::{Debug, Display};
use std::ops::{Deref, DerefMut};
Expand Down Expand Up @@ -115,20 +115,14 @@ where

#[inline]
fn from_request(req: &HttpRequest, _: &mut Payload) -> Self::Future {
let query_config = req.app_data::<QsQueryConfig>();

let error_handler = query_config.map(|c| c.ehandler.clone()).unwrap_or(None);
let query_config = req.app_data::<QsQueryConfig>().unwrap_or(&DEFAULT_CONFIG);

let default_qsconfig = QsConfig::default();
let qsconfig = query_config
.map(|c| &c.qs_config)
.unwrap_or(&default_qsconfig);

let res = qsconfig
let res = query_config
.qs_config
.deserialize_str::<T>(req.query_string())
.map(|val| Ok(QsQuery(val)))
.unwrap_or_else(move |e| {
let e = if let Some(error_handler) = error_handler {
let e = if let Some(error_handler) = &query_config.ehandler {
(error_handler)(e, req)
} else {
e.into()
Expand Down Expand Up @@ -179,11 +173,17 @@ where
/// );
/// }
/// ```
#[derive(Clone)]
pub struct QsQueryConfig {
ehandler: Option<Arc<dyn Fn(QsError, &HttpRequest) -> ActixError + Send + Sync>>,
qs_config: QsConfig,
}

static DEFAULT_CONFIG: QsQueryConfig = QsQueryConfig {
ehandler: None,
qs_config: crate::de::DEFAULT_CONFIG,
};

impl QsQueryConfig {
/// Set custom error handler
pub fn error_handler<F>(mut self, f: F) -> Self
Expand All @@ -210,8 +210,6 @@ impl Default for QsQueryConfig {
}
}

// QS Form Extractor

#[derive(PartialEq, Eq, PartialOrd, Ord)]
/// Extract typed information from from the request's form data.
///
Expand All @@ -223,8 +221,6 @@ impl Default for QsQueryConfig {
/// # use actix_web4 as actix_web;
/// # #[cfg(feature = "actix3")]
/// # use actix_web3 as actix_web;
/// # #[cfg(feature = "actix2")]
/// # use actix_web2 as actix_web;
/// use actix_web::{web, App, HttpResponse};
/// use serde_qs::actix::QsForm;
///
Expand All @@ -248,13 +244,9 @@ impl Default for QsQueryConfig {
/// .route(web::get().to(filter_users)));
/// }
/// ```


#[derive(Debug)]
pub struct QsForm<T>(T);


// let foo: T = QsQuery<T>.into_inner()
impl<T> QsForm<T> {
/// Unwrap into inner T value
pub fn into_inner(self) -> T {
Expand All @@ -276,120 +268,36 @@ impl<T> DerefMut for QsForm<T> {
}
}

// private fields on QsQueryConfig prevent its reuse here, so a new struct
// is defined

/// Form extractor configuration
///
/// ```rust
/// # #[macro_use] extern crate serde_derive;
/// # #[cfg(feature = "actix4")]
/// # use actix_web4 as actix_web;
/// # #[cfg(feature = "actix3")]
/// # use actix_web3 as actix_web;
/// # #[cfg(feature = "actix2")]
/// # use actix_web2 as actix_web;
/// use actix_web::{error, web, App, FromRequest, HttpResponse};
/// use serde_qs::actix::QsQuery;
/// use serde_qs::Config as QsConfig;
/// use serde_qs::actix::QsFormConfig;
///
/// #[derive(Deserialize)]
/// struct Info {
/// username: String,
/// }
///
/// /// deserialize `Info` from request's payload
/// async fn index(info: QsForm<Info>) -> HttpResponse {
/// use serde_qs::actix::QsForm;
/// HttpResponse::Ok().body(
/// format!("Welcome {}!", info.username)
/// )
/// }
///
/// fn main() {
/// let qs_config = QsFormConfig::default()
/// .error_handler(|err, req| { // <- create custom error response
/// error::InternalError::from_response(
/// err, HttpResponse::Conflict().finish()).into()
/// })
/// .qs_config(QsConfig::default());
///
/// let app = App::new().service(
/// web::resource("/index.html").app_data(qs_config)
/// .route(web::post().to(index))
/// );
/// }
/// ```

pub struct QsFormConfig {
ehandler: Option<Arc<dyn Fn(QsError, &HttpRequest) -> ActixError + Send + Sync>>,
qs_config: QsConfig,
}

impl QsFormConfig {
/// Set custom error handler
pub fn error_handler<F>(mut self, f: F) -> Self
where
F: Fn(QsError, &HttpRequest) -> ActixError + Send + Sync + 'static,
{
self.ehandler = Some(Arc::new(f));
self
}

/// Set custom serialization parameters
pub fn qs_config(mut self, config: QsConfig) -> Self {
self.qs_config = config;
self
}
}

impl Default for QsFormConfig {
fn default() -> Self {
QsFormConfig {
qs_config: QsConfig::default(),
ehandler: None,
}
}
}

//
// See:
// - https://github.com/actix/actix-web/blob/master/src/types/form.rs
// - https://github.com/samscott89/serde_qs/blob/main/src/actix.rs
impl<T> FromRequest for QsForm<T>
where
T: DeserializeOwned + Debug,
{
type Error = ActixError;
// type Config = ();
type Future = LocalBoxFuture<'static, Result<Self, ActixError>>;
#[cfg(feature = "actix3")]
type Config = QsQueryConfig;

fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
let mut stream = payload.take();
let req_clone = req.clone();

let query_config: QsQueryConfig = req
.app_data::<QsQueryConfig>()
.unwrap_or(&DEFAULT_CONFIG)
.clone();
async move {
let mut bytes = web::BytesMut::new();

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

let query_config = req_clone.app_data::<QsFormConfig>().clone();
let error_handler = query_config.map(|c| c.ehandler.clone()).unwrap_or(None);

let default_qsconfig = QsConfig::default();
let qsconfig = query_config
.map(|c| &c.qs_config)
.unwrap_or(&default_qsconfig);

qsconfig
query_config
.qs_config
.deserialize_bytes::<T>(&bytes)
.map(|val| Ok(QsForm(val)))
.unwrap_or_else(|e| {
let e = if let Some(error_handler) = error_handler {
// e.into()
let e = if let Some(error_handler) = &query_config.ehandler {
(error_handler)(e, &req_clone)
} else {
e.into()
Expand Down
8 changes: 7 additions & 1 deletion src/de/mod.rs
Expand Up @@ -80,6 +80,7 @@ use std::collections::btree_map::{BTreeMap, Entry, IntoIter};
/// assert_eq!(map.get("a").unwrap().get("b").unwrap().get("c").unwrap(), "1");
/// ```
///
#[derive(Clone, Copy)]
pub struct Config {
/// Specifies the maximum depth key that `serde_qs` will attempt to
/// deserialize. Default is 5.
Expand All @@ -88,9 +89,14 @@ pub struct Config {
strict: bool,
}

pub const DEFAULT_CONFIG: Config = Config {
max_depth: 5,
strict: true,
};

impl Default for Config {
fn default() -> Self {
Self::new(5, true)
DEFAULT_CONFIG
}
}

Expand Down
24 changes: 23 additions & 1 deletion tests/test_actix.rs
Expand Up @@ -16,7 +16,7 @@ use actix_web::error::InternalError;
use actix_web::http::StatusCode;
use actix_web::test::TestRequest;
use actix_web::{FromRequest, HttpResponse};
use qs::actix::{QsQuery, QsQueryConfig};
use qs::actix::{QsForm, QsQuery, QsQueryConfig};
use qs::Config as QsConfig;
use serde::de::Error;

Expand Down Expand Up @@ -143,3 +143,25 @@ fn test_custom_qs_config() {
assert_eq!(s.common.remaining, true);
})
}

#[test]
fn test_form_extractor() {
futures::executor::block_on(async {
let test_data = Query {
foo: 1,
bars: vec![0, 1],
common: CommonParams {
limit: 100,
offset: 50,
remaining: true,
},
};
let req = TestRequest::with_uri("/test")
.set_payload(serde_qs::to_string(&test_data).unwrap())
.to_srv_request();
let (req, mut pl) = req.into_parts();

let s = QsForm::<Query>::from_request(&req, &mut pl).await.unwrap();
assert_eq!(s.into_inner(), test_data);
})
}

0 comments on commit a7de616

Please sign in to comment.