DATACMNS-460 - Repository metadata now supports array return types for query methods.
Added array checks to AbstractRepositoryMetadata.getReturnedDomainClass() to detect array component types. QueryMethod now considers methods returning arrays to be collection query methods.
This commit is contained in:
@@ -60,7 +60,9 @@ public abstract class AbstractRepositoryMetadata implements RepositoryMetadata {
|
||||
TypeInformation<?> returnTypeInfo = typeInformation.getReturnType(method);
|
||||
Class<?> rawType = returnTypeInfo.getType();
|
||||
|
||||
return Iterable.class.isAssignableFrom(rawType) ? returnTypeInfo.getComponentType().getType() : rawType;
|
||||
boolean needToUnwrap = Iterable.class.isAssignableFrom(rawType) || rawType.isArray();
|
||||
|
||||
return needToUnwrap ? returnTypeInfo.getComponentType().getType() : rawType;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2008-2013 the original author or authors.
|
||||
* Copyright 2008-2014 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.
|
||||
@@ -154,7 +154,7 @@ public class QueryMethod {
|
||||
|
||||
Class<?> returnType = method.getReturnType();
|
||||
return !(isPageQuery() || isSliceQuery())
|
||||
&& org.springframework.util.ClassUtils.isAssignable(Iterable.class, returnType);
|
||||
&& org.springframework.util.ClassUtils.isAssignable(Iterable.class, returnType) || returnType.isArray();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,6 +40,9 @@ import org.springframework.data.repository.core.RepositoryMetadata;
|
||||
*/
|
||||
public class AbstractRepositoryMetadataUnitTests {
|
||||
|
||||
/**
|
||||
* @see DATACMNS-98
|
||||
*/
|
||||
@Test
|
||||
public void discoversSimpleReturnTypeCorrectly() throws Exception {
|
||||
|
||||
@@ -58,6 +61,9 @@ public class AbstractRepositoryMetadataUnitTests {
|
||||
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-98
|
||||
*/
|
||||
@Test
|
||||
public void determinesReturnTypeFromPageable() throws Exception {
|
||||
|
||||
@@ -86,6 +92,9 @@ public class AbstractRepositoryMetadataUnitTests {
|
||||
assertThat(metadata.isPagingRepository(), is(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-98
|
||||
*/
|
||||
@Test
|
||||
public void determinesReturnTypeFromGenericType() throws Exception {
|
||||
RepositoryMetadata metadata = new DummyRepositoryMetadata(ExtendingRepository.class);
|
||||
@@ -93,6 +102,9 @@ public class AbstractRepositoryMetadataUnitTests {
|
||||
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(GenericType.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-98
|
||||
*/
|
||||
@Test
|
||||
public void handlesGenericTypeInReturnedCollectionCorrectly() throws SecurityException, NoSuchMethodException {
|
||||
|
||||
@@ -101,6 +113,18 @@ public class AbstractRepositoryMetadataUnitTests {
|
||||
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(Map.class)));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-471
|
||||
*/
|
||||
@Test
|
||||
public void detectsArrayReturnTypeCorrectly() throws Exception {
|
||||
|
||||
RepositoryMetadata metadata = new DefaultRepositoryMetadata(PagedRepository.class);
|
||||
Method method = PagedRepository.class.getMethod("returnsArray");
|
||||
|
||||
assertThat(metadata.getReturnedDomainClass(method), is(typeCompatibleWith(User.class)));
|
||||
}
|
||||
|
||||
interface UserRepository extends Repository<User, Long> {
|
||||
|
||||
User findSingle();
|
||||
@@ -126,6 +150,7 @@ public class AbstractRepositoryMetadataUnitTests {
|
||||
|
||||
interface PagedRepository extends PagingAndSortingRepository<User, Long> {
|
||||
|
||||
User[] returnsArray();
|
||||
}
|
||||
|
||||
class GenericType<T> {
|
||||
|
||||
@@ -126,6 +126,18 @@ public class QueryMethodUnitTests {
|
||||
assertThat(queryMethod.isPageQuery(), is(false));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATACMNS-471
|
||||
*/
|
||||
@Test
|
||||
public void detectsCollectionMethodForArrayRetrunType() throws Exception {
|
||||
|
||||
RepositoryMetadata repositoryMetadata = new DefaultRepositoryMetadata(SampleRepository.class);
|
||||
Method method = SampleRepository.class.getMethod("arrayOfUsers");
|
||||
|
||||
assertThat(new QueryMethod(method, repositoryMetadata).isCollectionQuery(), is(true));
|
||||
}
|
||||
|
||||
interface SampleRepository extends Repository<User, Serializable> {
|
||||
|
||||
String pagingMethodWithInvalidReturnType(Pageable pageable);
|
||||
@@ -143,6 +155,8 @@ public class QueryMethodUnitTests {
|
||||
Integer returnsProjection();
|
||||
|
||||
Slice<User> sliceOfUsers();
|
||||
|
||||
User[] arrayOfUsers();
|
||||
}
|
||||
|
||||
class User {
|
||||
|
||||
Reference in New Issue
Block a user