json-logic-java icon indicating copy to clipboard operation
json-logic-java copied to clipboard

concat expression throws NPE when one of the values is null

Open hpragt opened this issue 2 years ago • 2 comments

The evaluator of the cat function applies toString() to all arguments at line 30 of io.github.jamsesso.jsonlogic.evaluator.expressions.ConcatenateExpression Object::toString

The expression below evaluates to the arguments {"E001", null} the evaluation of this expression throws a NPE. {"cat": ["E001:", {"var": "fieldA"}]}

I expect that the expression {"cat": ["E001:", null]} evaluates to "E001"

hpragt avatar Sep 06 '23 14:09 hpragt

Hi @jamsesso I got some code which solves this issue. How can I offer this code to the project ? @Override public Object evaluate(List arguments, Object data) throws JsonLogicEvaluationException { StringBuilder sb = new StringBuilder(); for (Object arg : arguments) { if (arg == null) continue; if (arg instanceof Number && ((Number) arg).intValue() == ((Number) arg).doubleValue()) { sb.append(((Number) arg).intValue()); } else { sb.append(arg); } } return sb.toString(); }

hpragt avatar Sep 07 '23 08:09 hpragt

@hpragt Thanks for the offer to contribute! You can "fork" this repository and make your changes - then open a pull request from your fork to this repository. I can review the code and merge it in.

jamsesso avatar Sep 09 '23 00:09 jamsesso