nearcore
nearcore copied to clipboard
Warn about unknown fields when deserialising JSON files
Currently, if serde encounters an unknown field it silently ignores it. We might want to at least warn if this happens. I’ve just spent noticeable amount of time debugging an issue caused by a typo in config.json
file.
If I understand correctly we may try to use that crate?: https://github.com/dtolnay/serde-ignored
Yes, looks exactly like what we want.
If so, am I right that we can do that in the following way: We can create a de wrapper type and use it for debug deserializations? Something like that:
impl<T> DeserializedWithDebug <T> {
fn deserialize<'de, D>(deserializer: D) -> Result<T, D::Error>
where
T: Deserialize<'de>,
D: Deserializer<'de>,
{
let mut unused = Set::new();
let p: Package = serde_ignored::deserialize(deserializer, |path| {
unused.insert(path.to_string());
});
debug!(unused);
p
}
}
That looks reasonable. The exact formatting for the debug message would need some work of course and maybe even have the function return (T, Vec<String>)
rather than printing the unrecognised keys, but in general seems to be what we want.
Thanks for the comments. So I'm ready to tackle on that.
It looks like this has been implemented in https://github.com/near/nearcore/pull/6490, but I don't get any warnings when I add an invalid option to the config. Opened another issue about it: https://github.com/near/nearcore/issues/10309
Is this done ? https://github.com/near/nearcore/blob/2b01868d472dfcec36f269f9c2ec450694dd5529/nearcore/src/config.rs#L462