diff --git a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java index 1ec163411..9560f9e6a 100644 --- a/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java +++ b/spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/support/SimpleReactiveMongoRepository.java @@ -27,6 +27,7 @@ import java.util.List; import java.util.stream.Collectors; import org.reactivestreams.Publisher; +import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.data.domain.Example; import org.springframework.data.domain.Sort; import org.springframework.data.mongodb.core.ReactiveMongoOperations; @@ -43,6 +44,7 @@ import org.springframework.util.Assert; * * @author Mark Paluch * @author Oliver Gierke + * @author Christoph Strobl * @since 2.0 */ @RequiredArgsConstructor @@ -86,7 +88,16 @@ public class SimpleReactiveMongoRepository implement Assert.notNull(example, "Sample must not be null!"); Query q = new Query(new Criteria().alike(example)); - return mongoOperations.findOne(q, example.getProbeType(), entityInformation.getCollectionName()); + q.limit(2); + + return mongoOperations.find(q, example.getProbeType(), entityInformation.getCollectionName()).buffer(2) + .flatMap(vals -> { + + if (vals.size() > 1) { + return Mono.error(new IncorrectResultSizeDataAccessException(1)); + } + return Mono.just(vals.iterator().next()); + }).single(); } /* diff --git a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java index d30162527..660548a03 100644 --- a/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java +++ b/spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/SimpleReactiveMongoRepositoryTests.java @@ -35,6 +35,7 @@ import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.IncorrectResultSizeDataAccessException; import org.springframework.data.annotation.Id; import org.springframework.data.domain.Example; import org.springframework.data.domain.Sort; @@ -49,9 +50,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.util.ClassUtils; /** - * Test for {@link ReactiveMongoRepository}. + * Tests for {@link ReactiveMongoRepository}. * * @author Mark Paluch + * @author Christoph Strobl */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("classpath:reactive-infrastructure.xml") @@ -403,6 +405,15 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware, StepVerifier.create(repository.exists(example)).expectNext(false).verifyComplete(); } + @Test // DATAMONGO-1619 + public void findOneShouldEmitIncorrectResultSizeDataAccessExceptionWhenMoreThanOneElementFound() { + + Example example = Example.of(new ReactivePerson(null, "Matthews", -1), + matching().withIgnorePaths("age")); + + StepVerifier.create(repository.findOne(example)).expectError(IncorrectResultSizeDataAccessException.class); + } + interface ReactivePersonRepostitory extends ReactiveMongoRepository { Flux findByLastname(String lastname);