Skip to content
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

Strict flattening #2812

Open
taorepoara opened this issue Aug 27, 2024 · 1 comment
Open

Strict flattening #2812

taorepoara opened this issue Aug 27, 2024 · 1 comment

Comments

@taorepoara
Copy link

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.

@Mingun
Copy link
Contributor

Mingun commented Aug 27, 2024

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

  • introduce new trait to mark types that can be flatten into other types. I thought for a long time about a suitable name and my last idea is Composite. Actually, two traits would be required, one for serialization and one for deserialization, because only serialization or only deserialization can be implemented by a type, CompositeDeserialize and CompositeSerialize.
  • add new attribute for flattening
  • the new traits should have methods to save / build the type in a composable way. More detailed design in this Avoid lossy buffering in #[serde(flatten)] #2186 (comment):
  • if field is marked with that new attribute, add Composite* bound to the corresponding types
  • implement (de)serialization in terms of new traits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants