Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-strict querystring parsing #66

Merged
merged 1 commit into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading