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

How to pass Vec of struct in the URL? #27

Open
TheCGDF opened this issue Mar 17, 2020 · 3 comments
Open

How to pass Vec of struct in the URL? #27

TheCGDF opened this issue Mar 17, 2020 · 3 comments

Comments

@TheCGDF
Copy link

TheCGDF commented Mar 17, 2020

#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct Address {
    city: String,
    postcode: String,
}
#[derive(Debug, PartialEq, Deserialize, Serialize)]
struct QueryParams {
    id: u8,
    name: String,
    address: Vec<Address>,
}

i have tried address[], but i don't know how to pass a struct in the URL. i always get invalid type: ... expected struct Address

@TheCGDF
Copy link
Author

TheCGDF commented Mar 17, 2020

i found address[0][city]=la&address[0][postcode]=123 works. but is there a way to omit the index([0])?

@gakonst
Copy link

gakonst commented Jun 23, 2020

Also having the same issue. Serializing a key: Vec<T> produces key[0]=a,key[1]=b,key[2]=c (for key = vec![a,b,c], but the expected result here would be: key=a,b,c.

I would be happy to send a PR which tries to fix this @samscott89 - could you give me some pointers?

@samscott89
Copy link
Owner

samscott89 commented Jun 23, 2020

i found address[0][city]=la&address[0][postcode]=123 works. but is there a way to omit the index([0])?

Unfortunately not. Normally for vectors you can omit the number and it will automatically infer the index. But in this case, the ordering is super important, and you need to remember what you have seen across indices so that you parse address[][city]=la&address[][postcode]=123&address[][city]=ny&address[][postcode]=345 correctly (there is no assumption that elements are in order). I just added a should_panic test that exhibits this: https://github.com/samscott89/serde_qs/blob/main/tests/test_deserialize.rs#L420

Also having the same issue. Serializing a key: Vec<T> produces key[0]=a,key[1]=b,key[2]=c (for key = vec![a,b,c], but the expected result here would be: key=a,b,c.

I would be happy to send a PR which tries to fix this @samscott89 - could you give me some pointers?

To be clear - is T in this case a struct or a primitive? In the former case, the same as above applies.

For the case where you want to serialize the vector as comma-separated. This isn't currently supported, but there is an example of achieving similar using serde attributes in the examples.

Would you be able to do something like that?

edit to add: if you don't have structs, you may find that you don't gain much by using serde_qs over something simpler like serde_urlencoded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants