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

Closed
dil-zgaal opened this issue Dec 21, 2024 · 5 comments · Fixed by #627
Closed

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

dil-zgaal opened this issue Dec 21, 2024 · 5 comments · Fixed by #627

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.

@silwol
Copy link

silwol commented Jan 9, 2025

Encountered this issue as well. Appears to be a regression, it worked with 0.15.2, but fails with 0.15.3 and 0.15.4.

@silwol
Copy link

silwol commented Jan 9, 2025

git bisect identified ec36bff as the commit that introduced this issue.

@epage
Copy link
Contributor

epage commented Jan 13, 2025

FYI a fix is proposed in #627

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

Successfully merging a pull request may close this issue.

5 participants