Skip to content

Handle Query.isSorted in QueryUtils proxy #4759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.3.3-SNAPSHOT</version>
<version>4.3.x-GH-4758-SNAPSHOT</version>
<packaging>pom</packaging>

<name>Spring Data MongoDB</name>
Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-benchmarks/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.3.3-SNAPSHOT</version>
<version>4.3.x-GH-4758-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.3.3-SNAPSHOT</version>
<version>4.3.x-GH-4758-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
2 changes: 1 addition & 1 deletion spring-data-mongodb/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<parent>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb-parent</artifactId>
<version>4.3.3-SNAPSHOT</version>
<version>4.3.x-GH-4758-SNAPSHOT</version>
<relativePath>../pom.xml</relativePath>
</parent>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
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;
Expand Down Expand Up @@ -116,13 +117,13 @@ static int indexOfAssignableParameter(Class<?> type, Class<?>[] parameters) {
*/
static int indexOfAssignableParameter(Class<?> type, List<Class<?>> parameters) {

if(parameters.isEmpty()) {
if (parameters.isEmpty()) {
return -1;
}

int i = 0;
for(Class<?> parameterType : parameters) {
if(ClassUtils.isAssignable(type, parameterType)) {
for (Class<?> parameterType : parameters) {
if (ClassUtils.isAssignable(type, parameterType)) {
return i;
}
i++;
Expand Down Expand Up @@ -151,6 +152,12 @@ public DefaultSortingInterceptor(Document defaultSort) {
@Override
public Object invoke(@NonNull MethodInvocation invocation) throws Throwable {

if (invocation.getMethod().getName().equals("isSorted")) {

boolean result = (Boolean) invocation.proceed();
return result || !defaultSort.isEmpty();
}

if (!invocation.getMethod().getName().equals("getSortObject")) {
return invocation.proceed();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@
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;
Expand Down Expand Up @@ -329,6 +332,20 @@ void usesAnnotatedSortWhenPresent() {
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() {

Expand Down Expand Up @@ -637,6 +654,9 @@ private interface Repo extends MongoRepository<Person, Long> {
@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);

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

@ReadPreference(value = "secondaryPreferred")
List<Person> findWithReadPreferenceByFirstname(String firstname);

}

// DATAMONGO-1872
Expand Down
Loading