DATAMONGO-1907 - Adjust SimpleReactiveMongoRepository.findOne(…) to complete without exception on empty result

We now no longer emit an exception via SimpleReactiveMongoRepository.findOne(Example) if the query completes without yielding a result. Previously findOne(Example) emitted a NoSuchElementException if the query returned no result.

Original pull request: #541.
This commit is contained in:
Ruben J Garcia
2018-03-20 20:57:07 +01:00
committed by Mark Paluch
parent 6b0b1cd97d
commit b47c5704e7
2 changed files with 11 additions and 1 deletions

View File

@@ -45,6 +45,7 @@ import org.springframework.util.Assert;
* @author Mark Paluch * @author Mark Paluch
* @author Oliver Gierke * @author Oliver Gierke
* @author Christoph Strobl * @author Christoph Strobl
* @author Ruben J Garcia
* @since 2.0 * @since 2.0
*/ */
@RequiredArgsConstructor @RequiredArgsConstructor
@@ -97,7 +98,7 @@ public class SimpleReactiveMongoRepository<T, ID extends Serializable> implement
return Mono.error(new IncorrectResultSizeDataAccessException(1)); return Mono.error(new IncorrectResultSizeDataAccessException(1));
} }
return Mono.just(vals.iterator().next()); return Mono.just(vals.iterator().next());
}).single(); }).next();
} }
/* /*

View File

@@ -54,6 +54,7 @@ import org.springframework.util.ClassUtils;
* *
* @author Mark Paluch * @author Mark Paluch
* @author Christoph Strobl * @author Christoph Strobl
* @author Ruben J Garcia
*/ */
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:reactive-infrastructure.xml") @ContextConfiguration("classpath:reactive-infrastructure.xml")
@@ -441,6 +442,14 @@ public class SimpleReactiveMongoRepositoryTests implements BeanClassLoaderAware,
StepVerifier.create(repository.findOne(example)).expectError(IncorrectResultSizeDataAccessException.class); StepVerifier.create(repository.findOne(example)).expectError(IncorrectResultSizeDataAccessException.class);
} }
@Test // DATAMONGO-1907
public void existsByExampleShouldReturnNonExistingWithoutThrowException() {
Example<ReactivePerson> example = Example.of(new ReactivePerson("foo", "bar", -1));
StepVerifier.create(repository.findOne(example)).verifyComplete();
}
interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> { interface ReactivePersonRepostitory extends ReactiveMongoRepository<ReactivePerson, String> {
Flux<ReactivePerson> findByLastname(String lastname); Flux<ReactivePerson> findByLastname(String lastname);