diff --git a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java index 01c7db03a..ded9bf14f 100644 --- a/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java +++ b/json-path/src/main/java/com/jayway/jsonpath/JsonPath.java @@ -222,7 +222,7 @@ public T set(Object jsonObject, Object newVal, Configuration configuration) if (evaluationContext.getPathList().isEmpty()) { boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); if (optSuppressExceptions) { - return handleMissingPathInContext(configuration); + return resultByConfiguration(handleMissingPathInContext(configuration), configuration, evaluationContext); } else { throw new PathNotFoundException(); } @@ -250,7 +250,7 @@ public T map(Object jsonObject, MapFunction mapFunction, Configuration confi if (evaluationContext.getPathList().isEmpty()) { boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); if (optSuppressExceptions) { - return handleMissingPathInContext(configuration); + return resultByConfiguration(handleMissingPathInContext(configuration), configuration, evaluationContext); } else { throw new PathNotFoundException(); } @@ -277,7 +277,7 @@ public T delete(Object jsonObject, Configuration configuration) { if (evaluationContext.getPathList().isEmpty()) { boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); if (optSuppressExceptions) { - return handleMissingPathInContext(configuration); + return resultByConfiguration(handleMissingPathInContext(configuration), configuration, evaluationContext); } else { throw new PathNotFoundException(); } @@ -304,7 +304,7 @@ public T add(Object jsonObject, Object value, Configuration configuration) { if (evaluationContext.getPathList().isEmpty()) { boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); if (optSuppressExceptions) { - return handleMissingPathInContext(configuration); + return resultByConfiguration(handleMissingPathInContext(configuration), configuration, evaluationContext); } else { throw new PathNotFoundException(); } @@ -333,7 +333,7 @@ public T put(Object jsonObject, String key, Object value, Configuration conf if (evaluationContext.getPathList().isEmpty()) { boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS); if (optSuppressExceptions) { - return handleMissingPathInContext(configuration); + return resultByConfiguration(handleMissingPathInContext(configuration), configuration, evaluationContext); } else { throw new PathNotFoundException(); } diff --git a/json-path/src/test/java/com/jayway/jsonpath/old/JsonPathTest.java b/json-path/src/test/java/com/jayway/jsonpath/old/JsonPathTest.java index e42410e67..fe9e56daa 100644 --- a/json-path/src/test/java/com/jayway/jsonpath/old/JsonPathTest.java +++ b/json-path/src/test/java/com/jayway/jsonpath/old/JsonPathTest.java @@ -1,7 +1,12 @@ package com.jayway.jsonpath.old; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; import com.jayway.jsonpath.*; import com.jayway.jsonpath.internal.path.PathCompiler; +import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider; +import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider; import org.assertj.core.api.Assertions; import org.junit.jupiter.api.Test; @@ -322,4 +327,17 @@ public void prevent_stack_overflow_error_when_unclosed_property() { assertThrows(InvalidPathException.class, () -> JsonPath.compile("$['boo','foo][?(@ =~ /bar/)]")); } + @Test + //see https://github.com/json-path/JsonPath/issues/1039 + public void prevent_class_cast_exception_when_path_is_missing() throws JsonProcessingException { + ObjectMapper objectMapper = new ObjectMapper(); + ObjectNode document = (ObjectNode) objectMapper.readTree(DOCUMENT); + DocumentContext context = JsonPath.using(Configuration.builder() + .jsonProvider(new JacksonJsonNodeJsonProvider(objectMapper)) + .mappingProvider(new JacksonMappingProvider(objectMapper)) + .options(Option.SUPPRESS_EXCEPTIONS) + .build()).parse(document); + context.delete(JsonPath.compile("$.notExisting")); + } + }