Skip to content

Commit 93ba10f

Browse files
committed
Merge branch '6.2.x'
2 parents b89c48e + 4d09eb5 commit 93ba10f

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

spring-webmvc/src/test/kotlin/org/springframework/web/servlet/mvc/method/annotation/RequestResponseBodyMethodProcessorKotlinTests.kt

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2025 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,16 +19,21 @@ package org.springframework.web.servlet.mvc.method.annotation
1919
import kotlinx.serialization.Serializable
2020
import org.assertj.core.api.Assertions
2121
import org.junit.jupiter.api.Test
22+
import org.springframework.core.MethodParameter
2223
import org.springframework.http.converter.StringHttpMessageConverter
2324
import org.springframework.http.converter.json.KotlinSerializationJsonHttpMessageConverter
25+
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
26+
import org.springframework.web.bind.WebDataBinder
2427
import org.springframework.web.bind.annotation.RequestMapping
2528
import org.springframework.web.bind.annotation.ResponseBody
29+
import org.springframework.web.bind.support.WebDataBinderFactory
2630
import org.springframework.web.context.request.NativeWebRequest
2731
import org.springframework.web.context.request.ServletWebRequest
2832
import org.springframework.web.method.HandlerMethod
2933
import org.springframework.web.method.support.ModelAndViewContainer
3034
import org.springframework.web.testfixture.servlet.MockHttpServletRequest
3135
import org.springframework.web.testfixture.servlet.MockHttpServletResponse
36+
import java.nio.charset.StandardCharsets
3237
import kotlin.reflect.jvm.javaMethod
3338

3439
/**
@@ -44,6 +49,8 @@ class RequestResponseBodyMethodProcessorKotlinTests {
4449

4550
private val request: NativeWebRequest = ServletWebRequest(servletRequest, servletResponse)
4651

52+
private val factory = ValidatingBinderFactory()
53+
4754
@Test
4855
fun writeWithKotlinSerializationJsonMessageConverter() {
4956
val method = SampleController::writeMessage::javaMethod.get()!!
@@ -79,33 +86,37 @@ class RequestResponseBodyMethodProcessorKotlinTests {
7986

8087
@Test
8188
fun readWithKotlinSerializationJsonMessageConverter() {
82-
val method = SampleController::readMessage::javaMethod.get()!!
83-
val handlerMethod = HandlerMethod(SampleController(), method)
84-
val methodReturnType = handlerMethod.returnType
89+
val content = "{\"value\" : \"foo\"}"
90+
this.servletRequest.setContent(content.toByteArray(StandardCharsets.UTF_8))
91+
this.servletRequest.setContentType("application/json")
8592

8693
val converters = listOf(StringHttpMessageConverter(), KotlinSerializationJsonHttpMessageConverter())
8794
val processor = RequestResponseBodyMethodProcessor(converters, null, null)
8895

89-
val returnValue: Any = SampleController().readMessage(Message("foo"))
90-
processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request)
96+
val method = SampleController::readMessage::javaMethod.get()!!
97+
val methodParameter = MethodParameter(method, 0)
98+
99+
val result = processor.resolveArgument(methodParameter, container, request, factory) as Message
91100

92-
Assertions.assertThat(this.servletResponse.contentAsString).isEqualTo("foo")
101+
Assertions.assertThat(result).isEqualTo(Message("foo"))
93102
}
94103

104+
@Suppress("UNCHECKED_CAST")
95105
@Test
96106
fun readGenericTypeWithKotlinSerializationJsonMessageConverter() {
97-
val method = SampleController::readMessages::javaMethod.get()!!
98-
val handlerMethod = HandlerMethod(SampleController(), method)
99-
val methodReturnType = handlerMethod.returnType
107+
val content = "[{\"value\" : \"foo\"}, {\"value\" : \"bar\"}]"
108+
this.servletRequest.setContent(content.toByteArray(StandardCharsets.UTF_8))
109+
this.servletRequest.setContentType("application/json")
100110

101111
val converters = listOf(StringHttpMessageConverter(), KotlinSerializationJsonHttpMessageConverter())
102112
val processor = RequestResponseBodyMethodProcessor(converters, null, null)
103113

104-
val returnValue: Any = SampleController().readMessages(listOf(Message("foo"), Message("bar")))
105-
processor.handleReturnValue(returnValue, methodReturnType, this.container, this.request)
114+
val method = SampleController::readMessages::javaMethod.get()!!
115+
val methodParameter = MethodParameter(method, 0)
106116

107-
Assertions.assertThat(this.servletResponse.contentAsString)
108-
.isEqualTo("foo bar")
117+
val result = processor.resolveArgument(methodParameter, container, request, factory) as List<Message>
118+
119+
Assertions.assertThat(result).containsExactly(Message("foo"), Message("bar"))
109120
}
110121

111122

@@ -130,4 +141,15 @@ class RequestResponseBodyMethodProcessorKotlinTests {
130141

131142
@Serializable
132143
data class Message(val value: String)
144+
145+
private class ValidatingBinderFactory : WebDataBinderFactory {
146+
override fun createBinder(request: NativeWebRequest, target: Any?, objectName: String): WebDataBinder {
147+
val validator = LocalValidatorFactoryBean()
148+
validator.afterPropertiesSet()
149+
val dataBinder = WebDataBinder(target, objectName)
150+
dataBinder.setValidator(validator)
151+
return dataBinder
152+
}
153+
}
154+
133155
}

0 commit comments

Comments
 (0)