samscott89 / serde_qs

Serde support for querystring-style strings
Apache License 2.0
193 stars 68 forks source link

failed deserialize untagged enum #108

Open sixleaveakkm opened 1 month ago

sixleaveakkm commented 1 month ago

I ran into an unexpected error and simplified it as the following code

    #[derive(Deserialize)]
    struct A {
        x: String,
        a: String,
    }

    #[derive(Deserialize)]
    struct B {
        x: String,
        b: String,
    }

    #[derive(Deserialize)]
    #[serde(untagged)]
    enum Q {
        A(A),
        B(B),
    }

    #[test]
    fn test_parse() {
        let q = "x=1&a=2";
        let a: Q = serde_urlencoded::from_str(q).unwrap();
        let a: Q = serde_qs::from_str(q).unwrap(); // called `Result::unwrap()` on an `Err` value: Custom("cannot deserialize primitive at the top level.Try deserializing into a struct.")
    }

checked with serde_json and serde_urlencoded which both successfully deserde as expected, consider this may be some limitation of serde_qs, as it support enum type:

At the top level, serde_qs only supports struct, map, and enum. These are the only top-level structs which can be de/serialized since Querystrings rely on having a (key, value) pair for each field, which necessitates this kind of structure.