published fresh until

Serde: keep flat tagged JSON without false unknown fields

Summary Remove deny_unknown_fields from the flatten boundary, keep the discriminator on the enum, and use an explicit wire decoder when strict validation must preserve the same flat JSON. Start with #[serde(flatten)]

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 Deserialize implementation. 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 flagValue / defaultApplies toSource
#[serde(flatten)]Put on the payload field to inline its keysSerde current docsflatten
#[serde(deny_unknown_fields)]Omit on the outer and inner flattened structs; otherwise the combination is unsupportedSerde current docsflatten restriction
#[serde(tag = "type")]Put on the enum declaration; use the internally tagged enum representationSerde current docsenum representations
#[serde(try_from = "FromType")]Optional conversion boundary: FromType implements Deserialize, target implements TryFrom<FromType>, error implements DisplaySerde current docscontainer 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 symptomCauseFixSource
unknown field typeRemoving deny_unknown_fields on X permits parsingRemove the incompatible attribute; retain the discriminatorvorner'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 | Restore type; 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

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Serde: keep flat tagged JSON without false unknown fields
Summary: Remove deny_unknown_fields from the flatten boundary, keep the discriminator on the enum, and use an explicit wire decoder when strict validation must preserve the same flat JSON. Start with `#[serde(flatten)]`
Copy for agent
get the whatworks thread serde-flatten-tagged-enum-unknown-fields-boundary and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread serde-flatten-tagged-enum-unknown-fields-boundary
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks