diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java index f31ff02a07..980bc215c3 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java @@ -272,6 +272,9 @@ public Query withHint(Document hint) { public Query with(Pageable pageable) { if (pageable.isUnpaged()) { + if(pageable.getSort().isSorted()) { + return with(pageable.getSort()); + } return this; } diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java index f428292c6d..9a534f4307 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/query/QueryTests.java @@ -23,6 +23,7 @@ import org.junit.jupiter.api.Test; import org.springframework.aop.framework.ProxyFactory; import org.springframework.data.domain.Limit; +import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Sort; import org.springframework.data.domain.Sort.Direction; import org.springframework.data.domain.Sort.Order; @@ -366,6 +367,15 @@ void queryOfShouldWorkOnProxiedObjects() { compareQueries(target, source); } + @Test // GH-4771 + void appliesSortOfUnpagedPageable() { + + Query query = new Query(); + query.with(Pageable.unpaged(Sort.by("sortMe"))); + + assertThat(query.isSorted()).isTrue(); + } + private void compareQueries(Query actual, Query expected) { assertThat(actual.getCollation()).isEqualTo(expected.getCollation());