Correctly serialize newtype variants for flatten

This commit is contained in:
Armin Ronacher 2018-03-20 23:05:05 +01:00
parent 99614c7266
commit 27f935f036
2 changed files with 37 additions and 2 deletions

View File

@ -1161,13 +1161,14 @@ impl<'a, M> Serializer for FlatMapSerializer<'a, M>
self,
_: &'static str,
_: u32,
_: &'static str,
variant: &'static str,
value: &T,
) -> Result<Self::Ok, Self::Error>
where
T: Serialize,
{
value.serialize(self)
try!(self.0.serialize_key(variant));
self.0.serialize_value(value)
}
fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq, Self::Error> {

View File

@ -1763,3 +1763,37 @@ fn test_lifetime_propagation_for_flatten() {
],
);
}
#[test]
fn test_flatten_enum_newtype() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
struct S {
#[serde(flatten)]
flat: E,
}
#[derive(Serialize, Deserialize, PartialEq, Debug)]
enum E {
Q(HashMap<String, String>),
}
let e = E::Q({
let mut map = HashMap::new();
map.insert("k".to_owned(), "v".to_owned());
map
});
let s = S { flat: e };
assert_tokens(
&s,
&[
Token::Map { len: None },
Token::Str("Q"),
Token::Map { len: Some(1) },
Token::Str("k"),
Token::Str("v"),
Token::MapEnd,
Token::MapEnd,
],
);
}