diff --git a/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationSupport.java b/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationSupport.java index bded9bb17cb..b3212239cb7 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationSupport.java +++ b/spring-web/src/main/java/org/springframework/http/codec/KotlinSerializationSupport.java @@ -133,24 +133,25 @@ public abstract class KotlinSerializationSupport { Assert.notNull(method, "Method must not be null"); if (KotlinDetector.isKotlinType(method.getDeclaringClass())) { KFunction function = ReflectJvmMapping.getKotlinFunction(method); - Assert.notNull(function, "Kotlin function must not be null"); - KType type = (parameter.getParameterIndex() == -1 ? function.getReturnType() : - KCallables.getValueParameters(function).get(parameter.getParameterIndex()).getType()); - KSerializer serializer = this.kTypeSerializerCache.get(type); - if (serializer == null) { - try { - serializer = SerializersKt.serializerOrNull(this.format.getSerializersModule(), type); - } - catch (IllegalArgumentException ignored) { - } - if (serializer != null) { - if (hasPolymorphism(serializer.getDescriptor(), new HashSet<>())) { - return null; + if (function != null) { + KType type = (parameter.getParameterIndex() == -1 ? function.getReturnType() : + KCallables.getValueParameters(function).get(parameter.getParameterIndex()).getType()); + KSerializer serializer = this.kTypeSerializerCache.get(type); + if (serializer == null) { + try { + serializer = SerializersKt.serializerOrNull(this.format.getSerializersModule(), type); + } + catch (IllegalArgumentException ignored) { + } + if (serializer != null) { + if (hasPolymorphism(serializer.getDescriptor(), new HashSet<>())) { + return null; + } + this.kTypeSerializerCache.put(type, serializer); } - this.kTypeSerializerCache.put(type, serializer); } + return serializer; } - return serializer; } } Type type = resolvableType.getType(); diff --git a/spring-web/src/test/kotlin/org/springframework/http/codec/json/KotlinSerializationJsonEncoderTests.kt b/spring-web/src/test/kotlin/org/springframework/http/codec/json/KotlinSerializationJsonEncoderTests.kt index 86c89a0d160..d5c92147929 100644 --- a/spring-web/src/test/kotlin/org/springframework/http/codec/json/KotlinSerializationJsonEncoderTests.kt +++ b/spring-web/src/test/kotlin/org/springframework/http/codec/json/KotlinSerializationJsonEncoderTests.kt @@ -145,10 +145,24 @@ class KotlinSerializationJsonEncoderTests : AbstractEncoderTests) = map + val value: Int + get() = 42 + }