jamsesso / json-logic-java

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

Merge + Map with Filter Issue #28

Open samarth202 opened 2 years ago

samarth202 commented 2 years ago

Description

Seeing an issue with the java library (v1.0.7) when logic should have returned an array of arrays but returned just an array

Input

{"parentObject":{"childObject":{"array":[{"amount":500,"type":"SOME_TYPE"}]}}}

Logic

{"if":[true,{"merge":[[],{"merge":[{"map":[{"filter":[{"var":"parentObject.childObject.array"},{"===":[{"var":"type"},"SOME_TYPE"]}]},["array_result","SOME_TYPE",{"*":[{"var":"amount"},0.01]}]]}]}]},[]]}

Returns

[array_result, SOME_TYPE, 5.0]

Problem:

According to JSONLogic Playground and the js library, it should have returned an array of arrays

[
    [
        "array_result",
        "SOME_TYPE",
        5
    ]
]

Code (Kotlin)

val jsonLogic = JsonLogic()
val testInput: MutableMap<String, Any> = HashMap()
val parentObject: MutableMap<String, Any> = HashMap()
val childObject: MutableMap<String, Any> = HashMap()
val array: ArrayList<MutableMap<String, Any>> = ArrayList<MutableMap<String, Any>>()

val array_result1: MutableMap<String, Any> = HashMap()

array_result1["type"] = "SOME_TYPE"

array_result1["amount"] = 500

array.add(array_result1)

childObject["array"] = array

parentObject["childObject"] = childObject

testInput["parentObject"] = parentObject

val evalResult =
    try {
        jsonLogic.apply(logic, testInput)
    } catch (e: JsonLogicException) {
        throw Exception("Error evaluating JsonLogic: ${e.message ?: "unknown error"}", e)
    }

            // evalResult: [array_result, SOME_TYPE, 5.0]
samarth202 commented 2 years ago

It looks like for the java package, each merge needed at least an empty array [] for it to work consistently as the playground and js

{"if":[true,{"merge":[[],{"merge":[[],{"map":[{"filter":[{"var":"parentObject.childObject.array"},{"===":[{"var":"type"},"SOME_TYPE"]}]},["fee","SOME_TYPE",{"*":[{"var":"amount"},0.01]}]]}]}]},[]]}
jamsesso commented 1 year ago

The ifs in the reproduction make this really hard to read. Can you reduce the testcase down? json-logic is hard to read.