Handle Query.isSorted in QueryUtils proxy.

Closes #4758
Original pull request: #4759
This commit is contained in:
Mark Paluch
2024-08-02 15:57:04 +02:00
parent 1d5715517b
commit 369a70d00a
2 changed files with 31 additions and 0 deletions

View File

@@ -23,6 +23,7 @@ import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.bson.Document;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.data.mongodb.core.query.Collation;
import org.springframework.data.mongodb.core.query.Query;
@@ -151,6 +152,15 @@ public class QueryUtils {
@Override
public Object invoke(@NonNull MethodInvocation invocation) throws Throwable {
if (invocation.getMethod().getName().equals("isSorted")) {
if (!defaultSort.isEmpty()) {
return true;
}
return invocation.proceed();
}
if (!invocation.getMethod().getName().equals("getSortObject")) {
return invocation.proceed();
}

View File

@@ -37,12 +37,15 @@ import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.data.domain.Limit;
import org.springframework.data.domain.OffsetScrollPosition;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.ScrollPosition;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.domain.Window;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.ExecutableFindOperation.ExecutableFind;
import org.springframework.data.mongodb.core.ExecutableFindOperation.FindWithQuery;
@@ -329,6 +332,20 @@ class AbstractMongoQueryUnitTests {
assertThat(captor.getValue().getSortObject()).isEqualTo(new Document("age", 1));
}
@Test // GH-4758
void scrollUsesAnnotatedSortWhenPresent() {
createQueryForMethod("scrollByAge", Integer.class, ScrollPosition.class) //
.execute(new Object[] { 1000, ScrollPosition.keyset()});
ArgumentCaptor<Query> captor = ArgumentCaptor.forClass(Query.class);
verify(withQueryMock).matching(captor.capture());
Query query = captor.getValue();
assertThat(query.getSortObject()).isEqualTo(new Document("age", 1));
assertThat(query.isSorted()).isTrue();
}
@Test // DATAMONGO-1979
void usesExplicitSortOverridesAnnotatedSortWhenPresent() {
@@ -637,6 +654,9 @@ class AbstractMongoQueryUnitTests {
@org.springframework.data.mongodb.repository.Query(sort = "{ age : 1 }")
List<Person> findByAge(Integer age);
@org.springframework.data.mongodb.repository.Query(sort = "{ age : 1 }")
Window<Person> scrollByAge(Integer age, ScrollPosition position);
@org.springframework.data.mongodb.repository.Query(sort = "{ age : 1 }")
List<Person> findByAge(Integer age, Sort page);
@@ -670,6 +690,7 @@ class AbstractMongoQueryUnitTests {
@ReadPreference(value = "secondaryPreferred")
List<Person> findWithReadPreferenceByFirstname(String firstname);
}
// DATAMONGO-1872