Skip to content

Commit ed0c286

Browse files
fix: v1 behaviour hardening (#502)
1 parent fe43b4c commit ed0c286

6 files changed

Lines changed: 179 additions & 8 deletions

File tree

‎core/engine/src/error.rs‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ impl fmt::Display for CompileFailure {
4545
}
4646

4747
#[derive(Debug, Error)]
48+
#[non_exhaustive]
4849
pub enum EvaluationError {
4950
#[error("Loader error")]
5051
LoaderError(LoaderError),

‎core/engine/src/lib.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,6 @@
118118
//! }
119119
//! ```
120120
121-
// `deny` rather than `forbid` so `nodes::variable_json` can opt in: reading a
122-
// `RefCell` without a guard is the only way to hand jsonschema `'a` borrows.
123121
#![forbid(unsafe_code)]
124122
#![deny(clippy::unwrap_used)]
125123
#![allow(clippy::module_inception)]

‎core/engine/src/nodes/variable_json.rs‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,14 @@ impl JsonNumber for VariableNumber {
9999
}
100100

101101
fn to_number(&self) -> Cow<'_, serde_json::Number> {
102-
let number = self
103-
.0
104-
.to_f64()
105-
.and_then(serde_json::Number::from_f64)
102+
let normalized = self.0.normalize().to_string();
103+
#[cfg(feature = "arbitrary_precision")]
104+
let number = serde_json::Number::from_string_unchecked(normalized);
105+
#[cfg(not(feature = "arbitrary_precision"))]
106+
let number = normalized
107+
.parse()
108+
.ok()
109+
.or_else(|| self.0.to_f64().and_then(serde_json::Number::from_f64))
106110
.unwrap_or_else(|| serde_json::Number::from(0));
107111
Cow::Owned(number)
108112
}

‎core/engine/tests/decision.rs‎

Lines changed: 163 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ use serde_json::json;
33
use std::ops::Deref;
44
use std::sync::Arc;
55
use tokio::runtime::Builder;
6-
use zen_engine::{Decision, DecisionGraphValidationError, EvaluationError, EvaluationOptions};
6+
use zen_engine::{
7+
Decision, DecisionGraphValidationError, EvaluationError, EvaluationOptions, Variable,
8+
};
79

810
mod support;
911

@@ -244,3 +246,163 @@ async fn decision_table_missing_cell_key_is_treated_as_empty() {
244246
.unwrap();
245247
assert_eq!(traced.result, result.result);
246248
}
249+
250+
#[tokio::test]
251+
#[cfg_attr(miri, ignore)]
252+
async fn node_handlers_do_not_mutate_aliased_inputs() {
253+
let content = serde_json::from_value(json!({
254+
"nodes": [
255+
{ "id": "in", "name": "in", "type": "inputNode", "content": {} },
256+
{
257+
"id": "e1", "name": "e1", "type": "expressionNode",
258+
"content": {
259+
"passThrough": true,
260+
"expressions": [{ "id": "x1", "key": "first", "value": "a + 1" }]
261+
}
262+
},
263+
{
264+
"id": "sw", "name": "sw", "type": "switchNode",
265+
"content": {
266+
"hitPolicy": "first",
267+
"statements": [{ "id": "s1", "condition": "" }]
268+
}
269+
},
270+
{
271+
"id": "e2", "name": "e2", "type": "expressionNode",
272+
"content": {
273+
"passThrough": true,
274+
"expressions": [{ "id": "x2", "key": "second", "value": "first + 1" }]
275+
}
276+
},
277+
{
278+
"id": "dt", "name": "dt", "type": "decisionTableNode",
279+
"content": {
280+
"passThrough": true,
281+
"hitPolicy": "first",
282+
"inputs": [{ "id": "i1", "name": "A", "field": "a" }],
283+
"outputs": [{ "id": "o1", "name": "Result", "field": "dtResult" }],
284+
"rules": [{ "_id": "r1", "i1": "", "o1": "'hit'" }]
285+
}
286+
},
287+
{ "id": "out", "name": "out", "type": "outputNode", "content": {} }
288+
],
289+
"edges": [
290+
{ "id": "ed1", "sourceId": "in", "targetId": "e1" },
291+
{ "id": "ed2", "sourceId": "e1", "targetId": "sw" },
292+
{ "id": "ed3", "sourceId": "sw", "targetId": "e2", "sourceHandle": "s1" },
293+
{ "id": "ed4", "sourceId": "e2", "targetId": "dt" },
294+
{ "id": "ed5", "sourceId": "dt", "targetId": "out" }
295+
]
296+
}))
297+
.unwrap();
298+
let decision = Decision::from(Arc::new(content));
299+
300+
let input: Variable = json!({ "a": 1, "nested": { "k": "v" } }).into();
301+
let snapshot = input.to_value();
302+
303+
let response = decision
304+
.evaluate_with_opts(
305+
input.clone(),
306+
EvaluationOptions {
307+
trace: true,
308+
..Default::default()
309+
},
310+
)
311+
.await
312+
.unwrap();
313+
314+
assert_eq!(input.to_value(), snapshot);
315+
assert_eq!(
316+
response.result,
317+
json!({ "a": 1, "nested": { "k": "v" }, "first": 2, "second": 3, "dtResult": "hit" })
318+
.into()
319+
);
320+
321+
let trace = response.trace.unwrap().into_graph().unwrap();
322+
let output_of = |id: &str| trace.get(id).unwrap().output.clone();
323+
324+
assert_eq!(
325+
output_of("in"),
326+
json!({ "a": 1, "nested": { "k": "v" } }).into()
327+
);
328+
assert_eq!(
329+
output_of("e1"),
330+
json!({ "a": 1, "nested": { "k": "v" }, "first": 2 }).into()
331+
);
332+
assert_eq!(
333+
output_of("sw"),
334+
json!({ "a": 1, "nested": { "k": "v" }, "first": 2 }).into()
335+
);
336+
assert_eq!(
337+
output_of("e2"),
338+
json!({ "a": 1, "nested": { "k": "v" }, "first": 2, "second": 3 }).into()
339+
);
340+
}
341+
342+
#[tokio::test]
343+
#[cfg_attr(miri, ignore)]
344+
async fn merged_inputs_do_not_mutate_parent_node_outputs() {
345+
let content = serde_json::from_value(json!({
346+
"nodes": [
347+
{ "id": "in", "name": "in", "type": "inputNode", "content": {} },
348+
{
349+
"id": "pa", "name": "pa", "type": "expressionNode",
350+
"content": {
351+
"passThrough": true,
352+
"expressions": [{ "id": "x1", "key": "pa", "value": "10" }]
353+
}
354+
},
355+
{
356+
"id": "pb", "name": "pb", "type": "expressionNode",
357+
"content": {
358+
"passThrough": true,
359+
"expressions": [{ "id": "x2", "key": "pb", "value": "20" }]
360+
}
361+
},
362+
{
363+
"id": "join", "name": "join", "type": "expressionNode",
364+
"content": {
365+
"passThrough": true,
366+
"expressions": [{ "id": "x3", "key": "sum", "value": "pa + pb" }]
367+
}
368+
},
369+
{ "id": "out", "name": "out", "type": "outputNode", "content": {} }
370+
],
371+
"edges": [
372+
{ "id": "ed1", "sourceId": "in", "targetId": "pa" },
373+
{ "id": "ed2", "sourceId": "in", "targetId": "pb" },
374+
{ "id": "ed3", "sourceId": "pa", "targetId": "join" },
375+
{ "id": "ed4", "sourceId": "pb", "targetId": "join" },
376+
{ "id": "ed5", "sourceId": "join", "targetId": "out" }
377+
]
378+
}))
379+
.unwrap();
380+
let decision = Decision::from(Arc::new(content));
381+
382+
let input: Variable = json!({ "a": 1 }).into();
383+
let snapshot = input.to_value();
384+
385+
let response = decision
386+
.evaluate_with_opts(
387+
input.clone(),
388+
EvaluationOptions {
389+
trace: true,
390+
..Default::default()
391+
},
392+
)
393+
.await
394+
.unwrap();
395+
396+
assert_eq!(input.to_value(), snapshot);
397+
assert_eq!(
398+
response.result,
399+
json!({ "a": 1, "pa": 10, "pb": 20, "sum": 30 }).into()
400+
);
401+
402+
let trace = response.trace.unwrap().into_graph().unwrap();
403+
let output_of = |id: &str| trace.get(id).unwrap().output.clone();
404+
405+
assert_eq!(output_of("in"), json!({ "a": 1 }).into());
406+
assert_eq!(output_of("pa"), json!({ "a": 1, "pa": 10 }).into());
407+
assert_eq!(output_of("pb"), json!({ "a": 1, "pb": 20 }).into());
408+
}

‎core/expression/src/functions/internal.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -476,7 +476,7 @@ pub(crate) mod imp {
476476
let a = args.array(0)?;
477477
let arr = a.borrow();
478478

479-
let Some(first) = arr.first() else {
479+
let Some(first) = arr.iter().find(|item| !matches!(item, V::Null)) else {
480480
return Ok(V::empty_object());
481481
};
482482

‎core/expression/tests/data/standard.csv‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,11 @@ flatMap([[1, 2], [3, 4], [5, 6]], #);; [1, 2, 3, 4, 5, 6]
161161
keys([10, 11, 12]);;[0, 1, 2]
162162
merge([[1, 2], [3, 4], [5]]);;[1, 2, 3, 4, 5]
163163
merge([[1, 2], [], [3]]);;[1, 2, 3]
164+
merge([null, [1, 2], [3]]);;[1, 2, 3]
165+
merge([[1, 2], null, [3]]);;[1, 2, 3]
166+
merge([null, {"a": 1}, {"b": 2}]);;{"a": 1, "b": 2}
167+
merge([null]);;{}
168+
merge([null, null]);;{}
164169
merge([]);;{}
165170
merge([{"a": 1}, {"b": 2}, {"c": 3}]);;{"a": 1, "b": 2, "c": 3}
166171
merge([{"a": 1, "b": 2}, {"b": 3, "c": 4}]);;{"a": 1, "b": 3, "c": 4}
@@ -170,6 +175,7 @@ mergeDeep([{"a": {"x": 1}}, {"a": {"y": 2}}]);;{"a": {"x": 1, "y": 2}}
170175
mergeDeep([{"a": [1, 2]}, {"a": [3, 4]}]);;{"a": [1, 2, 3, 4]}
171176
mergeDeep([{"a": {"b": {"c": 1}}}, {"a": {"b": {"d": 2}}}]);;{"a": {"b": {"c": 1, "d": 2}}}
172177
mergeDeep([{"a": 1}, {"a": 2}]);;{"a": 2}
178+
mergeDeep([null, {"a": 1}]);;{"a": 1}
173179
mergeDeep([]);;{}
174180

175181
# Dates

0 commit comments

Comments
 (0)