From a2c65db3398f19dc0a23f17aa0d69f6403f778f1 Mon Sep 17 00:00:00 2001 From: Diego Henrique Oliveira Date: Tue, 16 Apr 2024 09:05:28 -0300 Subject: [PATCH] Ensure getVars only returns primitive values when value == --- jsonlogic.go | 4 ++-- jsonlogic_test.go | 25 +++++++++++++++++++++++++ vars.go | 9 ++++++++- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/jsonlogic.go b/jsonlogic.go index 704cbf5..6968101 100644 --- a/jsonlogic.go +++ b/jsonlogic.go @@ -423,8 +423,8 @@ func some(values, data interface{}) interface{} { for _, value := range subject.([]interface{}) { v := apply( solveVars( - solveVars(parsed[1], value), - data, + solveVars(parsed[1], data), + value, ), value, ) diff --git a/jsonlogic_test.go b/jsonlogic_test.go index feefb9c..5bda383 100644 --- a/jsonlogic_test.go +++ b/jsonlogic_test.go @@ -907,3 +907,28 @@ func TestIssue79(t *testing.T) { expected := `true` assert.JSONEq(t, expected, result.String()) } + +func TestIssue81(t *testing.T) { + rule := `{ + "some": [ + {"var": "A"}, + {"!=": [ + {"var": ".B"}, + {"var": "B"} + ]} + ]} + ` + + data := `{"A":[{"B":1}], "B":2}` + + var result bytes.Buffer + + err := Apply(strings.NewReader(rule), strings.NewReader(data), &result) + + if err != nil { + t.Fatal(err) + } + + expected := `true` + assert.JSONEq(t, expected, result.String()) +} diff --git a/vars.go b/vars.go index b375eb9..10bf073 100644 --- a/vars.go +++ b/vars.go @@ -45,7 +45,14 @@ func solveVars(values, data interface{}) interface{} { } func getVar(value, data interface{}) interface{} { - if value == nil || (isString(value) && toString(value) == "") { + if value == nil { + if !isPrimitive(data) { + return nil + } + return data + } + + if isString(value) && toString(value) == "" { return data }