config-rs
config-rs copied to clipboard
serde flatten does not work for type in flattened structs that are not strings
Using the flatten attribute from serde almost works but breaks in the case of non string values in flattened structs. In this case config always parses size as a string. However, if I put the size attribute directly in Config then everything works.
#[derive(Deserialize)]
struct Config {
#[serde(flatten)]
pub subconfig: Subconfig
}
#[derive(Deserialize)]
struct Subconfig {
pub size: usize
}
The issue occur when parsing ini (parsed as str), hjson (parsed as float) and env (parsed as str in Windows)
I just ran into this issue when merging env with a toml file on a linux system.
There is a workaround - annotate those fields with e.g. #[serde(deserialize_with = "deserialize_bool_from_anything")] from use serde_aux::prelude::*;
That sounds right, would you contribute a patch introducing some tests showcasing it? That means, failing ones without the proper annotation and succeeding ones with the proper annotation? :laughing: