Skip to content

Commit af0e2e6

Browse files
committed
Add a warning that flatten and deny_unknown_fields should not be mixed.
There are various bug reports about the interaction of `flatten` and `deny_unknown_fields`. In the end they cannot really be combined, since flatten does not consume the fields thus `deny_unknown_fields` will error on them. serde-rs/serde#1957 serde-rs/serde#1600 serde-rs/serde#1547
1 parent 33a897a commit af0e2e6

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

_src/attr-flatten.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ The `flatten` attribute inlines keys from a field into the parent struct.
55
supported only within structs that have named fields, and the field to which it
66
is applied must be a struct or map type.
77

8+
**Note:** The `flatten` attribute clashes with
9+
[`deny_unknown_fields`](container-attrs.md#deny_unknown_fields) and might make
10+
the struct un-deserializable. Neither the outer nor the inner flattened struct
11+
should use [`deny_unknown_fields`](container-attrs.md#deny_unknown_fields).
12+
813
The `flatten` attribute serves the following two common use cases:
914

1015
### Factor out frequently grouped keys

_src/container-attrs.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@
3030
this attribute is not present, by default unknown fields are ignored for
3131
self-describing formats like JSON.
3232

33+
**Note:** This attribute clashes with [`flatten`](attr-flatten.md) and might
34+
make the struct un-deserializable. Neither the outer nor the inner flattened
35+
struct should use `deny_unknown_fields`.
36+
3337
- ##### `#[serde(tag = "type")]` {#tag}
3438

3539
Use the internally tagged enum representation for this enum, with the given

_src/field-attrs.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@
3838
with arbitrary string keys. The [struct flattening](attr-flatten.md) page
3939
provides some examples.
4040

41+
**Note:** This attribute clashes with
42+
[`deny_unknown_fields`](container-attrs.md#deny_unknown_fields) and might make
43+
the struct un-deserializable. Neither the outer nor the inner flattened struct
44+
should use[`deny_unknown_fields`](container-attrs.md#deny_unknown_fields).
45+
4146
- ##### `#[serde(skip)]` {#skip}
4247

4348
Skip this field: do not serialize or deserialize it.

0 commit comments

Comments
 (0)