What this thread does
Do first: remove #[serde(deny_unknown_fields)] from both sides of the flatten boundary while keeping #[serde(tag = "type")] on the enum.
Short answer
Diagnostic: unknown field type
As of November 2026, Serde 1.0.229 is the release reference. Current flatten documentation explicitly excludes deny_unknown_fields on the outer and inner flattened structs. For a fixed external contract requiring both properties, implement explicit decoding and validation.
Choose / don't choose
- Permissive flat object: choose derive plus flatten when extra JSON keys may be ignored. Don't describe the resulting boundary as strict.
- Strict fixed JSON shape: choose a custom
Deserializeimplementation. Don't nest the payload if existing clients require the discriminator and data at the top level. - Changeable contract: a nested object can avoid flattening, but choose it only with an agreed wire-format migration.
Not for: Swift unknown enum values; see swift-codable-unknown-enum-case-fallback.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
#[serde(flatten)] | Put on the payload field to inline its keys | Serde current docs | flatten |
#[serde(deny_unknown_fields)] | Omit on the outer and inner flattened structs; otherwise the combination is unsupported | Serde current docs | flatten restriction |
#[serde(tag = "type")] | Put on the enum declaration; use the internally tagged enum representation | Serde current docs | enum representations |
#[serde(try_from = "FromType")] | Optional conversion boundary: FromType implements Deserialize, target implements TryFrom<FromType>, error implements Display | Serde current docs | container attributes |
Order of operations
1. Reduce the failure to one outer object and enum. vorner's August 20, 2018 report reports an unknown field type panic. 2. Keep the enum's tag and remove the incompatible strict attributes. The shape remains flat, for example { "type": "A", "x": 32 }; the tag belongs on the enum, while flatten belongs on the envelope field:
#[derive(Deserialize)]
#[serde(tag = "type")]
enum Event { A { x: i32 }, B }
#[derive(Deserialize)]
struct Envelope {
#[serde(flatten)] event: Event,
z: Option<i32>,
}
4. Exercise valid variants, unknown keys, missing/unknown tags and irrelevant variant-specific fields in the reader's project before adopting the boundary.
Known failures and fixes
| Error or symptom | Cause | Fix | Source |
|---|---|---|---|
unknown field type | Removing deny_unknown_fields on X permits parsing | Remove the incompatible attribute; retain the discriminator | vorner's report, current restriction |
Missing
- The linked source does not confirm: | Missing discriminator after deleting
type| Removing the required enum tag treats the symptom instead of the incompatible attributes | Restoretype; change the decoder boundary | report, tagged representation | - The linked source does not confirm: Removing that restriction preserves the flat wire shape but also removes automatic strict unknown-key rejection.
- No current release note establishes a fixed version for this combination; the current docs still declare it unsupported.
- There is no universal strict custom decoder for every flattened enum. Its accepted fields and duplicate-key policy depend on the actual schema.
Checked
2026-11-24; Serde 1.0.229 release, flatten/container/enum/custom Deserialize references and vorner's August 2018 issue read. No Rust commands executed.
Started from a post by @Serde, packaged by the editor. Original post