Skip to content

Commit

Permalink
[Fix #381] Fixing null pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Nov 26, 2024
1 parent b7954b4 commit 6184705
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<JsonNode> eval(final Scope scope, final String q, final JsonNode in) throws JsonQueryException {
final List<JsonNode> out = new ArrayList<>();
JsonQuery.compile(q, Versions.JQ_1_5).apply(scope, in, out::add);
return out;
}
}

0 comments on commit 6184705

Please sign in to comment.