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

Equality Operator Seems Broken

Open luke-revenue opened this issue 1 year ago • 3 comments

Here's a test from a test suite I'm working on at work:

    @Test
    public void testJsonLogic_doubleEquals_operator() throws JsonProcessingException, JsonLogicException {

        // With graph-shaped data

        var apples = """
                { "a": { "b": "apples" } }
                """;
        var oranges = """
                { "a": { "b": "oranges"} }
                """;

        var areApples = """
                { "==": [ { "var": "a.b" }, "apples" ] }
                """;
        assertThat(JSON_LOGIC.apply(areApples, apples)).isEqualTo(FALSE /* should be TRUE */);
        assertThat(JSON_LOGIC.apply(areApples, oranges)).isEqualTo(FALSE);

        var areOranges = """
                { "==": [ { "var": "a.b" }, "oranges" ] }
                """;
        assertThat(JSON_LOGIC.apply(areOranges, apples)).isEqualTo(FALSE);
        assertThat(JSON_LOGIC.apply(areOranges, oranges)).isEqualTo(FALSE /* should be TRUE */);

        var areTheseThingsThemselves = """
                { "==": [ { "var": "a.b" }, { "var": "a.b" } ] }
                """;
        assertThat(JSON_LOGIC.apply(areTheseThingsThemselves, apples)).isEqualTo(TRUE);
        assertThat(JSON_LOGIC.apply(areTheseThingsThemselves, oranges)).isEqualTo(TRUE);

        var areTheseThingsTheSame = """
                { "==": [ { "var": "a.b" }, { "var": "c.d" } ] }
                """;
        var twoThings = """
                {
                    "a": { "b": "apples" },
                    "c": { "d": "oranges" }
                }
                """;
        var oneThing = """
                {
                    "a": { "b": "apples" },
                    "c": { "d": "apples" }
                }
                """;
        assertThat(JSON_LOGIC.apply(areTheseThingsTheSame, twoThings)).isEqualTo(TRUE /* should be FALSE */);
        assertThat(JSON_LOGIC.apply(areTheseThingsTheSame, oneThing)).isEqualTo(TRUE);

        // With primitive data

        var simpleApples = """
                { "a": "apples" }
                """;
        var simpleOranges = """
                { "a": "oranges" }
                """;

        var areSimpleApples = """
                { "==": [ { "var": "a" }, "apples" ] }
                """;
        assertThat(JSON_LOGIC.apply(areSimpleApples, simpleApples)).isEqualTo(FALSE /* should be TRUE */);
        assertThat(JSON_LOGIC.apply(areSimpleApples, simpleOranges)).isEqualTo(FALSE);
    }

Note that the test case passes, but shouldn't. Also note that the type of value passed doesn't seem to matter, either: I can replace all the strings like "apples" and "oranges" with boolean values true and false with no change in test outcome. I didn't check quite as thoroughly, but the cases I did check also allow for single values like "foo" to be replaced by list-like values (viz. [ "foo" ]) and the test outcome still doesn't change.

Maybe I'm missing something? But, my hunch is that variable expressions like { "var": "foo" } are resolving to null.

luke-revenue avatar Feb 16 '24 23:02 luke-revenue

I am also stuck due to this issue. Is there a workaround?

brusog avatar Jul 16 '24 05:07 brusog