Skip to content

Reading HashMap from empty json fails with type error. #624

Closed
@dil-zgaal

Description

@dil-zgaal

version: 0.15.4

Config fails to initialize (an empty) HashMap from an empty object.

Repro:

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Config {
    pub data: HashMap<String, String>,
}
...
 let cfg = config::Config::builder()
            .add_source(config::File::from_str(
                //r#"{"data": {"a": "b"}}"#,  //  it works fine
                r#"{"data": {}}"#,                // it fails
                config::FileFormat::Json,
            ))
            .build()
            .unwrap();
        let cfg = cfg.try_deserialize::<Config>();
        log::info!("configuration: {:#?}", cfg);

Expected behavior: data is initialized to an empty HashMap.
Actual result: Err( invalid type: unit value, expected a map )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions