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

Allow internal tag field in untagged variant #2646

Merged
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: 3 additions & 0 deletions serde_derive/src/internals/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
for variant in variants {
match variant.style {
Style::Struct => {
if variant.attrs.untagged() {
continue;
}
for field in &variant.fields {
let check_ser =
!(field.attrs.skip_serializing() || variant.attrs.skip_serializing());
Expand Down
94 changes: 94 additions & 0 deletions test_suite/tests/test_macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,100 @@ fn test_internally_tagged_enum() {
);
}

#[test]
fn test_internally_tagged_enum_with_untagged_variant() {
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind")]
enum InternallyTagged {
Tagged {
a: u8,
},
#[serde(untagged)]
Untagged {
kind: String,
b: u8,
},
}

assert_de_tokens(
&InternallyTagged::Tagged { a: 1 },
&[
Token::Map { len: Some(2) },
Token::Str("kind"),
Token::Str("Tagged"),
Token::Str("a"),
Token::U8(1),
Token::MapEnd,
],
);

assert_tokens(
&InternallyTagged::Tagged { a: 1 },
&[
Token::Struct {
name: "InternallyTagged",
len: 2,
},
Token::Str("kind"),
Token::Str("Tagged"),
Token::Str("a"),
Token::U8(1),
Token::StructEnd,
],
);

assert_de_tokens(
&InternallyTagged::Untagged {
kind: "Foo".to_owned(),
b: 2,
},
&[
Token::Map { len: Some(2) },
Token::Str("kind"),
Token::Str("Foo"),
Token::Str("b"),
Token::U8(2),
Token::MapEnd,
],
);

assert_tokens(
&InternallyTagged::Untagged {
kind: "Foo".to_owned(),
b: 2,
},
&[
Token::Struct {
name: "InternallyTagged",
len: 2,
},
Token::Str("kind"),
Token::Str("Foo"),
Token::Str("b"),
Token::U8(2),
Token::StructEnd,
],
);

assert_tokens(
&InternallyTagged::Untagged {
kind: "Tagged".to_owned(),
b: 2,
},
&[
Token::Struct {
name: "InternallyTagged",
len: 2,
},
Token::Str("kind"),
Token::Str("Tagged"),
Token::Str("b"),
Token::U8(2),
Token::StructEnd,
],
);
}

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