Skip to content

Commit

Permalink
Update Non-strict querystring parsing (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Antony1060 authored Jan 24, 2024
1 parent 5fc4ec5 commit 1052b0b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 8 additions & 1 deletion server/src/routes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use axum::Json;
use enstate_shared::core::error::ProfileError;
use enstate_shared::utils::vec::dedup_ord;
use ethers::prelude::ProviderError;
use lazy_static::lazy_static;
use serde::{Deserialize, Deserializer};
use thiserror::Error;

Expand Down Expand Up @@ -116,6 +117,10 @@ pub fn validate_bulk_input(

pub struct Qs<T>(T);

lazy_static! {
static ref SERDE_QS_CONFIG: serde_qs::Config = serde_qs::Config::new(2, false);
}

#[axum::async_trait]
impl<T, S> FromRequestParts<S> for Qs<T>
where
Expand All @@ -126,7 +131,9 @@ where
async fn from_request_parts(parts: &mut Parts, _: &S) -> Result<Self, Self::Rejection> {
let query = parts.uri.query().unwrap_or("");
Ok(Self(
serde_qs::from_str(query).map_err(|error| error.to_string())?,
SERDE_QS_CONFIG
.deserialize_str(query)
.map_err(|error| error.to_string())?,
))
}
}
7 changes: 6 additions & 1 deletion worker/src/http_util.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use enstate_shared::core::error::ProfileError;
use ethers::prelude::ProviderError;
use http::status::StatusCode;
use lazy_static::lazy_static;
use serde::de::DeserializeOwned;
use serde::{Deserialize, Deserializer, Serialize};
use thiserror::Error;
Expand All @@ -23,11 +24,15 @@ where
Ok(value.map(|it| it == "true").unwrap_or(false))
}

lazy_static! {
static ref SERDE_QS_CONFIG: serde_qs::Config = serde_qs::Config::new(2, false);
}

pub fn parse_query<T: DeserializeOwned>(req: &Request) -> worker::Result<T> {
let url = req.url()?;
let query = url.query().unwrap_or("");

serde_qs::from_str::<T>(query).map_err(|_| http_simple_status_error(StatusCode::BAD_REQUEST))
SERDE_QS_CONFIG.deserialize_str::<T>(query).map_err(|_| http_simple_status_error(StatusCode::BAD_REQUEST))
}

#[derive(Error, Debug)]
Expand Down

0 comments on commit 1052b0b

Please sign in to comment.