From 46998c27a536d3dcde5abaebc48dc2d45ba71a4d Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sat, 9 Apr 2016 14:09:40 +0200 Subject: [PATCH] DATACMNS-842 - Projections on repository methods support Slices now. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ResultProcessor now checks for Slice instead of Page as the projection applying method map(…) is available on Slice as well. --- .../repository/query/ResultProcessor.java | 6 +++--- .../query/ResultProcessorUnitTests.java | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/data/repository/query/ResultProcessor.java b/src/main/java/org/springframework/data/repository/query/ResultProcessor.java index 76e24fa87..492e070ad 100644 --- a/src/main/java/org/springframework/data/repository/query/ResultProcessor.java +++ b/src/main/java/org/springframework/data/repository/query/ResultProcessor.java @@ -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 { 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()) { diff --git a/src/test/java/org/springframework/data/repository/query/ResultProcessorUnitTests.java b/src/test/java/org/springframework/data/repository/query/ResultProcessorUnitTests.java index 210c9f95c..905289593 100644 --- a/src/test/java/org/springframework/data/repository/query/ResultProcessorUnitTests.java +++ b/src/test/java/org/springframework/data/repository/query/ResultProcessorUnitTests.java @@ -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 { assertThat(getProcessor("findOneDto").processResult(null), is(nullValue())); } + /** + * @see DATACMNS-842 + */ + @Test + public void supportsSlicesAsReturnWrapper() throws Exception { + + Slice slice = new SliceImpl(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 { Page findPageProjection(Pageable pageable); + Slice findSliceProjection(Pageable pageable); + T findOneDynamic(Class type); }