Skip to content

Commit

Permalink
Throw exception if Kotlin projection requires non-null value but null…
Browse files Browse the repository at this point in the history
… result present

Fix spring-projectsGH-3242

Signed-off-by: Yanming Zhou <[email protected]>
  • Loading branch information
quaff committed Feb 14, 2025
1 parent 906d581 commit 7331706
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@
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;
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;
Expand All @@ -44,6 +48,7 @@
* @author Mark Paluch
* @author Christoph Strobl
* @author Johannes Englmeier
* @author Yanming Zhou
* @since 1.10
*/
class ProjectingMethodInterceptor implements MethodInterceptor {
Expand All @@ -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())
Expand All @@ -83,6 +90,14 @@ public Object invoke(@SuppressWarnings("null") @NonNull MethodInvocation invocat
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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
* @author Saulo Medeiros de Araujo
* @author Mark Paluch
* @author Christoph Strobl
* @author Yanming Zhou
*/
@ExtendWith(MockitoExtension.class)
class ProjectingMethodInterceptorUnitTests {
Expand Down Expand Up @@ -204,6 +205,30 @@ void returnsEnumSet() throws Throwable {
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.
*
Expand Down
25 changes: 25 additions & 0 deletions src/test/kotlin/org/springframework/data/projection/Person.kt
Original file line number Diff line number Diff line change
@@ -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?
}

0 comments on commit 7331706

Please sign in to comment.