Skip to content

Commit 96f0582

Browse files
authored
Fixes bug arising from operating on empty arrays in '+' and '*' (#55)
1 parent 6585a24 commit 96f0582

File tree

6 files changed

+97
-2
lines changed

6 files changed

+97
-2
lines changed

src/main/java/io/github/jamsesso/jsonlogic/JsonLogicException.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ public JsonLogicException(Throwable cause, String jsonPath) {
1818
this.jsonPath = jsonPath;
1919
}
2020

21+
public JsonLogicException(String msg, Throwable cause, String jsonPath) {
22+
super(msg, cause);
23+
this.jsonPath = jsonPath;
24+
}
25+
2126
public String getJsonPath() {
2227
return jsonPath;
2328
}

src/main/java/io/github/jamsesso/jsonlogic/ast/JsonLogicParseException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ public JsonLogicParseException(String msg, String jsonPath) {
1010
public JsonLogicParseException(Throwable cause, String jsonPath) {
1111
super(cause, jsonPath);
1212
}
13+
14+
public JsonLogicParseException(String msg, Throwable cause, String jsonPath) {
15+
super(msg, cause, jsonPath);
16+
}
1317
}

src/main/java/io/github/jamsesso/jsonlogic/evaluator/JsonLogicEvaluationException.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,8 @@ public JsonLogicEvaluationException(String msg, String jsonPath) {
1010
public JsonLogicEvaluationException(Throwable cause, String jsonPath) {
1111
super(cause, jsonPath);
1212
}
13+
14+
public JsonLogicEvaluationException(String msg, Throwable cause, String jsonPath) {
15+
super(msg, cause, jsonPath);
16+
}
1317
}

src/main/java/io/github/jamsesso/jsonlogic/evaluator/expressions/MathExpression.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ public Object evaluate(List arguments, Object data, String jsonPath) throws Json
4848

4949
if (key.equals("*") || key.equals("+")) {
5050
while (ArrayLike.isEligible(value)) {
51-
value = new ArrayLike(value).get(0);
51+
ArrayLike array = new ArrayLike(value);
52+
if (array.isEmpty()) {
53+
break;
54+
}
55+
value = array.get(0);
5256
}
5357
}
5458
if (value instanceof String) {

src/test/java/io/github/jamsesso/jsonlogic/MathExpressionTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ public void testMultiplyWithArray() throws JsonLogicException {
9595
assertEquals(6.0, result); // This matches reference impl at jsonlogic.com
9696
}
9797

98+
@Test
99+
public void testMultiplyWithEmptyArray() throws JsonLogicException {
100+
String json = "{\"*\":[2,[]]}";
101+
Object result = jsonLogic.apply(json, null);
102+
103+
assertEquals(null, result); // This matches reference impl at jsonlogic.com
104+
}
105+
98106
@Test
99107
public void testDivide() throws JsonLogicException {
100108
String json = "{\"/\":[4,2]}";

src/test/resources/fixtures.json

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,6 +931,36 @@
931931
{},
932932
2
933933
],
934+
[
935+
{
936+
"+": [
937+
1,
938+
[2, 3]
939+
]
940+
},
941+
{},
942+
3
943+
],
944+
[
945+
{
946+
"+": [
947+
1,
948+
[]
949+
]
950+
},
951+
{},
952+
null
953+
],
954+
[
955+
{
956+
"+": [
957+
1,
958+
null
959+
]
960+
},
961+
{},
962+
null
963+
],
934964
[
935965
{
936966
"*": [
@@ -971,6 +1001,46 @@
9711001
{},
9721002
1
9731003
],
1004+
[
1005+
{
1006+
"*": [
1007+
[3, 4],
1008+
[2, 3]
1009+
]
1010+
},
1011+
{},
1012+
6
1013+
],
1014+
[
1015+
{
1016+
"*": [
1017+
3,
1018+
[]
1019+
]
1020+
},
1021+
{},
1022+
null
1023+
],
1024+
[
1025+
{
1026+
"*": [
1027+
[],
1028+
[]
1029+
]
1030+
},
1031+
{},
1032+
null
1033+
],
1034+
[
1035+
{
1036+
"*": [
1037+
3,
1038+
null
1039+
]
1040+
},
1041+
{},
1042+
null
1043+
],
9741044
[
9751045
{
9761046
"-": [
@@ -3826,4 +3896,4 @@
38263896
{ "item": ["item4"] },
38273897
false
38283898
]
3829-
]
3899+
]

0 commit comments

Comments
 (0)