diff --git a/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/binaryop/assignment/ComplexAssignment.java b/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/binaryop/assignment/ComplexAssignment.java index 22c863bf..55205a3d 100644 --- a/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/binaryop/assignment/ComplexAssignment.java +++ b/jackson-jq/src/main/java/net/thisptr/jackson/jq/internal/tree/binaryop/assignment/ComplexAssignment.java @@ -4,6 +4,7 @@ import java.util.List; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.NullNode; import net.thisptr.jackson.jq.Expression; import net.thisptr.jackson.jq.PathOutput; @@ -38,7 +39,7 @@ public void apply(final Scope scope, final JsonNode in, final Path ipath, final }, true); JsonNode out = in; for (final Path lpath : lpaths) - out = lpath.mutate(out, (lval) -> operator.apply(scope.getObjectMapper(), lval, rval)); + out = lpath.mutate(out, (lval) -> operator.apply(scope.getObjectMapper(), lval == null ? NullNode.getInstance() : lval, rval)); output.emit(out, null); }); } diff --git a/jackson-jq/src/test/java/net/thisptr/jackson/jq/internal/NullLHSFunctionTest.java b/jackson-jq/src/test/java/net/thisptr/jackson/jq/internal/NullLHSFunctionTest.java new file mode 100644 index 00000000..14c41657 --- /dev/null +++ b/jackson-jq/src/test/java/net/thisptr/jackson/jq/internal/NullLHSFunctionTest.java @@ -0,0 +1,42 @@ +package net.thisptr.jackson.jq.internal; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import javax.management.ObjectName; + +import org.junit.jupiter.api.Test; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.IntNode; +import com.fasterxml.jackson.databind.node.NullNode; +import com.fasterxml.jackson.databind.node.ObjectNode; + +import net.thisptr.jackson.jq.BuiltinFunctionLoader; +import net.thisptr.jackson.jq.JsonQuery; +import net.thisptr.jackson.jq.Scope; +import net.thisptr.jackson.jq.Versions; +import net.thisptr.jackson.jq.exception.JsonQueryException; +import net.thisptr.jackson.jq.internal.javacc.ExpressionParser; + +public class NullLHSFunctionTest { + @Test + public void test() throws IOException { + final ObjectMapper mapper = new ObjectMapper(); + final Scope scope = Scope.newEmptyScope(); + BuiltinFunctionLoader.getInstance().loadFunctions(Versions.JQ_1_5, scope); + ObjectNode input = mapper.createObjectNode().set("input", mapper.createArrayNode().add(1)); + assertEquals(Arrays.asList(input.deepCopy().set("output", mapper.createArrayNode().add(2))), eval(scope, ".output+=[.input[0]+1]", input)); + } + + public static List eval(final Scope scope, final String q, final JsonNode in) throws JsonQueryException { + final List out = new ArrayList<>(); + JsonQuery.compile(q, Versions.JQ_1_5).apply(scope, in, out::add); + return out; + } +}