-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Support for Vec in Query? #434
Comments
|
I'm gonna do some digging and compare Some notes:
|
From looking around I think sticking with struct Qs<T>(T);
#[axum::async_trait]
impl<T> FromRequest for Qs<T>
where
T: serde::de::DeserializeOwned,
{
type Rejection = Infallible;
async fn from_request(req: &mut RequestParts) -> Result<Self, Self::Rejection> {
// TODO: error handling
let query = req.uri().query().unwrap();
Ok(Self(serde_qs::from_str(query).unwrap()))
}
} thats probably gonna be fine. I'll close this issue for now, but feel free to reopen if there are more thoughts/comments. |
FWIW I'm maintaining a patched version of
fn deserialize_array<'de, D, T>(deserializer: D) -> Result<Vec<T>, D::Error>
where
D: Deserializer<'de>,
T: Deserialize<'de>,
{
let s = String::deserialize(deserializer)?;
let v = todo!("parse the bracketed comma-separated items")
Ok(v)
} and use it with |
For the curious, the patch of Other relevant issues in |
Yes, and we have |
This does not work for strings though. At least I haven't found a way to make it work. The problem is that at that point, the string The workaround with the custom Extractor that uses serde_qs is not perfect either because it's not possible to apply this to only one field but only the whole struct afaik. What's left is:
If you guys see a better way to make this work please help me out! |
@flo-at can you describe your use case in a bit more detail? Have you seen the |
@jplatte After some fiddling I found a working solution that is alright. It's a combination of the #[derive(serde::Deserialize)]
struct SearchArgs {
#[serde(default, deserialize_with = "filter_vec_deserialize")]
filters : Option<Vec<Filter>>,
}
This way I can use my custom deserializer on the inner type Edit: Just in case someone else stumbles across this: If you use the option I posted above, you'll realize that only vectors will work. If there is just a single parameter in the URL, it will fail to deserialize that into a #[derive(Deserialize)]
#[serde(untagged)]
enum OneOrMany<T> {
One(T),
Many(Vec<T>),
} Edit2: With the current implementation, it works a lot better if you leave out the |
Using Form in the axum-extra crate solved it for me really quickly. |
https://docs.rs/axum-extra/0.9.3/axum_extra/extract/struct.Query.html |
Discussed in #433
Originally posted by Vesafary October 29, 2021
Hi!
I've been struggling with this for a while, and didn't get it to work.
Looking through the docs and examples also didn't give me the answer.
What I'm looking for is support for one of the following:
I've tried several options like
Of course I can implement it myself using the RawQuery extractor, but it seems like fairly basic usage so I was wondering whether I did something wrong.
Cheers!
The text was updated successfully, but these errors were encountered: