From 4ed80966feea2eb907f226d612a8eefc851d921b Mon Sep 17 00:00:00 2001 From: Ameer Ghani Date: Thu, 22 Feb 2024 13:09:26 -0500 Subject: [PATCH] Fix bug in deserializing unknown features (#823) --- src/entity/aggregator/feature.rs | 68 ++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/entity/aggregator/feature.rs b/src/entity/aggregator/feature.rs index b4be24f8..1f953e1c 100644 --- a/src/entity/aggregator/feature.rs +++ b/src/entity/aggregator/feature.rs @@ -4,6 +4,7 @@ use std::collections::HashSet; #[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash)] pub enum Feature { TokenHash, + #[serde(untagged)] Unknown(String), } @@ -27,3 +28,70 @@ impl FromIterator for Features { Self(iter.into_iter().collect()) } } + +#[cfg(test)] +mod tests { + use serde_json::json; + + use crate::entity::aggregator::{Feature, Features}; + + #[test] + fn features_none() { + assert_eq!( + serde_json::from_value::(json!([])).unwrap(), + Features::from_iter([]) + ) + } + + #[test] + fn features_token_hash() { + assert_eq!( + serde_json::from_value::(json!(["TokenHash"])).unwrap(), + Features::from_iter([Feature::TokenHash]) + ); + assert_eq!( + serde_json::from_value::(json!(["TokenHash", "TokenHash", "TokenHash"])) + .unwrap(), + Features::from_iter([Feature::TokenHash]) + ); + } + + #[test] + fn features_unknown() { + assert_eq!( + serde_json::from_value::(json!(["UnspecifiedUnknownFeature"])).unwrap(), + Features::from_iter([Feature::Unknown("UnspecifiedUnknownFeature".to_string())]) + ); + + assert_eq!( + serde_json::from_value::(json!([ + "UnspecifiedUnknownFeature", + "UnspecifiedUnknownFeature", + "UnspecifiedUnknownFeature" + ])) + .unwrap(), + Features::from_iter([Feature::Unknown("UnspecifiedUnknownFeature".to_string())]) + ); + + assert_eq!( + serde_json::from_value::(json!([ + "UnspecifiedUnknownFeature", + "AnotherUnspecifiedUnknownFeature" + ])) + .unwrap(), + Features::from_iter([ + Feature::Unknown("UnspecifiedUnknownFeature".to_string()), + Feature::Unknown("AnotherUnspecifiedUnknownFeature".to_string()) + ]) + ); + + assert_eq!( + serde_json::from_value::(json!(["UnspecifiedUnknownFeature", "TokenHash"])) + .unwrap(), + Features::from_iter([ + Feature::TokenHash, + Feature::Unknown("UnspecifiedUnknownFeature".to_string()) + ]) + ); + } +}