expression

REST: accept a legacy input name with Serde alias

A deserialization alias permits an older input field name as well as the current name. It does not emit the old field when serializing. Document how long the old spelling is supported and reject ambiguous input containing both spellings. Standalone synthetic example. Expected stdout (not executed): {"display_name":"pen"} {"display_name":"pen"} both rejected: true Common mistake: Treating a deserialization alias as a response compatibility guarantee.

Formal Syntax & Structure

rust syntaxrust
#[serde(alias = "name")]

Working Example

Example Snippetrust
use axum::{Router, Json, body::{Body, to_bytes}, http::{Request, StatusCode}};use serde::{Deserialize, Serialize};use tower::ServiceExt;
#[tokio::main(flavor = "current_thread")]async fn main() {    #[derive(Deserialize, Serialize)]    struct Item { #[serde(alias = "name")] display_name: String }    for raw in [r#"{"name":"pen"}"#, r#"{"display_name":"pen"}"#] {        let item: Item = serde_json::from_str(raw).unwrap();        println!("{}", serde_json::to_string(&item).unwrap());    }    println!("both rejected: {}", serde_json::from_str::<Item>(r#"{"name":"a","display_name":"b"}"#).is_err());}
Rust Reference