jamsesso / json-logic-java

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

NullPointerException when custom operation uses upper case letters #4

Closed pn closed 5 years ago

pn commented 5 years ago

Hi!

Thank you for this useful library! :+1:

Found one issue. When defining custom operation with upper case letters and then using it in a rule NullPointerException is thrown. When changing name to all lowercase it works ok. Here's sample kotlin code that triggers the issue:

jsonLogic.addOperation("Greet") { args -> "Hello, " + args[0] + "!" }
val result = jsonLogic.apply("""{"Greet": ["Sam"]}""", "{}") as String
assertEquals("Hello, Sam!", result)

Changing "Greet" to "greet" fixes the issue. If that is intended behavior it would be nice to get custom exception with proper message (preferably when adding such operation).

Here's the stacktrace:

java.lang.NullPointerException
    at io.github.jamsesso.jsonlogic.ast.JsonLogicParser.parse(JsonLogicParser.java:27)
    at io.github.jamsesso.jsonlogic.ast.JsonLogicParser.parse(JsonLogicParser.java:71)
    at io.github.jamsesso.jsonlogic.ast.JsonLogicParser.parse(JsonLogicParser.java:18)
    at io.github.jamsesso.jsonlogic.JsonLogic.apply(JsonLogic.java:83)
    at com.example.RulesTest.customOperationTest(RulesTest.kt:127)
    ...
jamsesso commented 5 years ago

Thanks for reporting @pn, I found a stray .toLowerCase that causes the issue. I've removed it and all other tests pass, so I'm assuming it's simply vestigial.

pn commented 5 years ago

Thank you, for quick fix! :+1: