1
1
/*
2
- * Copyright 2025 the original author or authors.
2
+ * Copyright 2002- 2025 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -19,16 +19,21 @@ package org.springframework.web.servlet.mvc.method.annotation
19
19
import kotlinx.serialization.Serializable
20
20
import org.assertj.core.api.Assertions
21
21
import org.junit.jupiter.api.Test
22
+ import org.springframework.core.MethodParameter
22
23
import org.springframework.http.converter.StringHttpMessageConverter
23
24
import org.springframework.http.converter.json.KotlinSerializationJsonHttpMessageConverter
25
+ import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean
26
+ import org.springframework.web.bind.WebDataBinder
24
27
import org.springframework.web.bind.annotation.RequestMapping
25
28
import org.springframework.web.bind.annotation.ResponseBody
29
+ import org.springframework.web.bind.support.WebDataBinderFactory
26
30
import org.springframework.web.context.request.NativeWebRequest
27
31
import org.springframework.web.context.request.ServletWebRequest
28
32
import org.springframework.web.method.HandlerMethod
29
33
import org.springframework.web.method.support.ModelAndViewContainer
30
34
import org.springframework.web.testfixture.servlet.MockHttpServletRequest
31
35
import org.springframework.web.testfixture.servlet.MockHttpServletResponse
36
+ import java.nio.charset.StandardCharsets
32
37
import kotlin.reflect.jvm.javaMethod
33
38
34
39
/* *
@@ -44,6 +49,8 @@ class RequestResponseBodyMethodProcessorKotlinTests {
44
49
45
50
private val request: NativeWebRequest = ServletWebRequest (servletRequest, servletResponse)
46
51
52
+ private val factory = ValidatingBinderFactory ()
53
+
47
54
@Test
48
55
fun writeWithKotlinSerializationJsonMessageConverter () {
49
56
val method = SampleController ::writeMessage::javaMethod.get()!!
@@ -79,33 +86,37 @@ class RequestResponseBodyMethodProcessorKotlinTests {
79
86
80
87
@Test
81
88
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 " )
85
92
86
93
val converters = listOf (StringHttpMessageConverter (), KotlinSerializationJsonHttpMessageConverter ())
87
94
val processor = RequestResponseBodyMethodProcessor (converters, null , null )
88
95
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
91
100
92
- Assertions .assertThat(this .servletResponse.contentAsString ).isEqualTo(" foo" )
101
+ Assertions .assertThat(result ).isEqualTo(Message ( " foo" ) )
93
102
}
94
103
104
+ @Suppress(" UNCHECKED_CAST" )
95
105
@Test
96
106
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 " )
100
110
101
111
val converters = listOf (StringHttpMessageConverter (), KotlinSerializationJsonHttpMessageConverter ())
102
112
val processor = RequestResponseBodyMethodProcessor (converters, null , null )
103
113
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 )
106
116
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" ))
109
120
}
110
121
111
122
@@ -130,4 +141,15 @@ class RequestResponseBodyMethodProcessorKotlinTests {
130
141
131
142
@Serializable
132
143
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
+
133
155
}
0 commit comments