← LeaderboardThe Skill Leaderboard
rust-serde-field-alias-conflicts
Migrate renamed JSON fields in Rust Serde. Test alias collisions and accept agreeing old and new keys when the wire contract permits both.
Try your first task
A source-grounded starter task has not been verified for this skill yet.
SKILL.md
name: rust-serde-field-alias-conflictsdescription: Migrate renamed JSON fields in Rust Serde. Test alias collisions and accept agreeing old and new keys when the wire contract permits both.category: developmentrisk: criticalsource: selfsource_type: selfdate_added: "2026-12-03"author: ShianMiketags: [rust, serde, serde-json, compatibility, field-alias]Rust Serde field alias conflicts
When to Use
- A Rust reader accepts a renamed JSON field with
#[serde(alias = "...")]and rejects payloads containing both spelling variants. - An API, persisted settings file, or migration producer can send the old and new names together.
- A compatibility test needs to preserve missing, null, invalid, or repeated-key behavior during a field rename.
Prerequisites
- Identify the JSON reader, the versions of all relevant writers, and the field's requiredness, nullability, and type.
- Find the project contract for two separate spellings in the same object. Decide whether to reject all such objects, accept equal values, or apply a documented precedence rule.
- Use raw JSON fixtures from each shape that can reach the reader. The example uses Serde 1.0.229 and serde_json 1.0.154.
1. Reproduce the alias collision
- Deserialize old-only and new-only objects. An alias adds an input name for the same field; it does not add another output name.
- Test an object with both spellings and matching values. A derived alias can report a duplicate field because both names target one field.
- Swap the order of the two names in a second object. Test conflicting values independently so the contract does not depend on which key appears first.
- Use raw JSON when repeated literal keys must be rejected. A generic map parsed beforehand can lose information about repeated occurrences.
2. Decode the two names before applying the conflict policy
- Keep a plain alias when one actual field per object is the contract. A duplicate-field error is appropriate for that contract.
- When both names may coexist, use a private input struct with separate fields and run a fallible conversion to the public application type.
#[serde(try_from = "Wire")]connects these stages while leaving serialization defined by the public type. - Give absent fields an internal optional representation. Preserve the original null behavior at each name. For a required non-null integer, the example uses
defaultfor an absent field anddeserialize_withto require a valid integer when it is present. - Apply the agreed policy after decoding. The example accepts two equal values, rejects disagreement, and rejects a missing value under both names. Typed struct decoding continues to detect repeated occurrences of one spelling.
3. Verify storage and rollout compatibility
- Compare decoded domain values for each accepted shape, including a valid zero. Assert the serialized key via a JSON value or schema test.
- Include matching keys in both orders, conflicting keys, a repeated literal spelling, missing input, null, and a wrong type. Compare outcomes at the type boundary, then exercise the actual reader or writer.
- Check whether older readers can read the newly emitted spelling before altering the writer. Follow the project's versioning contract for persistent or mixed-version inputs.
- Record which shapes were tested with which versions. Keep the legacy input name until the compatibility window permits removal.
Example
Accept agreeing field names demonstrates a failing alias on a dual-written setting. It reads both names separately, checks their agreement, and emits only the canonical name.
Limitations
- The example uses one required non-null JSON integer and a policy that permits equal values. A truly nullable field needs a representation for omitted, null, and concrete states.
- Older writers, readers, schema generation, and other Serde formats are outside the fixture. Verify their contracts separately.
- Reading both names does not automatically make a writer compatible with readers that recognize only the older name.
Safety and Side Effects
Keep examples synthetic and compare raw fixtures before altering the reader or writer in production. Changes to the serialized name or conflict policy can affect persisted data and downstream consumers.
Related Skills and Documentation
- Rust Pro gives general Rust engineering guidance.
- Robius State Management covers application state and persistence.
- Serde field attributes describes input aliases and per-field defaults.
- Serde container attributes describes fallible conversion.