nearcore icon indicating copy to clipboard operation
nearcore copied to clipboard

Warn about unknown fields when deserialising JSON files

Open mina86 opened this issue 2 years ago • 7 comments

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.

mina86 avatar Mar 15 '22 17:03 mina86

If I understand correctly we may try to use that crate?: https://github.com/dtolnay/serde-ignored

kakoc avatar Mar 23 '22 06:03 kakoc

Yes, looks exactly like what we want.

mina86 avatar Mar 23 '22 10:03 mina86

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
    }
}

kakoc avatar Mar 23 '22 15:03 kakoc

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.

mina86 avatar Mar 23 '22 17:03 mina86

Thanks for the comments. So I'm ready to tackle on that.

kakoc avatar Mar 24 '22 12:03 kakoc

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

jancionear avatar Dec 07 '23 10:12 jancionear

Is this done ? https://github.com/near/nearcore/blob/2b01868d472dfcec36f269f9c2ec450694dd5529/nearcore/src/config.rs#L462

andrei-near avatar May 30 '24 13:05 andrei-near