Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Deserialize empty map and default value #304

Closed
ibigbug opened this issue Aug 8, 2022 · 1 comment · Fixed by #312
Closed

Deserialize empty map and default value #304

ibigbug opened this issue Aug 8, 2022 · 1 comment · Fixed by #312

Comments

@ibigbug
Copy link

ibigbug commented Aug 8, 2022

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
Copy link
Owner

dtolnay commented Aug 13, 2022

Fixed in 0.9.7.

molpopgen added a commit to molpopgen/demes-rs that referenced this issue Aug 22, 2022
serde_yaml 0.9.7 resulted in a regression,
accepting `metadata:` (empty metadata mappings)
instead of returning Err.

See dtolnay/serde-yaml#304 and dtolnay/serde-yaml#312

This change uses a custom deserializer to error
in this case.
molpopgen added a commit to molpopgen/demes-rs that referenced this issue Aug 22, 2022
serde_yaml 0.9.7 resulted in a regression,
accepting `metadata:` (empty metadata mappings)
instead of returning Err.

See dtolnay/serde-yaml#304 and dtolnay/serde-yaml#312

This change uses a custom deserializer to error
in this case.
molpopgen added a commit to molpopgen/demes-rs that referenced this issue Aug 22, 2022
serde_yaml 0.9.7 resulted in a regression,
accepting `metadata:` (empty metadata mappings)
instead of returning Err.

See dtolnay/serde-yaml#304 and dtolnay/serde-yaml#312

This change uses a custom deserializer to error
in this case.
molpopgen added a commit to molpopgen/demes-rs that referenced this issue Aug 22, 2022
* refactor: Metadata now uses custom deserializer

serde_yaml 0.9.7 resulted in a regression,
accepting `metadata:` (empty metadata mappings)
instead of returning Err.

See dtolnay/serde-yaml#304 and dtolnay/serde-yaml#312

This change uses a custom deserializer to error
in this case.

* fix: fix clippy::unnecessary_to_owned lint
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants