diff --git a/src/lib.rs b/src/lib.rs index 89abf1e..fd53ff2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -524,7 +524,7 @@ mod tests { } #[test] - fn catchall_schema() { + fn catchall_schema_v0() { #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] struct Catchall(HashMap); impl SchemaV0 for Catchall {} @@ -539,6 +539,64 @@ mod tests { ) } + #[test] + fn catchall_schema_v1_v2() { + #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] + struct Catchall0(HashMap); + impl SchemaV0 for Catchall0 {} + + #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] + struct Catchall1 { + count: usize, + #[serde(flatten)] + data: HashMap, + } + impl Schema for Catchall1 { + type Prev = Catchall0; + } + impl From for Catchall1 { + fn from(old: Catchall0) -> Self { + Catchall1 { + data: old.0, + count: 0, + } + } + } + + #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)] + struct Catchall2 { + count: usize, + name: String, + #[serde(flatten)] + data: HashMap, + } + impl Schema for Catchall2 { + type Prev = Catchall1; + } + impl From for Catchall2 { + fn from(old: Catchall1) -> Self { + Catchall2 { + data: old.data, + count: old.count, + name: String::new(), + } + } + } + + let mut data = HashMap::new(); + data.insert("hello".into(), "world".into()); + data.insert("catch".into(), "all".into()); + + assert_eq!( + Catchall2::parse(r#"{"version":1,"count":42,"hello":"world","catch":"all"}"#).unwrap(), + Catchall2 { + data, + count: 42, + name: String::new(), + }, + ) + } + #[test] fn unversioned() { #[derive(Debug, PartialEq, Eq, Deserialize, Serialize)]