Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure exception type and message is appropriate.

}
}

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?
}