serde
serde copied to clipboard
Fix roundtrip of flatten externally tagged enum unit variant
Unit variant of externally tagged enum can be deserialized when enum is flatten, but cannot be serialized -- ser test failed:
#![cfg(test)]
use serde::{Deserialize, Serialize}; // 1.0.204;
use serde_json::{from_str, to_string}; // 1.0.121
#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Flatten {
#[serde(flatten)]
data: Enum,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
enum Enum {
Unit,
}
const JSON: &str = r#"{"Unit":null}"#;
#[test]
fn de() {
// ok
assert_eq!(from_str::<Flatten>(JSON).unwrap(), Flatten { data: Enum::Unit });
}
#[test]
fn ser() {
// called `Result::unwrap()` on an `Err` value: Error("can only flatten structs and maps (got an enum)", line: 0, column: 0)
assert_eq!(to_string(&Flatten { data: Enum::Unit }).unwrap(), JSON);
}
This PR fixes this inconsistency
This PR does not conflicts with #2608, because changed files not moved in #2608, it has pretty obvious test case and reproduction example, so is it seems it shouldn't take too much time for review.