Skip to content

Commit

Permalink
respect SUPPRESS_EXCEPTIONS configuration (#767)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardstartin authored Jan 30, 2022
1 parent b14341a commit df9cfd2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class EvaluationContextImpl implements EvaluationContext {
private final List<PathRef> updateOperations;
private final HashMap<Path, Object> documentEvalCache = new HashMap<Path, Object>();
private final boolean forUpdate;
private final boolean suppressExceptions;
private int resultIndex = 0;


Expand All @@ -61,7 +62,8 @@ public EvaluationContextImpl(Path path, Object rootDocument, Configuration confi
this.configuration = configuration;
this.valueResult = configuration.jsonProvider().createArray();
this.pathResult = configuration.jsonProvider().createArray();
this.updateOperations = new ArrayList<PathRef>();
this.updateOperations = new ArrayList<>();
this.suppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
}

public HashMap<Path, Object> documentEvalCache() {
Expand Down Expand Up @@ -129,7 +131,10 @@ public <T> T getValue() {
@Override
public <T> T getValue(boolean unwrap) {
if (path.isDefinite()) {
if(resultIndex == 0){
if(resultIndex == 0) {
if (suppressExceptions) {
return null;
}
throw new PathNotFoundException("No results for path: " + path.toString());
}
int len = jsonProvider().length(valueResult);
Expand All @@ -145,7 +150,10 @@ public <T> T getValue(boolean unwrap) {
@SuppressWarnings("unchecked")
@Override
public <T> T getPath() {
if(resultIndex == 0){
if(resultIndex == 0) {
if (suppressExceptions) {
return null;
}
throw new PathNotFoundException("No results for path: " + path.toString());
}
return (T)pathResult;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.jayway.jsonpath;

import com.jayway.jsonpath.spi.json.JacksonJsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import org.junit.Test;

import static org.junit.Assert.assertNull;


public class TestSuppressExceptions {

@Test
public void testSuppressExceptionsIsRespected() {
ParseContext parseContext = JsonPath.using(
new Configuration.ConfigurationBuilder().jsonProvider(new JacksonJsonProvider())
.mappingProvider(new JacksonMappingProvider()).options(Option.SUPPRESS_EXCEPTIONS)
.build());
String json = "{}";
assertNull(parseContext.parse(json).read(JsonPath.compile("$.missing")));
}

@Test
public void testSuppressExceptionsIsRespectedPath() {
ParseContext parseContext = JsonPath.using(
new Configuration.ConfigurationBuilder().jsonProvider(new JacksonJsonProvider())
.mappingProvider(new JacksonMappingProvider()).options(Option.SUPPRESS_EXCEPTIONS, Option.AS_PATH_LIST)
.build());
String json = "{}";
assertNull(parseContext.parse(json).read(JsonPath.compile("$.missing")));
}
}

0 comments on commit df9cfd2

Please sign in to comment.