Support nullable Kotlin value class arguments

This commit refines WebMVC and WebFlux argument resolution in order to
convert properly Kotlin value class arguments by using the type of the
value instead of the type of the value class.

Closes gh-32353
This commit is contained in:
Sébastien Deleuze
2024-03-01 14:44:37 +01:00
parent de828e9764
commit 227e75dae4
4 changed files with 78 additions and 16 deletions

View File

@@ -26,6 +26,7 @@ import kotlin.reflect.KFunction;
import kotlin.reflect.KParameter;
import kotlin.reflect.jvm.ReflectJvmMapping;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.beans.factory.config.BeanExpressionContext;
@@ -281,8 +282,12 @@ public abstract class AbstractNamedValueMethodArgumentResolver implements Handle
NamedValueInfo namedValueInfo, @Nullable Object arg) throws Exception {
WebDataBinder binder = binderFactory.createBinder(webRequest, null, namedValueInfo.name);
Class<?> parameterType = parameter.getParameterType();
if (KotlinDetector.isKotlinPresent() && KotlinDetector.isInlineClass(parameterType)) {
parameterType = BeanUtils.findPrimaryConstructor(parameterType).getParameterTypes()[0];
}
try {
arg = binder.convertIfNecessary(arg, parameter.getParameterType(), parameter);
arg = binder.convertIfNecessary(arg, parameterType, parameter);
}
catch (ConversionNotSupportedException ex) {
throw new MethodArgumentConversionNotSupportedException(arg, ex.getRequiredType(),