Add Kotlin body advices

This commit introduces KotlinRequestBodyAdvice and
KotlinResponseBodyAdvice in order to set a KType hint when relevant.

Closes gh-34923
This commit is contained in:
Sébastien Deleuze
2025-06-05 18:50:25 +02:00
parent 9f7a321c44
commit 826041d2f7
7 changed files with 252 additions and 49 deletions

View File

@@ -33,8 +33,10 @@ import org.springframework.web.testfixture.http.MockHttpOutputMessage
import java.lang.reflect.ParameterizedType
import java.math.BigDecimal
import java.nio.charset.StandardCharsets
import kotlin.reflect.KType
import kotlin.reflect.javaType
import kotlin.reflect.jvm.javaMethod
import kotlin.reflect.jvm.jvmName
import kotlin.reflect.typeOf
/**
@@ -246,7 +248,10 @@ class KotlinSerializationJsonHttpMessageConverterTests {
val inputMessage = MockHttpInputMessage(body.toByteArray(StandardCharsets.UTF_8))
inputMessage.headers.contentType = MediaType.APPLICATION_JSON
val methodParameter = MethodParameter.forExecutable(::handleMapWithNullable::javaMethod.get()!!, 0)
val result = converter.read(ResolvableType.forMethodParameter(methodParameter), inputMessage, null) as Map<String, String?>
val hints = mapOf(KType::class.jvmName to typeOf<Map<String, String?>>())
val result = converter.read(ResolvableType.forMethodParameter(methodParameter), inputMessage,
hints) as Map<String, String?>
assertThat(result).containsExactlyEntriesOf(mapOf("value" to null))
}
@@ -400,9 +405,10 @@ class KotlinSerializationJsonHttpMessageConverterTests {
val serializableBean = mapOf<String, String?>("value" to null)
val expectedJson = """{"value":null}"""
val methodParameter = MethodParameter.forExecutable(::handleMapWithNullable::javaMethod.get()!!, -1)
val hints = mapOf(KType::class.jvmName to typeOf<Map<String, String?>>())
this.converter.write(serializableBean, ResolvableType.forMethodParameter(methodParameter), null,
outputMessage, null)
outputMessage, hints)
val result = outputMessage.getBodyAsString(StandardCharsets.UTF_8)