serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
9.15k stars 774 forks source link

Strict flattening #2812

Open taorepoara opened 2 months ago

taorepoara commented 2 months ago

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:

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.

Mingun commented 2 months ago

If introduce new attribute it would be better do the following: