DATACMNS-917 - Query method execution for Maps now defaults null to an empty Map.

QueryExecutionResultHandler now explicitly handles null values for query methods returning Maps by returning an empty Map. This can be the case if a query is supposed to produce a Map (a single result) but doesn't actually yield any results at all.
This commit is contained in:
Oliver Gierke
2016-09-23 14:46:59 +02:00
parent e5008f94b0
commit 7ae5b66101
2 changed files with 23 additions and 5 deletions

View File

@@ -15,6 +15,9 @@
*/
package org.springframework.data.repository.core.support;
import java.util.Map;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
@@ -68,11 +71,15 @@ class QueryExecutionResultHandler {
return conversionService.convert(new NullableWrapper(result), expectedReturnType);
}
if (result == null) {
return null;
if (result != null) {
return conversionService.canConvert(result.getClass(), expectedReturnType)
? conversionService.convert(result, expectedReturnType) : result;
}
return conversionService.canConvert(result.getClass(), expectedReturnType) ? conversionService.convert(result,
expectedReturnType) : result;
if (Map.class.equals(expectedReturnType)) {
return CollectionFactory.createMap(expectedReturnType, 0);
}
return null;
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -21,6 +21,7 @@ import static org.junit.Assert.*;
import java.lang.reflect.Method;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
@@ -102,6 +103,14 @@ public class QueryExecutionResultHandlerUnitTests {
assertThat(optional, is(com.google.common.base.Optional.of(entity)));
}
/**
* @see DATACMNS-917
*/
@Test
public void defaultsNullToEmptyMap() throws Exception {
assertThat(handler.postProcessInvocationResult(null, getTypeDescriptorFor("map")), is(instanceOf(Map.class)));
}
private static TypeDescriptor getTypeDescriptorFor(String methodName) throws Exception {
Method method = Sample.class.getMethod(methodName);
@@ -117,6 +126,8 @@ public class QueryExecutionResultHandlerUnitTests {
Optional<Entity> jdk8Optional();
com.google.common.base.Optional<Entity> guavaOptional();
Map<Integer, Entity> map();
}
static class Entity {}