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

fix: don't check skipped variant with internal tag #2266

Merged
merged 1 commit into from Jul 26, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 4 additions & 2 deletions serde_derive/src/internals/check.rs
Expand Up @@ -267,8 +267,10 @@ fn check_internal_tag_field_name_conflict(cx: &Ctxt, cont: &Container) {
match variant.style {
Style::Struct => {
for field in &variant.fields {
let check_ser = !field.attrs.skip_serializing();
let check_de = !field.attrs.skip_deserializing();
let check_ser =
!(field.attrs.skip_serializing() || variant.attrs.skip_serializing());
let check_de =
!(field.attrs.skip_deserializing() || variant.attrs.skip_deserializing());
let name = field.attrs.name();
let ser_name = name.serialize_name();

Expand Down
31 changes: 31 additions & 0 deletions test_suite/tests/test_annotations.rs
Expand Up @@ -2265,6 +2265,37 @@ fn test_externally_tagged_enum_containing_flatten() {
);
}

#[test]
fn test_internally_tagged_enum_with_skipped_conflict() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
#[serde(tag = "t")]
enum Data {
A,
#[serde(skip)]
B {
t: String
},
Comment on lines +2274 to +2277
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What mean that variant is skipped? How it behaves when you try to serialize that variant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variant cannot be serialized & deserialized. Serializing it will throw a runtime error Err(Error("the enum variant Data::B cannot be serialized", line: 0, column: 0)).

C {
#[serde(default, skip)]
t: String
},
}

let _data = Data::B { t: "".to_string() };

let data = Data::C { t: "".to_string() };

assert_tokens(
&data,
&[
Token::Struct { name: "Data", len: 1 },
Token::Str("t"),
Token::Str("C"),
Token::StructEnd,
],
);
}

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