DATACMNS-1056 - Prevent QueryExecutionResultHander from rewrapping JDK 8 Optionals.
In case a repository method execution returns a JDK 8 Optional in the first place, the Optional instance had been wrapped into a third-party null-wrapper as is. We're now unwrapping the value held inside that optional and forward it to the value conversion.
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2014-2016 the original author or authors.
|
||||
* Copyright 2014-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -16,6 +16,7 @@
|
||||
package org.springframework.data.repository.core.support;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.springframework.core.CollectionFactory;
|
||||
import org.springframework.core.convert.TypeDescriptor;
|
||||
@@ -67,6 +68,8 @@ class QueryExecutionResultHandler {
|
||||
return result;
|
||||
}
|
||||
|
||||
result = unwrapOptional(result);
|
||||
|
||||
if (QueryExecutionConverters.supports(expectedReturnType)) {
|
||||
|
||||
TypeDescriptor targetType = TypeDescriptor.valueOf(expectedReturnType);
|
||||
@@ -102,4 +105,19 @@ class QueryExecutionResultHandler {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unwraps the given value if it's a JDK 8 {@link Optional}.
|
||||
*
|
||||
* @param source can be {@literal null}.
|
||||
* @return
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
private static Object unwrapOptional(Object source) {
|
||||
|
||||
if (source == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return Optional.class.isInstance(source) ? Optional.class.cast(source).orElse(null) : source;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,6 +17,7 @@ package org.springframework.data.repository.core.support;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import javaslang.control.Option;
|
||||
import reactor.core.publisher.Flux;
|
||||
import reactor.core.publisher.Mono;
|
||||
import rx.Completable;
|
||||
@@ -336,6 +337,17 @@ public class QueryExecutionResultHandlerUnitTests {
|
||||
assertThat(flux.next().block()).isEqualTo(entity.block());
|
||||
}
|
||||
|
||||
@Test // DATACMNS-1056
|
||||
public void convertsOptionalToThirdPartyOption() throws Exception {
|
||||
|
||||
Entity value = new Entity();
|
||||
Optional<Entity> entity = Optional.of(value);
|
||||
|
||||
Object result = handler.postProcessInvocationResult(entity, TypeDescriptor.valueOf(Option.class));
|
||||
|
||||
assertThat(result).isInstanceOfSatisfying(Option.class, it -> assertThat(it.get()).isEqualTo(value));
|
||||
}
|
||||
|
||||
private static TypeDescriptor getTypeDescriptorFor(String methodName) throws Exception {
|
||||
|
||||
Method method = Sample.class.getMethod(methodName);
|
||||
|
||||
Reference in New Issue
Block a user