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
serde(flatten) breaks metadata propagation in errors #80
Comments
The way that
This means that figment sees that an error occurred for the entire structure, not any individual key/value pair, and so it has no idea what to assign the error to. To concretize this, let's take a simplified version of your example: #[derive(Clone, Deserialize, Debug)]
struct TcpConfig {
timeout: u32,
}
#[derive(Clone, Deserialize, Debug)]
struct Config {
#[serde(flatten)]
port: TcpConfig,
} For the TOML file In the case of flattened structs, however, the internal "flattening" deserializer accepts everything. Thus the string This might be a bit confusing - I apologize. The details are rather gnarly. The short of it is: flattening breaks what it means to deserialize a structure by buffering values and lying that deserialization succeeded, depriving the true deserializer of the information about when errors occur. This means we can't emit a proper error during flattening. Perhaps there's some work-around, but I'll have to think about it more. |
See also serde-rs/serde#2186 (comment). |
Thank you for such a detailed reply! I didn't think to look into the details of how I was only using Perhaps the figment docs could have a mention of this "gotcha" under the "tips" section? |
Good idea! |
When using Figment with nested structs, some of which have
#[serde(flatten)]
attached, any error that occurs while loading the config for the nested flattened keys wont have any context info attached.In the following examples the
test.toml
config is invalid -- a string is specified where a u32 is expected. In the first exampleflatten
isn't used. In the second, the intermediate enum is flattened on the root struct.When run the output is:
The first error message is far more useful as it explains which key is invalid and which source file.
The text was updated successfully, but these errors were encountered: