Browse Source

Fixed issue #97. IndexOutOfBoundsException in DocumentContext.delete

pull/98/head
Kalle Stenflo 10 years ago
parent
commit
dbc7e90904
  1. 9
      json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java
  2. 26
      json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java

9
json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java

@ -149,6 +149,15 @@ public abstract class PathRef implements Comparable<PathRef> {
public Object getAccessor() { public Object getAccessor() {
return index; return index;
} }
@Override
public int compareTo(PathRef o) {
if(o instanceof ArrayIndexPathRef){
ArrayIndexPathRef pf = (ArrayIndexPathRef) o;
return Integer.compare(pf.index, this.index);
}
return super.compareTo(o);
}
} }

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

@ -10,8 +10,10 @@ import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.PathNotFoundException; import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.Predicate; import com.jayway.jsonpath.Predicate;
import com.jayway.jsonpath.internal.Utils; import com.jayway.jsonpath.internal.Utils;
import com.jayway.jsonpath.spi.json.GsonJsonProvider;
import com.jayway.jsonpath.spi.json.JacksonJsonProvider; import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.json.JsonProvider; import com.jayway.jsonpath.spi.json.JsonProvider;
import com.jayway.jsonpath.spi.mapper.GsonMappingProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider; import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import com.jayway.jsonpath.spi.mapper.MappingProvider; import com.jayway.jsonpath.spi.mapper.MappingProvider;
import net.minidev.json.JSONAware; import net.minidev.json.JSONAware;
@ -604,4 +606,28 @@ public class IssuesTest extends BaseTest {
} }
@Test
public void issue_97() throws Exception {
String json = "{ \"books\": [ " +
"{ \"category\": \"fiction\" }, " +
"{ \"category\": \"reference\" }, " +
"{ \"category\": \"fiction\" }, " +
"{ \"category\": \"fiction\" }, " +
"{ \"category\": \"reference\" }, " +
"{ \"category\": \"fiction\" }, " +
"{ \"category\": \"reference\" }, " +
"{ \"category\": \"reference\" }, " +
"{ \"category\": \"reference\" }, " +
"{ \"category\": \"reference\" }, " +
"{ \"category\": \"reference\" } ] }";
Configuration conf = Configuration.builder()
.jsonProvider(new GsonJsonProvider())
.mappingProvider(new GsonMappingProvider())
.build();
DocumentContext context = JsonPath.using(conf).parse(json);
context.delete("$.books[?(@.category == 'reference')]");
}
} }

Loading…
Cancel
Save