dtolnay/serde-yaml

A yaml array with 2 items can be parsed as a struct with two fields, is this on purpose?

jhoobergs opened this issue · 2 comments

Following test fails because it seems that a yaml array with 2 items can be parsed as a struct with two fields.

#[derive(Serialize, Deserialize)]
pub struct TemplateWithDefault<T> {
    pub template_key: String,
    pub default_value: Option<T>,
}

#[cfg(test)]
mod test {
    use super::*;
    #[test]
    fn parsing_strange_list_doesnt_work() {
        let yaml = r#"---
- nonjmetext§
- "template:test"
"#;

        let parsed: Result<TemplateWithDefault<String>, _> = serde_yaml::from_str(yaml);
        println!("{:?}", parsed);
        assert!(parsed.is_err())
    }
}

The println at the end yields Ok(TemplateWithDefault { template_key: "nonjmetext§", default_value: Some("template:test") })

I was not able to reproduce this. The test passes and the println prints this:

Err(Error("invalid type: sequence, expected struct TemplateWithDefault", line: 2, column: 1))

The test fails with v0.8 but passes with v0.9. This is not mentioned under breaking changes in the release notes of v0.9. serde_json does allow deserializing lists as structs.