Repository: diegoholiveira/jsonlogic
Pull request #42 (2021-08-02 19:30:05)

Fixing nil exception for in operation
rule: `{"filter":[{"var": "accounts"},{"and":[{"in":["abc",{"var":"tags.tag-1"}]}]}]}`
data: `{"accounts":[{"id":"111111111111","name":"account-2","tags":{"tag-1":"abc"},"createdAt":"2021-06-24T11:14:25Z","updatedAt":"2021-06-24T11:14:25Z"},{"id":"111111111117","name":"account-7","tags":{"mytag":"custom"},"createdAt":"2021-06-24T11:14:25Z","updatedAt":"2021-06-24T11:14:25Z"}]}`

playground code that shows the problem: https://play.golang.org/p/vH_npwpjLFM

diegoholiveira: Can you please add a test into `jsonlogic_test.go`? This way we use it as a regression test to make sure future refactoring doesn't break this again.

Thanks in advance.
codecov[bot]: # [Codecov](https://codecov.io/gh/diegoholiveira/jsonlogic/pull/42?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Diego+Oliveira) Report
> Merging [#42](https://codecov.io/gh/diegoholiveira/jsonlogic/pull/42?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Diego+Oliveira) (add26fb) into [master](https://codecov.io/gh/diegoholiveira/jsonlogic/commit/10d266f79dc23dfe3aa25f46464655988f6168f5?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=Diego+Oliveira) (10d266f) will **increase** coverage by `0.01%`.
> The diff coverage is `100.00%`.

```diff
@@            Coverage Diff             @@
##           master      #42      +/-   ##
==========================================
+ Coverage   95.63%   95.64%   +0.01%     
==========================================
  Files           9        9              
  Lines         641      643       +2     
==========================================
+ Hits          613      615       +2     
  Misses         15       15              
  Partials       13       13              
```


darrendao: @diegoholiveira unit test added

Patch:
diff --git a/jsonlogic.go b/jsonlogic.go
index a1db1bd..37e92a2 100644
--- a/jsonlogic.go
+++ b/jsonlogic.go
@@ -161,6 +161,10 @@ func _inSorted(value interface{}, values interface{}) bool {
 }
 
 func _in(value interface{}, values interface{}) bool {
+	if value == nil || values == nil {
+		return false
+	}
+
 	if isString(values) {
 		return strings.Contains(values.(string), value.(string))
 	}


Test patch:
diff --git a/jsonlogic_test.go b/jsonlogic_test.go
index f0fea44..c884508 100644
--- a/jsonlogic_test.go
+++ b/jsonlogic_test.go
@@ -539,3 +539,25 @@ func TestUnaryOperation(t *testing.T) {
 
 	assert.JSONEq(t, `true`, result.String())
 }
+
+func TestInOperatorAgainstNil(t *testing.T) {
+	rule := strings.NewReader(`{"filter":[{"var": "accounts"},{"and":[{"in":["abc",{"var":"tags.tag-1"}]}]}]}`)
+	data := strings.NewReader(`{"accounts":[{"name":"account-1","tags":{"tag-1":"abc"}}, {"name":"account-2","tags":{"tag-2":"xyz"}}]}`)
+
+	var result bytes.Buffer
+	err := Apply(rule, data, &result)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	expected := `[
+		{
+			"name": "account-1",
+			"tags": {
+				"tag-1": "abc"
+			}
+		}
+	]`
+
+	assert.JSONEq(t, expected, result.String())
+}