Skip to content

Commit 369a70d

Browse files
committed
Handle Query.isSorted in QueryUtils proxy.
Closes #4758 Original pull request: #4759
1 parent 1d57155 commit 369a70d

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/QueryUtils.java

+10
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.commons.logging.Log;
2424
import org.apache.commons.logging.LogFactory;
2525
import org.bson.Document;
26+
2627
import org.springframework.aop.framework.ProxyFactory;
2728
import org.springframework.data.mongodb.core.query.Collation;
2829
import org.springframework.data.mongodb.core.query.Query;
@@ -151,6 +152,15 @@ public DefaultSortingInterceptor(Document defaultSort) {
151152
@Override
152153
public Object invoke(@NonNull MethodInvocation invocation) throws Throwable {
153154

155+
if (invocation.getMethod().getName().equals("isSorted")) {
156+
157+
if (!defaultSort.isEmpty()) {
158+
return true;
159+
}
160+
161+
return invocation.proceed();
162+
}
163+
154164
if (!invocation.getMethod().getName().equals("getSortObject")) {
155165
return invocation.proceed();
156166
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/query/AbstractMongoQueryUnitTests.java

+21
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,15 @@
3737
import org.mockito.junit.jupiter.MockitoSettings;
3838
import org.mockito.quality.Strictness;
3939
import org.springframework.data.domain.Limit;
40+
import org.springframework.data.domain.OffsetScrollPosition;
4041
import org.springframework.data.domain.Page;
4142
import org.springframework.data.domain.PageRequest;
4243
import org.springframework.data.domain.Pageable;
44+
import org.springframework.data.domain.ScrollPosition;
4345
import org.springframework.data.domain.Slice;
4446
import org.springframework.data.domain.Sort;
4547
import org.springframework.data.domain.Sort.Direction;
48+
import org.springframework.data.domain.Window;
4649
import org.springframework.data.mongodb.MongoDatabaseFactory;
4750
import org.springframework.data.mongodb.core.ExecutableFindOperation.ExecutableFind;
4851
import org.springframework.data.mongodb.core.ExecutableFindOperation.FindWithQuery;
@@ -329,6 +332,20 @@ void usesAnnotatedSortWhenPresent() {
329332
assertThat(captor.getValue().getSortObject()).isEqualTo(new Document("age", 1));
330333
}
331334

335+
@Test // GH-4758
336+
void scrollUsesAnnotatedSortWhenPresent() {
337+
338+
createQueryForMethod("scrollByAge", Integer.class, ScrollPosition.class) //
339+
.execute(new Object[] { 1000, ScrollPosition.keyset()});
340+
341+
ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class);
342+
verify(withQueryMock).matching(captor.capture());
343+
344+
Query query = captor.getValue();
345+
assertThat(query.getSortObject()).isEqualTo(new Document("age", 1));
346+
assertThat(query.isSorted()).isTrue();
347+
}
348+
332349
@Test // DATAMONGO-1979
333350
void usesExplicitSortOverridesAnnotatedSortWhenPresent() {
334351

@@ -637,6 +654,9 @@ private interface Repo extends MongoRepository<Person, Long> {
637654
@org.springframework.data.mongodb.repository.Query(sort = "{ age : 1 }")
638655
List<Person> findByAge(Integer age);
639656

657+
@org.springframework.data.mongodb.repository.Query(sort = "{ age : 1 }")
658+
Window<Person> scrollByAge(Integer age, ScrollPosition position);
659+
640660
@org.springframework.data.mongodb.repository.Query(sort = "{ age : 1 }")
641661
List<Person> findByAge(Integer age, Sort page);
642662

@@ -670,6 +690,7 @@ private interface Repo extends MongoRepository<Person, Long> {
670690

671691
@ReadPreference(value = "secondaryPreferred")
672692
List<Person> findWithReadPreferenceByFirstname(String firstname);
693+
673694
}
674695

675696
// DATAMONGO-1872

0 commit comments

Comments
 (0)