diff --git a/src/main/java/io/github/jamsesso/jsonlogic/JsonLogicException.java b/src/main/java/io/github/jamsesso/jsonlogic/JsonLogicException.java index 71b961f..0e34710 100644 --- a/src/main/java/io/github/jamsesso/jsonlogic/JsonLogicException.java +++ b/src/main/java/io/github/jamsesso/jsonlogic/JsonLogicException.java @@ -18,6 +18,11 @@ public JsonLogicException(Throwable cause, String jsonPath) { this.jsonPath = jsonPath; } + public JsonLogicException(String msg, Throwable cause, String jsonPath) { + super(msg, cause); + this.jsonPath = jsonPath; + } + public String getJsonPath() { return jsonPath; } diff --git a/src/main/java/io/github/jamsesso/jsonlogic/ast/JsonLogicParseException.java b/src/main/java/io/github/jamsesso/jsonlogic/ast/JsonLogicParseException.java index 1df20ec..8bc5873 100644 --- a/src/main/java/io/github/jamsesso/jsonlogic/ast/JsonLogicParseException.java +++ b/src/main/java/io/github/jamsesso/jsonlogic/ast/JsonLogicParseException.java @@ -10,4 +10,8 @@ public JsonLogicParseException(String msg, String jsonPath) { public JsonLogicParseException(Throwable cause, String jsonPath) { super(cause, jsonPath); } + + public JsonLogicParseException(String msg, Throwable cause, String jsonPath) { + super(msg, cause, jsonPath); + } } diff --git a/src/main/java/io/github/jamsesso/jsonlogic/evaluator/JsonLogicEvaluationException.java b/src/main/java/io/github/jamsesso/jsonlogic/evaluator/JsonLogicEvaluationException.java index 2fcf35d..b35d2bc 100644 --- a/src/main/java/io/github/jamsesso/jsonlogic/evaluator/JsonLogicEvaluationException.java +++ b/src/main/java/io/github/jamsesso/jsonlogic/evaluator/JsonLogicEvaluationException.java @@ -10,4 +10,8 @@ public JsonLogicEvaluationException(String msg, String jsonPath) { public JsonLogicEvaluationException(Throwable cause, String jsonPath) { super(cause, jsonPath); } + + public JsonLogicEvaluationException(String msg, Throwable cause, String jsonPath) { + super(msg, cause, jsonPath); + } } diff --git a/src/main/java/io/github/jamsesso/jsonlogic/evaluator/expressions/MathExpression.java b/src/main/java/io/github/jamsesso/jsonlogic/evaluator/expressions/MathExpression.java index 4703715..e1d656b 100644 --- a/src/main/java/io/github/jamsesso/jsonlogic/evaluator/expressions/MathExpression.java +++ b/src/main/java/io/github/jamsesso/jsonlogic/evaluator/expressions/MathExpression.java @@ -48,7 +48,11 @@ public Object evaluate(List arguments, Object data, String jsonPath) throws Json if (key.equals("*") || key.equals("+")) { while (ArrayLike.isEligible(value)) { - value = new ArrayLike(value).get(0); + ArrayLike array = new ArrayLike(value); + if (array.isEmpty()) { + break; + } + value = array.get(0); } } if (value instanceof String) { diff --git a/src/test/java/io/github/jamsesso/jsonlogic/MathExpressionTests.java b/src/test/java/io/github/jamsesso/jsonlogic/MathExpressionTests.java index b7a356b..773f706 100644 --- a/src/test/java/io/github/jamsesso/jsonlogic/MathExpressionTests.java +++ b/src/test/java/io/github/jamsesso/jsonlogic/MathExpressionTests.java @@ -95,6 +95,14 @@ public void testMultiplyWithArray() throws JsonLogicException { assertEquals(6.0, result); // This matches reference impl at jsonlogic.com } + @Test + public void testMultiplyWithEmptyArray() throws JsonLogicException { + String json = "{\"*\":[2,[]]}"; + Object result = jsonLogic.apply(json, null); + + assertEquals(null, result); // This matches reference impl at jsonlogic.com + } + @Test public void testDivide() throws JsonLogicException { String json = "{\"/\":[4,2]}"; diff --git a/src/test/resources/fixtures.json b/src/test/resources/fixtures.json index fa39dfc..8c4f7bf 100644 --- a/src/test/resources/fixtures.json +++ b/src/test/resources/fixtures.json @@ -931,6 +931,36 @@ {}, 2 ], + [ + { + "+": [ + 1, + [2, 3] + ] + }, + {}, + 3 + ], + [ + { + "+": [ + 1, + [] + ] + }, + {}, + null + ], + [ + { + "+": [ + 1, + null + ] + }, + {}, + null + ], [ { "*": [ @@ -971,6 +1001,46 @@ {}, 1 ], + [ + { + "*": [ + [3, 4], + [2, 3] + ] + }, + {}, + 6 + ], + [ + { + "*": [ + 3, + [] + ] + }, + {}, + null + ], + [ + { + "*": [ + [], + [] + ] + }, + {}, + null + ], + [ + { + "*": [ + 3, + null + ] + }, + {}, + null + ], [ { "-": [ @@ -3826,4 +3896,4 @@ { "item": ["item4"] }, false ] -] \ No newline at end of file +]