From 393be40179cc3981ab3c69745162853586d13778 Mon Sep 17 00:00:00 2001 From: dogukanakkaya Date: Mon, 14 Aug 2023 01:22:14 +0300 Subject: [PATCH] Refactor empty arrays/objects should return empty instead of null --- permissive-json-pointer/src/lib.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/permissive-json-pointer/src/lib.rs b/permissive-json-pointer/src/lib.rs index 7e5b3371c..c771f3321 100644 --- a/permissive-json-pointer/src/lib.rs +++ b/permissive-json-pointer/src/lib.rs @@ -186,12 +186,16 @@ fn create_value(value: &Document, mut selectors: HashSet<&str>) -> Document { let array = create_array(array, &sub_selectors); if !array.is_empty() { new_value.insert(key.to_string(), array.into()); + } else { + new_value.insert(key.to_string(), Value::Array(vec![])); } } Value::Object(object) => { let object = create_value(object, sub_selectors); if !object.is_empty() { new_value.insert(key.to_string(), object.into()); + } else { + new_value.insert(key.to_string(), Value::Object(Map::new())); } } _ => (), @@ -211,6 +215,8 @@ fn create_array(array: &[Value], selectors: &HashSet<&str>) -> Vec { let array = create_array(array, selectors); if !array.is_empty() { res.push(array.into()); + } else { + res.push(Value::Array(vec![])); } } Value::Object(object) => { @@ -637,6 +643,24 @@ mod tests { ); } + #[test] + fn empty_array_object_return_empty() { + let value: Value = json!({ + "array": [], + "object": {}, + }); + let value: &Document = value.as_object().unwrap(); + + let res: Value = select_values(value, vec!["array.name", "object.name"]).into(); + assert_eq!( + res, + json!({ + "array": [], + "object": {}, + }) + ); + } + #[test] fn all_conflict_variation() { let value: Value = json!({