Throw exception if Kotlin projection requires non-null value but null result present

Closes: #3242
Original Pull Request: #3244

Signed-off-by: Yanming Zhou <zhouyanming@gmail.com>
This commit is contained in:
Yanming Zhou
2025-02-14 11:41:07 +08:00
committed by Christoph Strobl
parent 9670c8772f
commit c6d275842c
3 changed files with 66 additions and 1 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.projection;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
@@ -23,10 +24,13 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import kotlin.reflect.KFunction;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.core.CollectionFactory;
import org.springframework.core.KotlinDetector;
import org.springframework.core.convert.ConversionService;
import org.springframework.data.util.KotlinReflectionUtils;
import org.springframework.data.util.NullableWrapper;
import org.springframework.data.util.NullableWrapperConverters;
import org.springframework.data.util.TypeInformation;
@@ -44,6 +48,7 @@ import org.springframework.util.ObjectUtils;
* @author Mark Paluch
* @author Christoph Strobl
* @author Johannes Englmeier
* @author Yanming Zhou
* @since 1.10
*/
class ProjectingMethodInterceptor implements MethodInterceptor {
@@ -64,11 +69,13 @@ class ProjectingMethodInterceptor implements MethodInterceptor {
@Override
public Object invoke(@SuppressWarnings("null") @NonNull MethodInvocation invocation) throws Throwable {
TypeInformation<?> type = TypeInformation.fromReturnTypeOf(invocation.getMethod());
Method method = invocation.getMethod();
TypeInformation<?> type = TypeInformation.fromReturnTypeOf(method);
TypeInformation<?> resultType = type;
TypeInformation<?> typeToReturn = type;
Object result = delegate.invoke(invocation);
boolean applyWrapper = false;
if (NullableWrapperConverters.supports(type.getType())
@@ -83,6 +90,14 @@ class ProjectingMethodInterceptor implements MethodInterceptor {
return conversionService.convert(new NullableWrapper(result), typeToReturn.getType());
}
if (result == null) {
KFunction<?> function = KotlinDetector.isKotlinType(method.getDeclaringClass()) ?
KotlinReflectionUtils.findKotlinFunction(method) : null;
if (function != null && !function.getReturnType().isMarkedNullable()) {
throw new IllegalArgumentException("Kotlin function '%s' requires non-null return value".formatted(method.toString()));
}
}
return result;
}

View File

@@ -43,6 +43,7 @@ import org.springframework.core.convert.support.DefaultConversionService;
* @author Saulo Medeiros de Araujo
* @author Mark Paluch
* @author Christoph Strobl
* @author Yanming Zhou
*/
@ExtendWith(MockitoExtension.class)
class ProjectingMethodInterceptorUnitTests {
@@ -204,6 +205,30 @@ class ProjectingMethodInterceptorUnitTests {
assertThat(collection).containsOnly(HelperEnum.Helpful);
}
@Test
void throwExceptionIfKotlinProjectionRequiresNonNullWithNullResult() throws Throwable {
MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(), interceptor,
conversionService);
when(invocation.getMethod()).thenReturn(Person.class.getMethod("getName"));
when(interceptor.invoke(invocation)).thenReturn(null);
assertThatIllegalArgumentException().isThrownBy(() -> methodInterceptor.invoke(invocation));
}
@Test
void returnsNullIfKotlinProjectionDoesNotRequiresNonNullWithNullResult() throws Throwable {
MethodInterceptor methodInterceptor = new ProjectingMethodInterceptor(new ProxyProjectionFactory(), interceptor,
conversionService);
when(invocation.getMethod()).thenReturn(Person.class.getMethod("getAge"));
when(interceptor.invoke(invocation)).thenReturn(null);
assertThat(methodInterceptor.invoke(invocation)).isNull();
}
/**
* Mocks the {@link Helper} method of the given name to return the given value.
*

View File

@@ -0,0 +1,25 @@
/*
* Copyright 2024-2025 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.projection
/**
* @author Yanming Zhou
*/
interface Person {
val name: String
val age: Int?
}