DATAMONGO-2374 - Fix simple type result handling for repository query methods.

Original pull request: #791.
This commit is contained in:
Christoph Strobl
2019-09-19 08:58:48 +02:00
committed by Mark Paluch
parent 26dc0c5c27
commit 9ebe1439f1
5 changed files with 33 additions and 2 deletions

View File

@@ -66,8 +66,12 @@ class PropertyOperations {
projectionInformation.getInputProperties().forEach(it -> projectedFields.append(it.getName(), 1));
}
} else {
mappingContext.getRequiredPersistentEntity(targetType).doWithProperties(
(SimplePropertyHandler) persistentProperty -> projectedFields.append(persistentProperty.getName(), 1));
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(targetType);
if(entity != null) {
entity.doWithProperties(
(SimplePropertyHandler) persistentProperty -> projectedFields.append(persistentProperty.getName(), 1));
}
}
return projectedFields;

View File

@@ -174,6 +174,10 @@ interface ReactiveMongoQueryExecution {
return source;
}
if(!operations.getConverter().getMappingContext().hasPersistentEntityFor(returnedType.getReturnedType())) {
return source;
}
Converter<Object, Object> converter = new DtoInstantiatingConverter(returnedType.getReturnedType(),
operations.getConverter().getMappingContext(), instantiators);

View File

@@ -1318,4 +1318,11 @@ public abstract class AbstractPersonRepositoryIntegrationTests {
.isInstanceOf(AggregationResults.class) //
.containsExactly(new SumAge(245L));
}
@Test // DATAMONGO-2374
public void findsWithNativeProjection() {
assertThat(repository.findDocumentById(dave.getId()).get()).containsEntry("firstname", dave.getFirstname())
.containsEntry("lastname", dave.getLastname());
}
}

View File

@@ -388,4 +388,7 @@ public interface PersonRepository extends MongoRepository<Person, String>, Query
@Aggregation(pipeline = "{ '$group' : { '_id' : null, 'total' : { $sum: '$age' } } }")
AggregationResults<SumAge> sumAgeAndReturnAggregationResultWrapperWithConcreteType();
@Query(value="{_id:?0}")
Optional<org.bson.Document> findDocumentById(String id);
}

View File

@@ -527,6 +527,16 @@ public class ReactiveMongoRepositoryTests {
.verifyComplete();
}
@Test // DATAMONGO-2374
public void findsWithNativeProjection() {
repository.findDocumentById(dave.getId()) //
.as(StepVerifier::create) //
.consumeNextWith(it -> {
assertThat(it).containsEntry("firstname", dave.getFirstname()).containsEntry("lastname", dave.getLastname());
}).verifyComplete();
}
interface ReactivePersonRepository
extends ReactiveMongoRepository<Person, String>, ReactiveQuerydslPredicateExecutor<Person> {
@@ -587,6 +597,9 @@ public class ReactiveMongoRepositoryTests {
@Aggregation(pipeline = "{ '$group' : { '_id' : null, 'total' : { $sum: '$age' } } }")
Mono<SumAge> sumAgeAndReturnSumWrapper();
@Query(value = "{_id:?0}")
Mono<org.bson.Document> findDocumentById(String id);
}
interface ReactiveContactRepository extends ReactiveMongoRepository<Contact, String> {}