Browse Source

DATACMNS-1145 - @JsonPath value lookup now returns null for non-existent paths.

If a JSONPath expression is using a path not available in the backing payload we now return null rather than letting the PathNotFoundException propagate.
pull/240/head
Oliver Gierke 9 years ago
parent
commit
2bfd278861
  1. 34
      src/main/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactory.java
  2. 5
      src/test/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactoryUnitTests.java

34
src/main/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactory.java

@ -42,6 +42,7 @@ import com.jayway.jsonpath.DocumentContext; @@ -42,6 +42,7 @@ import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.ParseContext;
import com.jayway.jsonpath.PathNotFoundException;
import com.jayway.jsonpath.TypeRef;
import com.jayway.jsonpath.spi.mapper.MappingProvider;
@ -135,24 +136,31 @@ public class JsonProjectingMethodInterceptorFactory implements MethodInterceptor @@ -135,24 +136,31 @@ public class JsonProjectingMethodInterceptorFactory implements MethodInterceptor
ResolvableType type = ResolvableType.forMethodReturnType(method);
String jsonPath = getJsonPath(method);
if (returnType.getActualType().getType().isInterface()) {
try {
List<?> result = context.read(jsonPath);
return result.isEmpty() ? null : result.get(0);
}
if (returnType.getActualType().getType().isInterface()) {
boolean isCollectionResult = Collection.class.isAssignableFrom(type.getRawClass());
type = isCollectionResult ? type : ResolvableType.forClassWithGenerics(List.class, type);
type = isCollectionResult && JsonPath.isPathDefinite(jsonPath)
? ResolvableType.forClassWithGenerics(List.class, type) : type;
List<?> result = context.read(jsonPath);
return result.isEmpty() ? null : result.get(0);
}
List<?> result = (List<?>) context.read(jsonPath, new ResolvableTypeRef(type));
boolean isCollectionResult = Collection.class.isAssignableFrom(type.getRawClass());
type = isCollectionResult ? type : ResolvableType.forClassWithGenerics(List.class, type);
type = isCollectionResult && JsonPath.isPathDefinite(jsonPath)
? ResolvableType.forClassWithGenerics(List.class, type)
: type;
if (isCollectionResult && JsonPath.isPathDefinite(jsonPath)) {
result = (List<?>) result.get(0);
}
List<?> result = (List<?>) context.read(jsonPath, new ResolvableTypeRef(type));
if (isCollectionResult && JsonPath.isPathDefinite(jsonPath)) {
result = (List<?>) result.get(0);
}
return isCollectionResult ? result : result.isEmpty() ? null : result.get(0);
return isCollectionResult ? result : result.isEmpty() ? null : result.get(0);
} catch (PathNotFoundException o_O) {
return null;
}
}
/**

5
src/test/java/org/springframework/data/web/JsonProjectingMethodInterceptorFactoryUnitTests.java

@ -137,6 +137,11 @@ public class JsonProjectingMethodInterceptorFactoryUnitTests { @@ -137,6 +137,11 @@ public class JsonProjectingMethodInterceptorFactoryUnitTests {
assertThat(customer.getNestedCities(), hasSize(2));
}
@Test // DATACMNS-1144
public void returnsNullForNonExistantValue() {
assertThat(customer.getName().getLastname(), is(nullValue()));
}
interface Customer {
String getFirstname();

Loading…
Cancel
Save