Skip to content
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
9 changes: 8 additions & 1 deletion json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import static com.jayway.jsonpath.Option.ALWAYS_RETURN_LIST;
import static com.jayway.jsonpath.Option.AS_PATH_LIST;
Expand Down Expand Up @@ -749,7 +750,13 @@ private <T> T handleMissingPathInContext(final Configuration configuration) {
boolean optAsPathList = configuration.containsOption(AS_PATH_LIST);
boolean optAlwaysReturnList = configuration.containsOption(Option.ALWAYS_RETURN_LIST);
if (optAsPathList) {
return (T) configuration.jsonProvider().createArray();
// AS_PATH_LIST results are java.util.List<String> path lists (see
// resultByConfiguration / EvaluationContextImpl.getPathList), consumed by the
// write operations of JsonContext which cast them to List. Returning
// jsonProvider.createArray() here broke that contract with document-model
// providers whose arrays are not java.util.List (e.g. Jackson's ArrayNode),
// throwing ClassCastException on non-matching paths with SUPPRESS_EXCEPTIONS.
return (T) new ArrayList<String>();
} else {
if (optAlwaysReturnList) {
return (T) configuration.jsonProvider().createArray();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,21 @@ public static final class Data {
}
}


// Write operations on a non-matching path used to throw ClassCastException with
// SUPPRESS_EXCEPTIONS: handleMissingPathInContext returned jsonProvider.createArray()
// (an ArrayNode with this provider) where JsonContext casts the AS_PATH_LIST result
// to java.util.List. Root cause of zalando/logbook#1369.
@Test
public void set_on_non_matching_path_with_suppressed_exceptions_does_not_throw() {
Configuration conf = JACKSON3_JSON_NODE_CONFIGURATION.addOptions(Option.SUPPRESS_EXCEPTIONS);
DocumentContext context = using(conf).parse(JSON_DOCUMENT);

context.set("$..does-not-exist.value", "XXX");
context.delete("$..does-not-exist");
context.renameKey("$..does-not-exist", "old", "new");

assertThat(context.read("$.string-property", String.class)).isEqualTo("string-value");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,21 @@ public static final class Data {
}
}


// Write operations on a non-matching path used to throw ClassCastException with
// SUPPRESS_EXCEPTIONS: handleMissingPathInContext returned jsonProvider.createArray()
// (an ArrayNode with this provider) where JsonContext casts the AS_PATH_LIST result
// to java.util.List. Root cause of zalando/logbook#1369.
@Test
public void set_on_non_matching_path_with_suppressed_exceptions_does_not_throw() {
Configuration conf = JACKSON_JSON_NODE_CONFIGURATION.addOptions(Option.SUPPRESS_EXCEPTIONS);
DocumentContext context = using(conf).parse(JSON_DOCUMENT);

context.set("$..does-not-exist.value", "XXX");
context.delete("$..does-not-exist");
context.renameKey("$..does-not-exist", "old", "new");

assertThat(context.read("$.string-property", String.class)).isEqualTo("string-value");
}

}