Browse Source

DATACMNS-842 - Projections on repository methods support Slices now.

ResultProcessor now checks for Slice instead of Page as the projection applying method map(…) is available on Slice as well.
pull/172/head
Oliver Gierke 10 years ago
parent
commit
46998c27a5
  1. 6
      src/main/java/org/springframework/data/repository/query/ResultProcessor.java
  2. 21
      src/test/java/org/springframework/data/repository/query/ResultProcessorUnitTests.java

6
src/main/java/org/springframework/data/repository/query/ResultProcessor.java

@ -26,7 +26,7 @@ import java.util.Map; @@ -26,7 +26,7 @@ import java.util.Map;
import org.springframework.core.CollectionFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Slice;
import org.springframework.data.projection.ProjectionFactory;
import org.springframework.util.Assert;
@ -129,8 +129,8 @@ public class ResultProcessor { @@ -129,8 +129,8 @@ public class ResultProcessor {
ChainingConverter converter = ChainingConverter.of(type.getReturnedType(), preparingConverter).and(this.converter);
if (source instanceof Page && method.isPageQuery()) {
return (T) ((Page<?>) source).map(converter);
if (source instanceof Slice && method.isPageQuery() || method.isSliceQuery()) {
return (T) ((Slice<?>) source).map(converter);
}
if (source instanceof Collection && method.isCollectionQuery()) {

21
src/test/java/org/springframework/data/repository/query/ResultProcessorUnitTests.java

@ -32,6 +32,8 @@ import org.springframework.core.convert.converter.Converter; @@ -32,6 +32,8 @@ import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.SliceImpl;
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
import org.springframework.data.repository.Repository;
import org.springframework.data.repository.core.support.DefaultRepositoryMetadata;
@ -196,6 +198,23 @@ public class ResultProcessorUnitTests { @@ -196,6 +198,23 @@ public class ResultProcessorUnitTests {
assertThat(getProcessor("findOneDto").processResult(null), is(nullValue()));
}
/**
* @see DATACMNS-842
*/
@Test
public void supportsSlicesAsReturnWrapper() throws Exception {
Slice<Sample> slice = new SliceImpl<Sample>(Collections.singletonList(new Sample("Dave", "Matthews")));
Object result = getProcessor("findSliceProjection", Pageable.class).processResult(slice);
assertThat(result, is(instanceOf(Slice.class)));
List<?> content = ((Slice<?>) result).getContent();
assertThat(content, is(not(empty())));
assertThat(content.get(0), is(instanceOf(SampleProjection.class)));
}
private static ResultProcessor getProcessor(String methodName, Class<?>... parameters) throws Exception {
return getQueryMethod(methodName, parameters).getResultProcessor();
}
@ -225,6 +244,8 @@ public class ResultProcessorUnitTests { @@ -225,6 +244,8 @@ public class ResultProcessorUnitTests {
Page<SampleProjection> findPageProjection(Pageable pageable);
Slice<SampleProjection> findSliceProjection(Pageable pageable);
<T> T findOneDynamic(Class<T> type);
}

Loading…
Cancel
Save