serde icon indicating copy to clipboard operation
serde copied to clipboard

Strict flattening

Open taorepoara opened this issue 5 months ago • 1 comments

There is many issues around the flatten field attribute: https://github.com/serde-rs/serde/issues?q=is%3Aissue+is%3Aopen+flatten

I think that adding a strict mode to that attribute could be a good way to solve at least some of them.

I see many way to define the strict mode:

  • as a feature: -F flatten_strict. I think it's not the best solution since it would change the flatten for the whole project.
  • as an attribute value: #[serde(flatten = "strict")] or #[serde(flatten = true)]
  • as a new attribute: #[serde(flatten_strict)]

The strict flatten behavior would first parse the flattened fields one by one, removing the managed fields of the nested struct form the parsed data and ignoring the deny_unknown_fields on those struct. The parsing would be the same as not flatten struct so it would probably solve many issues.

Then the main struct remaining fields would be parsed thanks to the remaining data fields. This way the deny_unknown_fields would work on the main struct.

taorepoara avatar Aug 27 '24 05:08 taorepoara