samscott89 / serde_qs

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

Failed parsing `[` and `]` in some cases #79

Open chungwong opened 1 year ago

chungwong commented 1 year ago

It looks like the parser doesn't understand the qs if it contains nested keys like a[b][c]

use serde::{
    Deserialize,
    Serialize,
};

#[derive(Debug, Deserialize)]
struct Officer {
    first_name: String,
    last_name: String,
}

#[derive(Debug, Deserialize)]
struct Test {
    foo: String,
    officers: Vec<Officer>,
}

fn main() {
    let encoded = "foo=&officers%5B0%5D%5Bfirst_name%5D=&officers%5B0%5D%5Blast_name%5D=";
    dbg!(serde_qs::from_str::<Test>(encoded));

    let decoded = "foo=&officers[0][first_name]=&officers[0][last_name]=";
    dbg!(serde_qs::from_str::<Test>(decoded));
}

/// error output
[src/main.rs:20] serde_qs::from_str::<Test>(encoded) = Err(
    Custom(
        "missing field `officers`",
    ),
)

// successful output
[src/main.rs:24] serde_qs::from_str::<Test>(decoded) = Ok(
    Test {
        foo: "",
        officers: [
            Officer {
                first_name: "",
                last_name: "",
            },
        ],
    },
)