dtolnay / serde-yaml

Strongly typed YAML library for Rust
Apache License 2.0
964 stars 164 forks source link

Deserialize empty map and default value #304

Closed ibigbug closed 2 years ago

ibigbug commented 2 years ago

I have this code

use std::collections::HashMap;
use serde::Deserialize;

#[derive(Deserialize, Debug)]
#[serde(default)]
struct Request {
    m: HashMap<String, String>
}

impl Default for Request {
    fn default() -> Self {
        Self {
            m: Default::default(),
        }
    }
}

fn main() {
    let json = r#"
    m:
    "#;

    let requests: Request = serde_yaml::from_str(json).unwrap();
    println!("{:?}", requests);
}

I'd expect the Request.m to be an empty map.

but it actually throws:

 value: Message("invalid type: unit value, expected a map", Some(Pos { marker: Marker { index: 12, line: 4, col: 0 }, path: "m" }))

why it's showing as uint?

can I get an empty map in this case?

playgroud: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=f2b451c3a8e43672a1e8632afcab23db

thanks

dtolnay commented 2 years ago

Fixed in 0.9.7.