config-rs icon indicating copy to clipboard operation
config-rs copied to clipboard

serde flatten does not work for type in flattened structs that are not strings

Open glademiller opened this issue 6 years ago • 4 comments

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
}

glademiller avatar Dec 19 '18 23:12 glademiller

The issue occur when parsing ini (parsed as str), hjson (parsed as float) and env (parsed as str in Windows)

kyleung271 avatar Feb 03 '19 04:02 kyleung271

I just ran into this issue when merging env with a toml file on a linux system.

Progdrasil avatar Feb 09 '21 16:02 Progdrasil

There is a workaround - annotate those fields with e.g. #[serde(deserialize_with = "deserialize_bool_from_anything")] from use serde_aux::prelude::*;

infinity0 avatar Jul 15 '21 16:07 infinity0

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:

matthiasbeyer avatar Jul 15 '21 20:07 matthiasbeyer