Browse Source

fix 656

pull/694/head
CindyChow123 4 years ago
parent
commit
fd3963228a
  1. 19
      json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java
  2. 82
      json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

19
json-path/src/main/java/com/jayway/jsonpath/internal/JsonContext.java

@ -26,11 +26,16 @@ import com.jayway.jsonpath.TypeRef;
import com.jayway.jsonpath.spi.cache.Cache;
import com.jayway.jsonpath.spi.cache.CacheProvider;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Collection;
import java.util.HashSet;
import static com.jayway.jsonpath.JsonPath.compile;
import static com.jayway.jsonpath.internal.Utils.notEmpty;
@ -48,6 +53,20 @@ public class JsonContext implements DocumentContext {
notNull(json, "json can not be null");
notNull(configuration, "configuration can not be null");
this.configuration = configuration;
//CS304 Issue link: https://github.com/json-path/JsonPath/issues/656
Set<Object> set = new HashSet<>();
Collection<String> properties = configuration.jsonProvider().getPropertyKeys(json);
for (String property : properties) {
Object propertyModel = configuration.jsonProvider().getMapValue(json,property);
if(!set.contains(propertyModel)){
set.add(propertyModel);
}else{
ObjectNode obj_cast = (ObjectNode) propertyModel;
ObjectNode newobj = obj_cast.deepCopy();
ObjectNode objects_cast = (ObjectNode) json;
objects_cast.replace(property,newobj);
}
}
this.json = json;
}

82
json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

@ -1,6 +1,7 @@
package com.jayway.jsonpath.old;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.google.gson.JsonObject;
import com.jayway.jsonpath.BaseTest;
import com.jayway.jsonpath.Configuration;
@ -1116,4 +1117,85 @@ public class IssuesTest extends BaseTest {
assertTrue(thrown);
}
//CS304 (manually written) Issue link: https://github.com/json-path/JsonPath/issues/656
@Test
public void issue_656_1(){
String json = "{\n" +
"\"jsonArr\": [\n" +
" {\n" +
" \"name\":\"nOne\"\n" +
" },\n" +
" {\n" +
" \"name\":\"nTwo\"\n" +
" },\n" +
" {\n" +
" \"name\":\"nThree\"\n" +
" }\n" +
" ]\n" +
"}";
Configuration configuration = Configuration.builder()
.jsonProvider(new JacksonJsonNodeJsonProvider())
.mappingProvider(new JacksonMappingProvider())
.build();
ObjectNode obj = (ObjectNode) configuration.jsonProvider().parse(json);
ObjectNode newObj = (ObjectNode) configuration.jsonProvider().createMap();
newObj.set("one", obj);
newObj.set("two", obj);
DocumentContext ctx = JsonPath.parse(newObj, configuration);
String path = "$..jsonArr[?(@.name == 'nOne')]";
JsonPath jsonPath = JsonPath.compile(path);
ctx.delete(jsonPath);
assertEquals("nTwo", ((TextNode)ctx.read("$.one.jsonArr[0].name")).asText());
assertEquals("nThree", ((TextNode)ctx.read("$.one.jsonArr[1].name")).asText());
assertEquals("nTwo", ((TextNode)ctx.read("$.two.jsonArr[0].name")).asText());
assertEquals("nThree", ((TextNode)ctx.read("$.two.jsonArr[1].name")).asText());
}
//CS304 (manually written) Issue link: https://github.com/json-path/JsonPath/issues/656
@Test
public void issue_656_2(){
String json = "{\n" +
"\"jsonArr\": [\n" +
" {\n" +
" \"name\":\"nOne\"\n" +
" },\n" +
" {\n" +
" \"name\":\"nTwo\"\n" +
" },\n" +
" {\n" +
" \"name\":\"nThree\"\n" +
" }\n" +
" ]\n" +
"}";
Configuration configuration = Configuration.builder()
.jsonProvider(new JacksonJsonNodeJsonProvider())
.mappingProvider(new JacksonMappingProvider())
.build();
ObjectNode obj = (ObjectNode) configuration.jsonProvider().parse(json);
ObjectNode obj1 = (ObjectNode) configuration.jsonProvider().parse(json);
ObjectNode newObj = (ObjectNode) configuration.jsonProvider().createMap();
newObj.set("one", obj);
newObj.set("three", obj);
newObj.set("two", obj1);
newObj.set("four", obj1);
newObj.set("five",obj1);
DocumentContext ctx = JsonPath.parse(newObj, configuration);
String path = "$..jsonArr[?(@.name == 'nOne')]";
JsonPath jsonPath = JsonPath.compile(path);
ctx.delete(jsonPath);
assertEquals("nTwo", ((TextNode)ctx.read("$.one.jsonArr[0].name")).asText());
assertEquals("nTwo", ((TextNode)ctx.read("$.two.jsonArr[0].name")).asText());
assertEquals("nTwo", ((TextNode)ctx.read("$.three.jsonArr[0].name")).asText());
assertEquals("nTwo", ((TextNode)ctx.read("$.four.jsonArr[0].name")).asText());
assertEquals("nTwo", ((TextNode)ctx.read("$.five.jsonArr[0].name")).asText());
}
}

Loading…
Cancel
Save