diff --git a/src/main/java/org/springframework/data/repository/core/support/QueryExecutionResultHandler.java b/src/main/java/org/springframework/data/repository/core/support/QueryExecutionResultHandler.java index f9013b440..50c5c5ff4 100644 --- a/src/main/java/org/springframework/data/repository/core/support/QueryExecutionResultHandler.java +++ b/src/main/java/org/springframework/data/repository/core/support/QueryExecutionResultHandler.java @@ -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; + } } diff --git a/src/test/java/org/springframework/data/repository/core/support/QueryExecutionResultHandlerUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/QueryExecutionResultHandlerUnitTests.java index cfca5ccee..8a7da8025 100755 --- a/src/test/java/org/springframework/data/repository/core/support/QueryExecutionResultHandlerUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/QueryExecutionResultHandlerUnitTests.java @@ -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 = 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);