From dc7d4560a0b13cd3a097522fb08fc682ba75e31b Mon Sep 17 00:00:00 2001 From: Vladimir Maksimovski Date: Sun, 30 Mar 2025 22:51:58 -0400 Subject: [PATCH] Null values are not discarded --- core/engine/tests/decision.rs | 13 ++++++++++ core/expression/src/variable/mod.rs | 23 +++++------------- test-data/test-null.json | 37 +++++++++++++++++++++++++++++ 3 files changed, 56 insertions(+), 17 deletions(-) create mode 100644 test-data/test-null.json diff --git a/core/engine/tests/decision.rs b/core/engine/tests/decision.rs index cc5609b0..3e8b9e83 100644 --- a/core/engine/tests/decision.rs +++ b/core/engine/tests/decision.rs @@ -88,3 +88,16 @@ fn decision_validation() { DecisionGraphValidationError::InvalidInputCount(_) )); } + +#[tokio::test] +#[cfg_attr(miri, ignore)] +async fn nulls_are_preserved_when_merging_incoming_nodes() { + let table_content = load_test_data("test-null.json"); + let decision = Decision::from(table_content); + + let context = json!({ "b": null, "c": { "d": null } }); + let result = decision.evaluate(context.into()).await; + + assert_eq!(result.unwrap().result, json!({ "b": null, "c": { "d": null } }).into()); +} + diff --git a/core/expression/src/variable/mod.rs b/core/expression/src/variable/mod.rs index 2c60b024..6c1df379 100644 --- a/core/expression/src/variable/mod.rs +++ b/core/expression/src/variable/mod.rs @@ -268,12 +268,8 @@ fn merge_variables( MergeStrategy::InPlace => { let mut map = doc_ref.borrow_mut(); for (key, value) in patch.deref() { - if value == &Variable::Null { - map.remove(key.as_str()); - } else { - let entry = map.entry(key.to_string()).or_insert(Variable::Null); - merge_variables(entry, value, false, strategy); - } + let entry = map.entry(key.to_string()).or_insert(Variable::Null); + merge_variables(entry, value, false, strategy); } return true; @@ -292,17 +288,10 @@ fn merge_variables( new_map.as_mut().unwrap() }; - if value == &Variable::Null { - // Remove null values - if map.remove(key.as_str()).is_some() { - changed = true; - } - } else { - // Handle nested merging - let entry = map.entry(key.to_string()).or_insert(Variable::Null); - if merge_variables(entry, value, false, strategy) { - changed = true; - } + // Handle nested merging + let entry = map.entry(key.to_string()).or_insert(Variable::Null); + if merge_variables(entry, value, false, strategy) { + changed = true; } } diff --git a/test-data/test-null.json b/test-data/test-null.json new file mode 100644 index 00000000..6ff402f5 --- /dev/null +++ b/test-data/test-null.json @@ -0,0 +1,37 @@ +{ + "contentType": "application/vnd.gorules.decision", + "nodes": [ + { + "id": "8f1971cc-1658-44b4-a64c-b6c734697e3d", + "name": "request", + "type": "inputNode", + "content": { + "schema": "" + }, + "position": { + "x": 110, + "y": 114 + } + }, + { + "id": "aff016a9-1d4f-4d5d-98a5-adf4f367f371", + "name": "response", + "type": "outputNode", + "content": { + "schema": "" + }, + "position": { + "x": 750, + "y": 114 + } + } + ], + "edges": [ + { + "id": "8dd114f0-06b3-4f7e-9213-4faaa5a855c8", + "type": "edge", + "sourceId": "8f1971cc-1658-44b4-a64c-b6c734697e3d", + "targetId": "aff016a9-1d4f-4d5d-98a5-adf4f367f371" + } + ] +} \ No newline at end of file