Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
dil-zgaal opened this issue Dec 21, 2024 · 2 comments
Open

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

dil-zgaal opened this issue Dec 21, 2024 · 2 comments

Comments

@dil-zgaal
Copy link

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 )

@arielb1
Copy link

arielb1 commented Dec 22, 2024

This seems to happen every time a value is initialized from an empty HashMap, including as a set_default

use config::Config;
use std::collections::HashMap;

fn main() {
    let config = Config::builder().set_default("var", HashMap::<String, i64>::new()).unwrap().build().unwrap();
    // returns empty HashMap on 0.14.*/0.15.2, Errs on "invalid type: unit value, expected a map" on 0.15.3/0.15.4
    let val : HashMap<String, i64> = config.get("var").unwrap();
    println!("Hello, world! {:?}", val);
}

@Holzhaus
Copy link

I'm stumbling over this as well. Also happens with structs where all fields are Option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants