diff --git a/serde/src/private/de.rs b/serde/src/private/de.rs index 383dc8b8..d771d089 100644 --- a/serde/src/private/de.rs +++ b/serde/src/private/de.rs @@ -1310,7 +1310,12 @@ mod content { where V: Visitor<'de>, { - visitor.visit_newtype_struct(self) + match self.content { + Content::Newtype(v) => { + visitor.visit_newtype_struct(ContentDeserializer::new(*v)) + } + _ => visitor.visit_newtype_struct(self), + } } fn deserialize_seq(self, visitor: V) -> Result @@ -2001,7 +2006,12 @@ mod content { where V: Visitor<'de>, { - visitor.visit_newtype_struct(self) + match *self.content { + Content::Newtype(ref v) => { + visitor.visit_newtype_struct(ContentRefDeserializer::new(v)) + } + _ => visitor.visit_newtype_struct(self), + } } fn deserialize_seq(self, visitor: V) -> Result diff --git a/test_suite/tests/test_macros.rs b/test_suite/tests/test_macros.rs index dc17c525..caf5ffbc 100644 --- a/test_suite/tests/test_macros.rs +++ b/test_suite/tests/test_macros.rs @@ -446,6 +446,54 @@ fn test_generic_newtype_struct() { ); } +#[test] +fn test_untagged_newtype_struct() { + #[derive(Debug, PartialEq, Serialize, Deserialize)] + #[serde(untagged)] + enum E { + Newtype(GenericNewTypeStruct), + Null, + } + + assert_tokens( + &E::Newtype(GenericNewTypeStruct(5u32)), + &[ + Token::NewtypeStruct { + name: "GenericNewTypeStruct", + }, + Token::U32(5), + ], + ); +} + +#[test] +fn test_adjacently_tagged_newtype_struct() { + #[derive(Debug, PartialEq, Serialize, Deserialize)] + #[serde(tag = "t", content = "c")] + enum E { + Newtype(GenericNewTypeStruct), + Null, + } + + assert_de_tokens( + &E::Newtype(GenericNewTypeStruct(5u32)), + &[ + Token::Struct { + name: "E", + len: 2, + }, + Token::Str("c"), + Token::NewtypeStruct { + name: "GenericNewTypeStruct", + }, + Token::U32(5), + Token::Str("t"), + Token::Str("Newtype"), + Token::StructEnd, + ], + ); +} + #[test] fn test_generic_tuple_struct() { assert_tokens(