mirror of
https://github.com/sigp/lighthouse.git
synced 2026-04-19 22:08:30 +00:00
Assume Content-Type is json for endpoints that require json (#4575)
* added default content type filter * Merge branch 'unstable' of https://github.com/sigp/lighthouse into unstable * create custom warp json filter that ignores content type header * cargo fmt and linting * updated test * updated test * merge unstable * merge conflicts * workspace=true * use Bytes instead of Buf * resolve merge conflict * resolve merge conflicts * add extra error message context * merge conflicts * lint
This commit is contained in:
22
common/warp_utils/src/json.rs
Normal file
22
common/warp_utils/src/json.rs
Normal file
@@ -0,0 +1,22 @@
|
||||
use bytes::Bytes;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::error::Error as StdError;
|
||||
use warp::{Filter, Rejection};
|
||||
|
||||
use crate::reject;
|
||||
|
||||
struct Json;
|
||||
|
||||
type BoxError = Box<dyn StdError + Send + Sync>;
|
||||
|
||||
impl Json {
|
||||
fn decode<T: DeserializeOwned>(bytes: Bytes) -> Result<T, BoxError> {
|
||||
serde_json::from_slice(&bytes).map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn json<T: DeserializeOwned + Send>() -> impl Filter<Extract = (T,), Error = Rejection> + Copy {
|
||||
warp::body::bytes().and_then(|bytes: Bytes| async move {
|
||||
Json::decode(bytes).map_err(|err| reject::custom_deserialize_error(format!("{:?}", err)))
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user