Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Fixed

- `Engine::add_data` now deep-merges nested data documents instead of only merging top-level keys. Adding `{ "a": { "x": 1 } }` followed by `{ "a": { "y": 2 } }` now yields `{ "a": { "x": 1, "y": 2 } }` (matching OPA's data-document merge). Nested sets under a shared key are unioned. Only genuine leaf conflicts (the same path holding two different values) are reported as errors.

## [0.10.1](https://github.com/microsoft/regorus/compare/regorus-v0.10.0...regorus-v0.10.1) - 2026-05-22

### Fixed
Expand Down
11 changes: 9 additions & 2 deletions src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,10 @@ impl Engine {

/// Add data document.
///
/// The specified data document is merged into existing data document.
/// The specified data document is deep-merged into the existing data document. Nested
/// objects are merged recursively (matching OPA's data-document merge), so adding
/// `{ "a": { "x": 1 } }` and then `{ "a": { "y": 2 } }` yields `{ "a": { "x": 1, "y": 2 } }`.
/// A conflict — the same path holding two different values — is an error.
///
/// ```
/// # use regorus::*;
Expand All @@ -453,9 +456,13 @@ impl Engine {
/// // Merge { "z" : 3 }. Conflict error.
/// assert!(engine.add_data(Value::from_json_str(r#"{ "z" : 3 }"#)?).is_err());
///
/// // Nested objects are deep-merged. Merge { "y" : { "a" : 10 } } then { "y" : { "b" : 20 } }.
/// assert!(engine.add_data(Value::from_json_str(r#"{ "y" : { "a" : 10 } }"#)?).is_ok());
/// assert!(engine.add_data(Value::from_json_str(r#"{ "y" : { "b" : 20 } }"#)?).is_ok());
///
/// assert_eq!(
/// engine.eval_query("data".to_string(), false)?.result[0].expressions[0].value,
/// Value::from_json_str(r#"{ "x": 1, "y": {}, "z": 2}"#)?
/// Value::from_json_str(r#"{ "x": 1, "y": { "a": 10, "b": 20 }, "z": 2}"#)?
/// );
/// # Ok(())
/// # }
Expand Down
349 changes: 349 additions & 0 deletions src/tests/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use anyhow::{bail, Result};
use core::num::NonZeroU32;
use core::time::Duration;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, BTreeSet};
use test_generator::test_resources;
use timer_test_support::{
apply_engine_timer, configure_time_source, reset_time_source, GlobalTimerGuard,
Expand Down Expand Up @@ -818,3 +819,351 @@ fn test_get_data() -> Result<()> {

Ok(())
}

#[test]
fn test_add_data_deep_merge() -> Result<()> {
let mut engine = Engine::new();

// Nested objects under a shared top-level key are deep-merged, not replaced.
engine.add_data(Value::from_json_str(r#"{ "a" : { "x" : 1 } }"#)?)?;
engine.add_data(Value::from_json_str(r#"{ "a" : { "y" : 2 } }"#)?)?;

assert_eq!(
engine.get_data(),
Value::from_json_str(r#"{ "a" : { "x" : 1, "y" : 2 } }"#)?
);

Ok(())
}

#[test]
fn test_add_data_deep_merge_multi_level() -> Result<()> {
let mut engine = Engine::new();

// Merging recurses through multiple levels of nesting.
engine.add_data(Value::from_json_str(
r#"{ "a" : { "b" : { "x" : 1 } }, "top" : 0 }"#,
)?)?;
engine.add_data(Value::from_json_str(
r#"{ "a" : { "b" : { "y" : 2 }, "c" : 3 } }"#,
)?)?;

assert_eq!(
engine.get_data(),
Value::from_json_str(r#"{ "a" : { "b" : { "x" : 1, "y" : 2 }, "c" : 3 }, "top" : 0 }"#)?
);

Ok(())
}

#[test]
fn test_add_data_leaf_conflict_errors() -> Result<()> {
let mut engine = Engine::new();

// A genuine leaf conflict (same nested path, different value) is an error.
engine.add_data(Value::from_json_str(r#"{ "a" : { "x" : 1 } }"#)?)?;
assert!(engine
.add_data(Value::from_json_str(r#"{ "a" : { "x" : 2 } }"#)?)
.is_err());

Ok(())
}

#[test]
fn test_add_data_object_vs_scalar_conflict_errors() -> Result<()> {
let mut engine = Engine::new();

// An object cannot be merged with a scalar at the same path.
engine.add_data(Value::from_json_str(r#"{ "a" : { "x" : 1 } }"#)?)?;
assert!(engine
.add_data(Value::from_json_str(r#"{ "a" : 5 }"#)?)
.is_err());

Ok(())
}

#[test]
fn test_add_data_equal_leaf_is_noop() -> Result<()> {
let mut engine = Engine::new();

// Re-adding identical data (including equal nested leaves) is tolerated as a no-op.
engine.add_data(Value::from_json_str(r#"{ "a" : { "x" : 1 } }"#)?)?;
engine.add_data(Value::from_json_str(r#"{ "a" : { "x" : 1 }, "b" : 2 }"#)?)?;

assert_eq!(
engine.get_data(),
Value::from_json_str(r#"{ "a" : { "x" : 1 }, "b" : 2 }"#)?
);

Ok(())
}

#[test]
fn test_add_data_set_union() -> Result<()> {
let mut engine = Engine::new();

// Sets under a shared key are unioned rather than conflicting (consistent with the
// rule-evaluation merge, where partial set rules accumulate elements). JSON cannot express
// sets, so the data documents are built via the `Value` API.
engine.add_data(Value::from(BTreeMap::from([(
Value::from("s"),
Value::from(BTreeSet::from([Value::from(1_u64), Value::from(2_u64)])),
)])))?;
engine.add_data(Value::from(BTreeMap::from([(
Value::from("s"),
Value::from(BTreeSet::from([Value::from(2_u64), Value::from(3_u64)])),
)])))?;

let expected = Value::from(BTreeMap::from([(
Value::from("s"),
Value::from(BTreeSet::from([
Value::from(1_u64),
Value::from(2_u64),
Value::from(3_u64),
])),
)]));
assert_eq!(engine.get_data(), expected);

Ok(())
}

#[test]
fn test_add_data_nested_set_union() -> Result<()> {
let mut engine = Engine::new();

// A set nested under an object key exercises the recursive merge: the outer objects are
// deep-merged and the inner sets are then unioned.
engine.add_data(Value::from(BTreeMap::from([(
Value::from("a"),
Value::from(BTreeMap::from([(
Value::from("s"),
Value::from(BTreeSet::from([Value::from(1_u64)])),
)])),
)])))?;
engine.add_data(Value::from(BTreeMap::from([(
Value::from("a"),
Value::from(BTreeMap::from([(
Value::from("s"),
Value::from(BTreeSet::from([Value::from(2_u64)])),
)])),
)])))?;

let expected = Value::from(BTreeMap::from([(
Value::from("a"),
Value::from(BTreeMap::from([(
Value::from("s"),
Value::from(BTreeSet::from([Value::from(1_u64), Value::from(2_u64)])),
)])),
)]));
assert_eq!(engine.get_data(), expected);

Ok(())
}

#[test]
fn test_add_data_equal_set_is_noop() -> Result<()> {
let mut engine = Engine::new();

// Re-adding an identical set is tolerated as a no-op (not a conflict).
engine.add_data(Value::from(BTreeMap::from([(
Value::from("s"),
Value::from(BTreeSet::from([Value::from(1_u64), Value::from(2_u64)])),
)])))?;
engine.add_data(Value::from(BTreeMap::from([(
Value::from("s"),
Value::from(BTreeSet::from([Value::from(1_u64), Value::from(2_u64)])),
)])))?;

let expected = Value::from(BTreeMap::from([(
Value::from("s"),
Value::from(BTreeSet::from([Value::from(1_u64), Value::from(2_u64)])),
)]));
assert_eq!(engine.get_data(), expected);

Ok(())
}

// The `Value::merge` used by `add_data` is shared with the rule-evaluation path
// (`Interpreter::merge_rule_value`, reached via `with data.* as ...` and rule-value
// materialization). The tests below pin down that making `merge` recursive changed only the
// data-document semantics and left rule evaluation — in particular the `with data.* as ...`
// modifier — behaving exactly as before (an override, never a deep merge).

#[test]
fn test_with_data_modifier_replaces_nested_object() -> Result<()> {
let mut engine = Engine::new();

// Base data provides a nested object with two keys.
engine.add_data(Value::from_json_str(
r#"{ "base" : { "foo" : { "a" : 1, "b" : 2 } } }"#,
)?)?;

engine.add_policy(
"policy.rego".to_string(),
r#"
package test

result := x if {
x := data.base.foo with data.base.foo as {"a": 99}
}
"#
.to_string(),
)?;

// `with data.base.foo as {"a": 99}` REPLACES the whole subtree for the duration of the
// rule; it must NOT deep-merge with the base `{ "a": 1, "b": 2 }`. So `b` is gone.
assert_eq!(
engine
.eval_query("data.test.result".to_string(), false)?
.result[0]
.expressions[0]
.value
.clone(),
Value::from_json_str(r#"{ "a" : 99 }"#)?
);

Ok(())
}

#[test]
fn test_with_data_modifier_replaces_whole_subtree() -> Result<()> {
let mut engine = Engine::new();

engine.add_data(Value::from_json_str(
r#"{ "base" : { "foo" : 1, "bar" : 2 } }"#,
)?)?;

engine.add_policy(
"policy.rego".to_string(),
r#"
package test

result := x if {
x := data.base with data.base as {"only": 3}
}
"#
.to_string(),
)?;

// `with data.base as {...}` replaces the entire `data.base` object; the original
// `foo`/`bar` keys are not merged in.
assert_eq!(
engine
.eval_query("data.test.result".to_string(), false)?
.result[0]
.expressions[0]
.value
.clone(),
Value::from_json_str(r#"{ "only" : 3 }"#)?
);

Ok(())
}

#[test]
fn test_with_data_modifier_nested_replace_preserves_siblings() -> Result<()> {
let mut engine = Engine::new();

// `data.base` has a nested `foo` object AND a sibling `bar`.
engine.add_data(Value::from_json_str(
r#"{ "base" : { "foo" : { "a" : 1, "b" : 2 }, "bar" : 7 } }"#,
)?)?;

engine.add_policy(
"policy.rego".to_string(),
r#"
package test

# `with` targets the nested `data.base.foo`, but the rule observes the PARENT `data.base`.
result := x if {
x := data.base with data.base.foo as {"a": 99}
}
"#
.to_string(),
)?;

// The nested `foo` is deep-replaced (its `b` is gone — `with` never merges), while the
// sibling `bar` under the same parent is preserved.
assert_eq!(
engine
.eval_query("data.test.result".to_string(), false)?
.result[0]
.expressions[0]
.value
.clone(),
Value::from_json_str(r#"{ "foo" : { "a" : 99 }, "bar" : 7 }"#)?
);

Ok(())
}

#[test]
fn test_rule_reads_deep_merged_base_data() -> Result<()> {
let mut engine = Engine::new();

// Two add_data calls deep-merge into a single nested object...
engine.add_data(Value::from_json_str(
r#"{ "base" : { "foo" : { "a" : 1 } } }"#,
)?)?;
engine.add_data(Value::from_json_str(
r#"{ "base" : { "foo" : { "b" : 2 } } }"#,
)?)?;

engine.add_policy(
"policy.rego".to_string(),
r#"
package test

a := data.base.foo.a
b := data.base.foo.b
"#
.to_string(),
)?;

// ...and both merged leaves are visible to rule evaluation.
assert_eq!(
engine.eval_query("data.test".to_string(), false)?.result[0].expressions[0]
.value
.clone(),
Value::from_json_str(r#"{ "a" : 1, "b" : 2 }"#)?
);

Ok(())
}

#[test]
fn test_rule_values_coexist_with_merged_base_data() -> Result<()> {
let mut engine = Engine::new();

// Deep-merged base data under `base`...
engine.add_data(Value::from_json_str(
r#"{ "base" : { "foo" : { "a" : 1 } } }"#,
)?)?;
engine.add_data(Value::from_json_str(
r#"{ "base" : { "foo" : { "b" : 2 } } }"#,
)?)?;

engine.add_policy(
"policy.rego".to_string(),
r#"
package test

computed := data.base.foo.a + data.base.foo.b
"#
.to_string(),
)?;

let data = engine.eval_query("data".to_string(), false)?.result[0].expressions[0]
.value
.clone();

// Base data is preserved and deep-merged...
assert_eq!(
data["base"],
Value::from_json_str(r#"{ "foo" : { "a" : 1, "b" : 2 } }"#)?
);
// ...and the rule-computed value materializes alongside it without disturbing the merge.
assert_eq!(data["test"]["computed"], Value::from(3_u64));

Ok(())
}
Loading
Loading