Skip to content

Utils.convertNulls mutates its input, so a nested evaluate in a custom function corrupts JSON nulls in the outer expression #124

Closed

Description

@aeberhart

Utils.convertNulls replaces Jsonata.NULL_VALUE with Java null in place. Because Jsonata.evaluate calls it on its result (when output null conversion is on, the default), any nested evaluate inside a custom function rewrites data that the outer expression is still using. After that, JSON nulls in the outer data are treated as undefined.

Reproduce

var expr = Jsonata.jsonata("( $t := {'p':[null, 1]}; $echo($t); $t.p.$type($) )");
expr.registerFunction("echo", (Object x) -> Jsonata.jsonata("$").evaluate(x));
expr.evaluate(null);

Expected: ["null", "number"]
Actual: "number". The nested evaluate turned $t.p into [null, 1] (Java null), so the first element is treated as undefined.

Without the $echo($t) call, the result is correct.

On 0.9.10 (before 49df9f8, "Preserve JSON null when indexing into an array") the effect is also visible with indexing. For example, $exists($t.tests[0].parameters[0]) flips from true to false after the list was passed to such a custom function.

Cause

  • Utils.convertNulls(Object) and its Map/List overloads modify the structure passed to them (Utils.java, ~L207-243).
  • Jsonata.evaluate calls Utils.convertNulls(it) on the result (Jsonata.java, ~L2728). That result can share lists and maps with the caller's input or with another expression's live data.

Suggested fix

Have convertNulls return a converted copy, or copy only the containers that contain NULL_VALUE, instead of mutating its argument.

Tests

Two failing tests that cover this:

@Test
public void testConvertNullsDoesNotModifyInput() {
    List<Object> list = new ArrayList<>(Arrays.asList(NULL_VALUE, 1));
    Map<String, Object> map = new java.util.HashMap<>(Map.of("p", list));
    Object res = Utils.convertNulls((Object) map);
    Assertions.assertEquals(Arrays.asList(null, 1), ((Map)res).get("p"));
    Assertions.assertEquals(Arrays.asList(NULL_VALUE, 1), list);
}

@Test
public void testNestedEvaluateInCustomFunctionKeepsJsonNull() {
    var expr = jsonata("( $t := {'p':[null, 1]}; $echo($t); $t.p.$type($) )");
    expr.registerFunction("echo", (Object x) -> jsonata("$").evaluate(x));
    Assertions.assertEquals(List.of("null", "number"), expr.evaluate(null));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions