I am developing an API (based on actix) and across all the routes there are several classes of query parameters that I want to allow for different routes. I am setting up structs for the parameters like:
// just a Test struct
#[derive(Debug, Deserialize, Serialize)]
pub struct Test {
pub limit: Option<usize>,
pub pt: Option<usize>,
}
// Struct for data subsetting
#[derive(Debug, Serialize, Deserialize)]
pub struct DataSubsetting {
pub subset: String,
pub datetime: String,
pub properties: Vec<String>,
pub exclude_properties: Vec<String>,
}
Now I want to use these structs in e.g. my /data
route and therefore I "combine" my structs into a QueryParameter
struct by flattening them with serde
...
#[derive(Debug, Deserialize, Serialize)]
pub struct QueryParams {
#[serde(flatten)]
pub limitandtest: Option<Test>,
#[serde(flatten)]
pub data_subsetting: Option<DataSubsetting>,
... be able to pass it to my data_handler
function like so.
pub async fn data_handler(query: web::Query<QueryParams>) -> impl Responder {
if let Some(subsetting) = &query.data_subsetting {
println!("Subset: {:?}", subsetting);
println!("Subset: {:?}", subsetting.subset);
}
if let Some(test) = &query.limitandtest {
println!("limitandtest: {:?}", test);
}
}
I followed this but was not able to get any values in the println!
statements except the structure of the QueryParams
variable with all values being None
. However, if I just pass Test
to the data_handler
I get my desired output. What is wrong with the way I write my QueryParams
struct?
I also saw this, but it does not seem to satisfy my requirements as the possible query parameters are static and will not change at all.
Any help is much appreciated!