jamsesso / json-logic-java

A pure Java implementation of JsonLogic without using the Nashorn JS engine
MIT License
99 stars 50 forks source link

Getting value from Array by Index exception #18

Closed iliecirciumaru closed 3 years ago

iliecirciumaru commented 3 years ago

Description

According to JSONLogic specification following behaviour is expected:

jsonLogic.apply(
    {"var" : 1 },
    [ "apple", "banana", "carrot" ]
);
// "banana"

Note that index 1 or "1" should return same result

According to JsonLogicEvaluator.evaluatePartialVariable, I think was planned, that following expression returns null.

jsonLogic.apply(
    {"var" : "5" },
    [ "apple", "banana", "carrot" ]
);
// null

Problem

If index asked equals to size of the array, then exception is thrown instead of null return.

jsonLogic.apply(
    {"var" : "3"},
    [ "apple", "banana", "carrot" ]
);
// throws IndexOutOfBoundsException

Problem in code JsonLogicEvaluator.evaluatePartialVariable: Current

if (index < 0 || index > list.size()) {
  return null;
}

Should be

if (index < 0 || index >= list.size()) {
  return null;
}

Solution

Change in JsonLogicEvaluator: index > list.size ---> index >= list.size

jamsesso commented 3 years ago

@iliecirciumaru Version 1.0.6 was deployed with your changes. Thanks for contributing a fix!