Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix #381] Fixing null pointer #382

Open
wants to merge 2 commits into
base: develop/1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ private static JsonNode mutate(JsonNode in, final String key, final Mutation mut
final Entry<String, JsonNode> entry = iter.next();
newobj.set(entry.getKey(), entry.getValue());
}
final JsonNode newval = mutation.apply(newobj.get(key));
if (newval != null)
JsonNode newval = newobj.get(key);
newval = mutation.apply(newval != null ? newval : NullNode.getInstance());
if (newval != null && !newval.isNull())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JSON null (NullNode) is a valid value for object fields, so && !newval.isNull() is unnecessary.

$ jq -c '.foo += null' <<< '{}'
{"foo":null}

Other than that, the patch looks good 👍

Copy link
Contributor Author

@fjtirado fjtirado Nov 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I remove the isNull, a lot of test will fail (acutally it took me a while to figure out that). The reason is the same why you add the original null check, to prevent null values to be added to the object (you have some unit test to prevent that)

newobj.set(key, newval);
return newobj;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
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))), JsonQueryFunctionTest.eval(scope, ".output+=[.input[0]+1]", input));
}
}