Skip to content

Commit b99833d

Browse files
committed
DATAMONGO-1080 - AbstractMongoQuery now refrains from eagerly post-processing the query execution results.
To properly support general post processing of query execution results (in QueryExecutorMethodInterceptor) we need to remove the eager post-processing of query execution results in AbstractMongoQuery. Removed the usage of the local ConversionService all together.
1 parent 4be6231 commit b99833d

File tree

2 files changed

+26
-21
lines changed

2 files changed

+26
-21
lines changed

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

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -87,39 +87,25 @@ public Object execute(Object[] parameters) {
8787

8888
applyQueryMetaAttributesWhenPresent(query);
8989

90-
Object result = null;
91-
9290
if (isDeleteQuery()) {
93-
result = new DeleteExecution().execute(query);
91+
return new DeleteExecution().execute(query);
9492
} else if (method.isGeoNearQuery() && method.isPageQuery()) {
9593

9694
MongoParameterAccessor countAccessor = new MongoParametersParameterAccessor(method, parameters);
9795
Query countQuery = createCountQuery(new ConvertingParameterAccessor(operations.getConverter(), countAccessor));
9896

99-
result = new GeoNearExecution(accessor).execute(query, countQuery);
97+
return new GeoNearExecution(accessor).execute(query, countQuery);
10098
} else if (method.isGeoNearQuery()) {
101-
result = new GeoNearExecution(accessor).execute(query);
99+
return new GeoNearExecution(accessor).execute(query);
102100
} else if (method.isSliceQuery()) {
103-
result = new SlicedExecution(accessor.getPageable()).execute(query);
101+
return new SlicedExecution(accessor.getPageable()).execute(query);
104102
} else if (method.isCollectionQuery()) {
105-
result = new CollectionExecution(accessor.getPageable()).execute(query);
103+
return new CollectionExecution(accessor.getPageable()).execute(query);
106104
} else if (method.isPageQuery()) {
107-
result = new PagedExecution(accessor.getPageable()).execute(query);
105+
return new PagedExecution(accessor.getPageable()).execute(query);
108106
} else {
109-
result = new SingleEntityExecution(isCountQuery()).execute(query);
110-
}
111-
112-
if (result == null) {
113-
return result;
107+
return new SingleEntityExecution(isCountQuery()).execute(query);
114108
}
115-
116-
Class<?> expectedReturnType = method.getReturnType().getType();
117-
118-
if (expectedReturnType.isAssignableFrom(result.getClass())) {
119-
return result;
120-
}
121-
122-
return CONVERSION_SERVICE.convert(result, expectedReturnType);
123109
}
124110

125111
private Query applyQueryMetaAttributesWhenPresent(Query query) {

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
import static org.hamcrest.CoreMatchers.*;
1919
import static org.junit.Assert.*;
20+
import static org.mockito.Matchers.*;
2021
import static org.mockito.Mockito.*;
2122

2223
import java.lang.reflect.Method;
2324
import java.util.Arrays;
2425
import java.util.Date;
2526
import java.util.List;
27+
import java.util.Optional;
2628

2729
import org.bson.types.ObjectId;
2830
import org.hamcrest.core.Is;
@@ -32,6 +34,7 @@
3234
import org.mockito.ArgumentCaptor;
3335
import org.mockito.Matchers;
3436
import org.mockito.Mock;
37+
import org.mockito.Mockito;
3538
import org.mockito.runners.MockitoJUnitRunner;
3639
import org.springframework.data.domain.Page;
3740
import org.springframework.data.domain.PageRequest;
@@ -282,6 +285,21 @@ public void slicedExecutionShouldRetainSort() {
282285
assertThat(captor.getAllValues().get(1).getSortObject(), is(expectedSortObject));
283286
}
284287

288+
/**
289+
* @see DATAMONGO-1080
290+
*/
291+
@Test
292+
public void doesNotTryToPostProcessQueryResultIntoWrapperType() {
293+
294+
Person reference = new Person();
295+
when(mongoOperationsMock.findOne(Mockito.any(Query.class), eq(Person.class), eq("persons"))).//
296+
thenReturn(reference);
297+
298+
AbstractMongoQuery query = createQueryForMethod("findByLastname", String.class);
299+
300+
assertThat(query.execute(new Object[] { "lastname" }), is((Object) reference));
301+
}
302+
285303
private MongoQueryFake createQueryForMethod(String methodName, Class<?>... paramTypes) {
286304

287305
try {
@@ -346,5 +364,6 @@ private interface Repo extends MongoRepository<Person, Long> {
346364
/** @see DATAMONGO-1057 */
347365
Slice<Person> findByLastname(String lastname, Pageable page);
348366

367+
Optional<Person> findByLastname(String lastname);
349368
}
350369
}

0 commit comments

Comments
 (0)