DATACMNS-867 - RepositoryInvoker.invokeQueryMethod(…) now returns an Optional.
This commit is contained in:
@@ -135,8 +135,8 @@ public class QuerydslRepositoryInvokerAdapter implements RepositoryInvoker {
|
||||
* @see org.springframework.data.repository.support.RepositoryInvoker#invokeQueryMethod(java.lang.reflect.Method, org.springframework.util.MultiValueMap, org.springframework.data.domain.Pageable, org.springframework.data.domain.Sort)
|
||||
*/
|
||||
@Override
|
||||
public Object invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters, Pageable pageable,
|
||||
Sort sort) {
|
||||
public Optional<Object> invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters,
|
||||
Pageable pageable, Sort sort) {
|
||||
return delegate.invokeQueryMethod(method, parameters, pageable, sort);
|
||||
}
|
||||
|
||||
|
||||
@@ -136,23 +136,12 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeFindOne(java.io.Serializable)
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Optional<T> invokeFindOne(Serializable id) {
|
||||
|
||||
Method method = methods.getFindOneMethod()//
|
||||
.orElseThrow(() -> new IllegalStateException("Repository doesn't have a find-one-method declared!"));
|
||||
|
||||
Object invoke = invoke(method, convertId(id));
|
||||
|
||||
if (Optional.class.isInstance(invoke)) {
|
||||
return (Optional<T>) invoke;
|
||||
}
|
||||
|
||||
if (invoke == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return conversionService.convert(QueryExecutionConverters.unwrap(invoke), Optional.class);
|
||||
return returnAsOptional(invoke(method, convertId(id)));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -190,8 +179,8 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
* @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeQueryMethod(java.lang.reflect.Method, java.util.Map, org.springframework.data.domain.Pageable, org.springframework.data.domain.Sort)
|
||||
*/
|
||||
@Override
|
||||
public Object invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters, Pageable pageable,
|
||||
Sort sort) {
|
||||
public Optional<Object> invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters,
|
||||
Pageable pageable, Sort sort) {
|
||||
|
||||
Assert.notNull(method, "Method must not be null!");
|
||||
Assert.notNull(parameters, "Parameters must not be null!");
|
||||
@@ -200,7 +189,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
|
||||
ReflectionUtils.makeAccessible(method);
|
||||
|
||||
return invoke(method, prepareParameters(method, parameters, pageable, sort));
|
||||
return returnAsOptional(invoke(method, prepareParameters(method, parameters, pageable, sort)));
|
||||
}
|
||||
|
||||
private Object[] prepareParameters(Method method, MultiValueMap<String, ? extends Object> rawParameters,
|
||||
@@ -262,6 +251,20 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker {
|
||||
return (T) ReflectionUtils.invokeMethod(method, repository, arguments);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> Optional<T> returnAsOptional(Object source) {
|
||||
|
||||
if (Optional.class.isInstance(source)) {
|
||||
return (Optional<T>) source;
|
||||
}
|
||||
|
||||
if (source == null) {
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
return conversionService.convert(QueryExecutionConverters.unwrap(source), Optional.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given id into the id type of the backing repository.
|
||||
*
|
||||
|
||||
@@ -99,6 +99,6 @@ public interface RepositoryInvoker extends RepositoryInvocationInformation {
|
||||
* @return the result of the invoked query method.
|
||||
* @since 1.11
|
||||
*/
|
||||
Object invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters, Pageable pageable,
|
||||
Sort sort);
|
||||
Optional<Object> invokeQueryMethod(Method method, MultiValueMap<String, ? extends Object> parameters,
|
||||
Pageable pageable, Sort sort);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user