config-rs icon indicating copy to clipboard operation
config-rs copied to clipboard

Incorrect serialize/deserialize struct with empty Vec field

Open XX opened this issue 5 years ago • 3 comments

I wrote the following test:

#[test]
fn test_empty_seq() {
    #[derive(Debug, Serialize, Deserialize, PartialEq)]
    struct Test {
        int: u32,
        seq: Vec<String>,
    }

    let test = Test {
        int: 1,
        seq: vec![],
    };
    let config = Config::try_from(&test).unwrap();

    let actual: Test = config.try_into().unwrap();
    assert_eq!(test, actual);
}

After running it displays this error:

thread 'ser::test::test_empty_seq' panicked at 'called `Result::unwrap()` on an `Err` value: missing field `seq`', src/libcore/result.rs:997:5

When I add the #[serde(default)] attribute to the seq field, it is successful, but if the struct contains only an empty-vec-field, it does not help.

XX avatar Jun 15 '19 18:06 XX

Is this still true for the latest release?

matthiasbeyer avatar Mar 19 '21 09:03 matthiasbeyer

Is this still true for the latest release?

Yes, this is still true for 0.11.

XX avatar Apr 03 '21 11:04 XX