Skip to content

Commit b3f7968

Browse files
committed
toBoolean() might return null #50
1 parent 4415e2c commit b3f7968

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

src/main/java/com/dashjoin/jsonata/Functions.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,7 +1607,8 @@ public static List filter(List arr, Object func) throws Throwable {
16071607
var func_args = hofFuncArgs(func, entry, i, arr);
16081608
// invoke func
16091609
var res = funcApply(func, func_args);
1610-
if (toBoolean(res)) {
1610+
var booledValue = toBoolean(res);
1611+
if (booledValue == null ? false : booledValue) {
16111612
result.add(entry);
16121613
}
16131614
}
@@ -1638,7 +1639,8 @@ public static Object single(List arr, Object func) throws Throwable {
16381639
var func_args = hofFuncArgs(func, entry, i, arr);
16391640
// invoke func
16401641
var res = funcApply(func, func_args);
1641-
positiveResult = toBoolean(res);
1642+
var booledValue = toBoolean(res);
1643+
positiveResult = booledValue == null ? false : booledValue;
16421644
}
16431645
if (positiveResult) {
16441646
if(!hasFoundMatch) {

src/test/java/com/dashjoin/jsonata/NullSafetyTest.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.dashjoin.jsonata;
22

33
import static com.dashjoin.jsonata.Jsonata.jsonata;
4-
4+
import java.util.Arrays;
55
import org.junit.jupiter.api.Assertions;
66
import org.junit.jupiter.api.Test;
77

@@ -36,4 +36,21 @@ public void testNullSafety() {
3636
res = jsonata("$spread(null)").evaluate(null);
3737
Assertions.assertEquals(null, res);
3838
}
39+
40+
@Test
41+
public void testFilterNull() {
42+
var x = Jsonata.jsonata("$filter($, function($v, $i, $a){$v})").evaluate(Arrays.asList(1, null));
43+
Assertions.assertEquals(1, x);
44+
}
45+
46+
@Test
47+
public void testNotNull() {
48+
Assertions.assertNull(Jsonata.jsonata("$not($)").evaluate(null));
49+
}
50+
51+
@Test
52+
public void testSingleNull() {
53+
var x = Jsonata.jsonata("$single($, function($v, $i, $a){ $v })").evaluate(Arrays.asList(null, 1));
54+
Assertions.assertEquals(1, x);
55+
}
3956
}

0 commit comments

Comments
 (0)