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

Serialization round-trip fails with empty Vec #78

Open
dfaust opened this issue Feb 28, 2023 · 3 comments
Open

Serialization round-trip fails with empty Vec #78

dfaust opened this issue Feb 28, 2023 · 3 comments

Comments

@dfaust
Copy link

dfaust commented Feb 28, 2023

When serializing a struct containing an empty Vec, the Vec is completely dropped.
But when de-serializing the struct, the Vec is required.
This breaks the serialization round-trip.

My expectation is that the de-serializing should work, even if the Vec is not present.

Here is an example:

#[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
struct Data {
    values: Vec<String>,
}

#[test]
fn serialization_roundtrip() {
    let data = Data { values: Vec::new() };

    let serialized = serde_qs::to_string(&data).unwrap();

    dbg!(&serialized);

    let deserialized = serde_qs::from_str::<Data>(&serialized).unwrap();

    assert_eq!(deserialized, data);
}
@samscott89
Copy link
Owner

Hey @dfaust!

Thanks for opening an issue. You raise an interesting problem. I'm struggling to come up with a way that we could solve this on the serde_qs side. I don't think there's any way for us to represent an empty vector.

One thing you could maybe do as a workaround it:

#[test]
fn serialization_roundtrip() {
    #[derive(Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
    struct Data {
        #[serde(default)] // <<<<< use this
        values: Vec<String>,
    }

    let data = Data { values: Vec::new() };
    let serialized = serde_qs::to_string(&data).unwrap();

    dbg!(&serialized);
    let deserialized = serde_qs::from_str::<Data>(&serialized).unwrap();
    assert_eq!(deserialized, data);
}

Do you think that would be possible?

@dfaust
Copy link
Author

dfaust commented Mar 2, 2023

Thanks @samscott89 for the quick reply!

Do you think that would be possible?

Sure. That's an easy enough work-around. 👍

I still hope that you can find a solution though, since this behavior is quite surprising.

Anyway, I still love your crate. Being able to serialize Vecs as URLs is fantastic!

@gitmalong
Copy link

Issue also occurs with an empty HashSet

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