Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug 612 and add two testcases #711

Merged
merged 3 commits into from Jun 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions json-path/src/main/java/com/jayway/jsonpath/JsonPath.java
Expand Up @@ -237,11 +237,31 @@ public <T> T map(Object jsonObject, MapFunction mapFunction, Configuration confi
notNull(jsonObject, "json can not be null");
notNull(configuration, "configuration can not be null");
notNull(mapFunction, "mapFunction can not be null");
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.convert(mapFunction, configuration);
boolean optAsPathList = configuration.containsOption(AS_PATH_LIST);
boolean optAlwaysReturnList = configuration.containsOption(Option.ALWAYS_RETURN_LIST);
boolean optSuppressExceptions = configuration.containsOption(Option.SUPPRESS_EXCEPTIONS);
try {
EvaluationContext evaluationContext = path.evaluate(jsonObject, jsonObject, configuration, true);
for (PathRef updateOperation : evaluationContext.updateOperations()) {
updateOperation.convert(mapFunction, configuration);
}
return resultByConfiguration(jsonObject, configuration, evaluationContext);
}catch (RuntimeException e) {
if (!optSuppressExceptions) {
throw e;
} else {
if (optAsPathList) {
return (T) configuration.jsonProvider().createArray();
} else {
if (optAlwaysReturnList) {
return (T) configuration.jsonProvider().createArray();
} else {
return (T) (path.isDefinite() ? null : configuration.jsonProvider().createArray());
}
}
}
}
return resultByConfiguration(jsonObject, configuration, evaluationContext);

}

/**
Expand Down
Expand Up @@ -146,8 +146,8 @@ public DocumentContext map(String path, MapFunction mapFunction, Predicate... fi

@Override
public DocumentContext map(JsonPath path, MapFunction mapFunction) {
path.map(json, mapFunction, configuration);
return this;
Object obj = path.map(json, mapFunction, configuration);
return obj==null ? null:this;
}

@Override
Expand Down
@@ -0,0 +1,37 @@
package com.jayway.jsonpath.internal.function;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.jayway.jsonpath.*;
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import com.jayway.jsonpath.spi.mapper.JacksonMappingProvider;
import org.junit.Assert;
import org.junit.Test;

import static com.jayway.jsonpath.JsonPath.using;
import static com.jayway.jsonpath.internal.path.PathCompiler.fail;

public class Issue612 {
@Test
public void test() {
Configuration config = Configuration.builder()
.options(Option.SUPPRESS_EXCEPTIONS)
.build();
String json = "{\"1\":{\"2\":null}}";
DocumentContext documentContext = JsonPath.using(config).parse(json);
JsonPath query = JsonPath.compile("$.1.2.a.b.c");
Assert.assertNull(documentContext.read(query));
Assert.assertNull(documentContext.map(query, (object, configuration) -> object));
}
@Test(expected = Exception.class)
public void test2() {
Configuration config = Configuration.builder()
.build();
String json = "{\"1\":{\"2\":null}}";
DocumentContext documentContext = JsonPath.using(config).parse(json);
JsonPath query = JsonPath.compile("$.1.2.a.b.c");
documentContext.map(query, (object, configuration) -> object);
}
}