DATAREST-1067 - EntityLookup.lookupEntity(…) now returns Optional<T>.
This commit is contained in:
@@ -208,8 +208,12 @@ class EntityLookupConfiguration implements EntityLookupRegistrar {
|
||||
* @see org.springframework.data.rest.core.support.EntityLookup#lookupEntity(java.io.Serializable)
|
||||
*/
|
||||
@Override
|
||||
public Optional<Object> lookupEntity(Object id) {
|
||||
return Optional.ofNullable(lookupInfo.getLookup().lookup(repository, id));
|
||||
@SuppressWarnings("unchecked")
|
||||
public Optional<T> lookupEntity(Object id) {
|
||||
|
||||
Object result = lookupInfo.getLookup().lookup(repository, id);
|
||||
|
||||
return Optional.class.isInstance(result) ? (Optional<T>) result : Optional.ofNullable((T) result);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -54,5 +54,5 @@ public interface EntityLookup<T> extends Plugin<Class<?>> {
|
||||
* @param id will never be {@literal null}.
|
||||
* @return can be {@literal null}.
|
||||
*/
|
||||
Optional<Object> lookupEntity(Object id);
|
||||
Optional<T> lookupEntity(Object id);
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.core.convert.converter.Converter;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
@@ -29,6 +30,7 @@ import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.support.RepositoryInvoker;
|
||||
import org.springframework.data.repository.support.RepositoryInvokerFactory;
|
||||
import org.springframework.data.rest.core.util.Java8PluginRegistry;
|
||||
import org.springframework.data.util.Optionals;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
|
||||
@@ -89,9 +91,10 @@ public class UnwrappingRepositoryInvokerFactory implements RepositoryInvokerFact
|
||||
@SuppressWarnings("unchecked")
|
||||
public <T> Optional<T> invokeFindById(Object id) {
|
||||
|
||||
return (Optional<T>) lookup//
|
||||
.map(it -> it.lookupEntity(id).orElse(Optional.empty()))//
|
||||
.orElseGet(() -> delegate.invokeFindById(id));
|
||||
Supplier<Optional<T>> viaLookup = () -> (Optional<T>) lookup.flatMap(it -> it.lookupEntity(id));
|
||||
Supplier<Optional<T>> fallback = () -> delegate.invokeFindById(id);
|
||||
|
||||
return Optionals.firstNonEmpty(viaLookup, fallback);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Reference in New Issue
Block a user