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

#1016 PR for Filters like IN cannot process objects and arrays inside arrays #1017

Open
wants to merge 1 commit into
base: master
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 @@ -5,9 +5,13 @@
import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.internal.Path;
import com.jayway.jsonpath.internal.path.PathCompiler;
import net.minidev.json.JSONArray;
import net.minidev.json.parser.JSONParser;

import java.time.OffsetDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import static com.jayway.jsonpath.internal.filter.ValueNodes.*;
Expand Down Expand Up @@ -175,6 +179,8 @@ public static ValueNode toValueNode(Object o){
else if(o instanceof Boolean) return createBooleanNode(o.toString());
else if(o instanceof Pattern) return createPatternNode((Pattern)o);
else if (o instanceof OffsetDateTime) return createOffsetDateTimeNode(o.toString()); //workaround for issue: https://github.com/json-path/JsonPath/issues/613
else if (o instanceof Map) return new JsonNode(o);
else if (o instanceof JSONArray) return new ValueListNode(Collections.unmodifiableList((List) o));
else throw new JsonPathException("Could not determine value type");

}
Expand Down Expand Up @@ -234,5 +240,4 @@ public static ValueNode createPathNode(Path path) {
}


}

}
52 changes: 52 additions & 0 deletions json-path/src/test/java/com/jayway/jsonpath/Issue_1016.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.jayway.jsonpath;

import org.junit.jupiter.api.Test;


public class Issue_1016
{
public static final Configuration jsonConf = Configuration.defaultConfiguration();

@Test
public void test_read_with_empty_array()
{
// Before fix:
// assertEvaluationThrows("[{\"id\":1,\"array\":[\"a\",{\"b\":\"c\"}]}]", "$.*[?(\"a\" in @.array)]", JsonPathException.class);

DocumentContext emptyArray = JsonPath.using(jsonConf).parse("[{\"id\":1,\"array\":[\"a\",[]]}]");
Object read = emptyArray.read("$.*[?(\"a\" in @.array)]");
assert(read.toString().equals("[{\"id\":1,\"array\":[\"a\",[]]}]"));
}

@Test
public void test_read_with_filled_array()
{
DocumentContext filledArray = JsonPath.using(jsonConf).parse("[{\"id\":1,\"array\":[\"a\",[\"b\", \"c\"]]}]");
Object read = filledArray.read("$.*[?(\"a\" in @.array)]");
assert(read.toString().equals("[{\"id\":1,\"array\":[\"a\",[\"b\",\"c\"]]}]"));
}

@Test
public void test_read_with_empty_object()
{
DocumentContext emptyObj = JsonPath.using(jsonConf).parse("[{\"id\":1,\"array\":[\"a\",{}]}]");
Object read = emptyObj.read("$.*[?(\"a\" in @.array)]");
assert(read.toString().equals("[{\"id\":1,\"array\":[\"a\",{}]}]"));
}

@Test
public void test_read_with_filled_object()
{
DocumentContext filledObj = JsonPath.using(jsonConf).parse("[{\"id\":1,\"array\":[\"a\",{\"b\":\"c\"}]}]");
Object read = filledObj.read("$.*[?(\"a\" in @.array)]");
assert(read.toString().equals("[{\"id\":1,\"array\":[\"a\",{\"b\":\"c\"}]}]"));
}

@Test
public void test_read_with_combined_elements()
{
DocumentContext combined = JsonPath.using(jsonConf).parse("[{\"id\":1,\"array\":[\"a\",[\"b\", {\"c\" : \"d\"}]]}]");
Object read = combined.read("$.*[?(\"a\" in @.array)]");
assert(read.toString().equals("[{\"id\":1,\"array\":[\"a\",[\"b\",{\"c\":\"d\"}]]}]"));
}
}