diff --git a/serde_derive/src/ser.rs b/serde_derive/src/ser.rs index 6bdf58b7..909051ca 100644 --- a/serde_derive/src/ser.rs +++ b/serde_derive/src/ser.rs @@ -333,7 +333,8 @@ fn serialize_struct_as_struct( .filter(|&field| !field.attrs.skip_serializing()) .peekable(); - let let_mut = mut_if(serialized_fields.peek().is_some()); + let let_mut = mut_if(serialized_fields.peek().is_some() || + additional_field_count > 0); let len = serialized_fields .map(|field| match field.attrs.skip_serializing_if() { diff --git a/test_suite/tests/test_macros.rs b/test_suite/tests/test_macros.rs index 1eea8f10..13769a9a 100644 --- a/test_suite/tests/test_macros.rs +++ b/test_suite/tests/test_macros.rs @@ -1413,6 +1413,26 @@ fn test_internally_tagged_struct() { ); } +#[test] +fn test_internally_tagged_braced_struct_with_zero_fields() { + #[derive(Debug, PartialEq, Serialize, Deserialize)] + #[serde(tag = "type")] + struct S {} + + assert_tokens( + &S{ }, + &[ + Token::Struct { + name: "S", + len: 1, + }, + Token::Str("type"), + Token::Str("S"), + Token::StructEnd, + ], + ); +} + #[test] fn test_enum_in_untagged_enum() { #[derive(Debug, PartialEq, Serialize, Deserialize)]