From dbc7e90904e63f8b7ad818442efc11ac9d1a8d00 Mon Sep 17 00:00:00 2001 From: Kalle Stenflo Date: Tue, 9 Jun 2015 23:15:14 +0200 Subject: [PATCH] Fixed issue #97. IndexOutOfBoundsException in DocumentContext.delete --- .../com/jayway/jsonpath/internal/PathRef.java | 9 +++++++ .../com/jayway/jsonpath/old/IssuesTest.java | 26 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java b/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java index e8be7057..c7fff03a 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java +++ b/json-path/src/main/java/com/jayway/jsonpath/internal/PathRef.java @@ -149,6 +149,15 @@ public abstract class PathRef implements Comparable { public Object getAccessor() { 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); + } } diff --git a/json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java b/json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java index 75962d0f..d798a81a 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/old/IssuesTest.java +++ b/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.Predicate; 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.JsonProvider; +import com.jayway.jsonpath.spi.mapper.GsonMappingProvider; import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider; import com.jayway.jsonpath.spi.mapper.MappingProvider; 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')]"); + } }