Skip to content

Commit 5885d08

Browse files
DATAMONGO-1619 - Polishing.
Align to changes in DATACMNS-995 and emit Exception if findOne yields more than one result. Original Pull Request: spring-projects#444
1 parent d8fdc18 commit 5885d08

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java

+12-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.stream.Collectors;
2828

2929
import org.reactivestreams.Publisher;
30+
import org.springframework.dao.IncorrectResultSizeDataAccessException;
3031
import org.springframework.data.domain.Example;
3132
import org.springframework.data.domain.Sort;
3233
import org.springframework.data.mongodb.core.ReactiveMongoOperations;
@@ -43,6 +44,7 @@
4344
*
4445
* @author Mark Paluch
4546
* @author Oliver Gierke
47+
* @author Christoph Strobl
4648
* @since 2.0
4749
*/
4850
@RequiredArgsConstructor
@@ -86,7 +88,16 @@ public <S extends T> Mono<S> findOne(Example<S> example) {
8688
Assert.notNull(example, "Sample must not be null!");
8789

8890
Query q = new Query(new Criteria().alike(example));
89-
return mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName());
91+
q.limit(2);
92+
93+
return mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName()).buffer(2)
94+
.flatMap(vals -> {
95+
96+
if (vals.size() > 1) {
97+
return Mono.error(new IncorrectResultSizeDataAccessException(1));
98+
}
99+
return Mono.just(vals.iterator().next());
100+
}).single();
90101
}
91102

92103
/*

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.springframework.beans.factory.BeanFactory;
3636
import org.springframework.beans.factory.BeanFactoryAware;
3737
import org.springframework.beans.factory.annotation.Autowired;
38+
import org.springframework.dao.IncorrectResultSizeDataAccessException;
3839
import org.springframework.data.annotation.Id;
3940
import org.springframework.data.domain.Example;
4041
import org.springframework.data.domain.Sort;
@@ -49,9 +50,10 @@
4950
import org.springframework.util.ClassUtils;
5051

5152
/**
52-
* Test for {@link ReactiveMongoRepository}.
53+
* Tests for {@link ReactiveMongoRepository}.
5354
*
5455
* @author Mark Paluch
56+
* @author Christoph Strobl
5557
*/
5658
@RunWith(SpringJUnit4ClassRunner.class)
5759
@ContextConfiguration("classpath:reactive-infrastructure.xml")
@@ -403,6 +405,15 @@ public void existsByExampleShouldReturnNonExisting() {
403405
StepVerifier.create(repository.exists(example)).expectNext(false).verifyComplete();
404406
}
405407

408+
@Test // DATAMONGO-1619
409+
public void findOneShouldEmitIncorrectResultSizeDataAccessExceptionWhenMoreThanOneElementFound() {
410+
411+
Example<ReactivePerson> example = Example.of(new ReactivePerson(null, "Matthews", -1),
412+
matching().withIgnorePaths("age"));
413+
414+
StepVerifier.create(repository.findOne(example)).expectError(IncorrectResultSizeDataAccessException.class);
415+
}
416+
406417
interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> {
407418

408419
Flux<ReactivePerson> findByLastname(String lastname);

0 commit comments

Comments
 (0)