Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make #[serde(alias)] works in #[serde(flatten)] context #2387

Merged
merged 2 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion serde_derive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2400,7 +2400,8 @@ fn deserialize_struct_as_struct_visitor(
.collect();

let fields_stmt = {
let field_names = field_names_idents.iter().map(|(name, _, _)| name);
let field_names = field_names_idents.iter().flat_map(|(_, _, aliases)| aliases);

quote_block! {
const FIELDS: &'static [&'static str] = &[ #(#field_names),* ];
}
Expand Down
72 changes: 67 additions & 5 deletions test_suite/tests/test_annotations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ fn test_unknown_field_rename_struct() {
Token::Str("a4"),
Token::I32(3),
],
"unknown field `a4`, expected one of `a1`, `a2`, `a6`",
"unknown field `a4`, expected one of `a1`, `a3`, `a2`, `a5`, `a6`",
);
}

Expand Down Expand Up @@ -780,7 +780,7 @@ fn test_rename_enum() {
Token::StructVariant {
name: "AliasEnum",
variant: "sailor_moon",
len: 3,
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
len: 5,
},
Token::Str("a"),
Token::I8(0),
Expand All @@ -798,7 +798,7 @@ fn test_rename_enum() {
Token::StructVariant {
name: "AliasEnum",
variant: "usagi_tsukino",
len: 3,
len: 5,
},
Token::Str("a"),
Token::I8(0),
Expand Down Expand Up @@ -827,7 +827,7 @@ fn test_unknown_field_rename_enum() {
Token::StructVariant {
name: "AliasEnum",
variant: "usagi_tsukino",
len: 3,
len: 5,
},
Token::Str("a"),
Token::I8(0),
Expand All @@ -836,7 +836,7 @@ fn test_unknown_field_rename_enum() {
Token::Str("d"),
Token::I8(2),
],
"unknown field `d`, expected one of `a`, `b`, `f`",
"unknown field `d`, expected one of `a`, `c`, `b`, `e`, `f`",
);
}

Expand Down Expand Up @@ -2658,6 +2658,68 @@ fn test_flatten_any_after_flatten_struct() {
);
}

#[test]
fn test_alias_in_flatten_context() {
#[derive(Debug, PartialEq, Deserialize)]
struct Outer {
#[serde(flatten)]
a: AliasStruct,
b: i32,
}

assert_de_tokens(
&Outer {
a: AliasStruct {
a1: 1,
a2: 2,
a4: 4,
},
b: 7,
},
&[
Token::Struct {
name: "Outer",
len: 4,
},
Token::Str("a1"),
Token::I32(1),
Token::Str("a2"),
Token::I32(2),
Token::Str("a5"),
Token::I32(4),
Token::Str("b"),
Token::I32(7),
Token::StructEnd,
],
);

assert_de_tokens(
&Outer {
a: AliasStruct {
a1: 1,
a2: 2,
a4: 4,
},
b: 7,
},
&[
Token::Struct {
name: "Outer",
len: 4,
},
Token::Str("a1"),
Token::I32(1),
Token::Str("a2"),
Token::I32(2),
Token::Str("a6"),
Token::I32(4),
Token::Str("b"),
Token::I32(7),
Token::StructEnd,
],
);
}

#[test]
fn test_expecting_message() {
#[derive(Deserialize, PartialEq, Debug)]
Expand Down