Skip to content

Commit d2616b7

Browse files
committed
Fix RestOperations extensions parameters nullability
Issue: SPR-16328
1 parent deac8e5 commit d2616b7

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

spring-web/src/main/kotlin/org/springframework/web/client/RestOperationsExtensions.kt

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ inline fun <reified T: Any> RestOperations.getForEntity(url: String, uriVariable
115115
* @since 5.0.2
116116
*/
117117
@Throws(RestClientException::class)
118-
inline fun <reified T: Any> RestOperations.patchForObject(url: String, request: Any, vararg uriVariables: Any): T? =
118+
inline fun <reified T: Any> RestOperations.patchForObject(url: String, request: Any? = null,
119+
vararg uriVariables: Any): T? =
119120
patchForObject(url, request, T::class.java, *uriVariables)
120121

121122
/**
@@ -128,7 +129,8 @@ inline fun <reified T: Any> RestOperations.patchForObject(url: String, request:
128129
* @since 5.0.2
129130
*/
130131
@Throws(RestClientException::class)
131-
inline fun <reified T: Any> RestOperations.patchForObject(url: String, request: Any, uriVariables: Map<String, *>): T? =
132+
inline fun <reified T: Any> RestOperations.patchForObject(url: String, request: Any? = null,
133+
uriVariables: Map<String, *>): T? =
132134
patchForObject(url, request, T::class.java, uriVariables)
133135

134136
/**
@@ -141,7 +143,7 @@ inline fun <reified T: Any> RestOperations.patchForObject(url: String, request:
141143
* @since 5.0.2
142144
*/
143145
@Throws(RestClientException::class)
144-
inline fun <reified T: Any> RestOperations.patchForObject(url: URI, request: Any): T? =
146+
inline fun <reified T: Any> RestOperations.patchForObject(url: URI, request: Any? = null): T? =
145147
patchForObject(url, request, T::class.java)
146148

147149
/**
@@ -155,7 +157,8 @@ inline fun <reified T: Any> RestOperations.patchForObject(url: URI, request: Any
155157
* @since 5.0
156158
*/
157159
@Throws(RestClientException::class)
158-
inline fun <reified T: Any> RestOperations.postForObject(url: String, request: Any, vararg uriVariables: Any): T? =
160+
inline fun <reified T: Any> RestOperations.postForObject(url: String, request: Any? = null,
161+
vararg uriVariables: Any): T? =
159162
postForObject(url, request, T::class.java, *uriVariables)
160163

161164
/**
@@ -169,7 +172,8 @@ inline fun <reified T: Any> RestOperations.postForObject(url: String, request: A
169172
* @since 5.0
170173
*/
171174
@Throws(RestClientException::class)
172-
inline fun <reified T: Any> RestOperations.postForObject(url: String, request: Any, uriVariables: Map<String, *>): T? =
175+
inline fun <reified T: Any> RestOperations.postForObject(url: String, request: Any? = null,
176+
uriVariables: Map<String, *>): T? =
173177
postForObject(url, request, T::class.java, uriVariables)
174178

175179
/**
@@ -183,7 +187,7 @@ inline fun <reified T: Any> RestOperations.postForObject(url: String, request: A
183187
* @since 5.0
184188
*/
185189
@Throws(RestClientException::class)
186-
inline fun <reified T: Any> RestOperations.postForObject(url: URI, request: Any): T? =
190+
inline fun <reified T: Any> RestOperations.postForObject(url: URI, request: Any? = null): T? =
187191
postForObject(url, request, T::class.java)
188192

189193
/**
@@ -197,7 +201,7 @@ inline fun <reified T: Any> RestOperations.postForObject(url: URI, request: Any)
197201
* @since 5.0
198202
*/
199203
@Throws(RestClientException::class)
200-
inline fun <reified T: Any> RestOperations.postForEntity(url: String, request: Any,
204+
inline fun <reified T: Any> RestOperations.postForEntity(url: String, request: Any? = null,
201205
vararg uriVariables: Any): ResponseEntity<T> =
202206
postForEntity(url, request, T::class.java, *uriVariables)
203207

@@ -212,7 +216,7 @@ inline fun <reified T: Any> RestOperations.postForEntity(url: String, request: A
212216
* @since 5.0
213217
*/
214218
@Throws(RestClientException::class)
215-
inline fun <reified T: Any> RestOperations.postForEntity(url: String, request: Any,
219+
inline fun <reified T: Any> RestOperations.postForEntity(url: String, request: Any? = null,
216220
uriVariables: Map<String, *>): ResponseEntity<T> =
217221
postForEntity(url, request, T::class.java, uriVariables)
218222

@@ -227,7 +231,7 @@ inline fun <reified T: Any> RestOperations.postForEntity(url: String, request: A
227231
* @since 5.0
228232
*/
229233
@Throws(RestClientException::class)
230-
inline fun <reified T: Any> RestOperations.postForEntity(url: URI, request: Any): ResponseEntity<T> =
234+
inline fun <reified T: Any> RestOperations.postForEntity(url: URI, request: Any? = null): ResponseEntity<T> =
231235
postForEntity(url, request, T::class.java)
232236

233237
/**
@@ -241,7 +245,7 @@ inline fun <reified T: Any> RestOperations.postForEntity(url: URI, request: Any)
241245
*/
242246
@Throws(RestClientException::class)
243247
inline fun <reified T: Any> RestOperations.exchange(url: String, method: HttpMethod,
244-
requestEntity: HttpEntity<*>, vararg uriVariables: Any): ResponseEntity<T> =
248+
requestEntity: HttpEntity<*>? = null, vararg uriVariables: Any): ResponseEntity<T> =
245249
exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}, *uriVariables)
246250

247251
/**
@@ -255,7 +259,7 @@ inline fun <reified T: Any> RestOperations.exchange(url: String, method: HttpMet
255259
*/
256260
@Throws(RestClientException::class)
257261
inline fun <reified T: Any> RestOperations.exchange(url: String, method: HttpMethod,
258-
requestEntity: HttpEntity<*>, uriVariables: Map<String, *>): ResponseEntity<T> =
262+
requestEntity: HttpEntity<*>? = null, uriVariables: Map<String, *>): ResponseEntity<T> =
259263
exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {}, uriVariables)
260264

261265
/**
@@ -269,7 +273,7 @@ inline fun <reified T: Any> RestOperations.exchange(url: String, method: HttpMet
269273
*/
270274
@Throws(RestClientException::class)
271275
inline fun <reified T: Any> RestOperations.exchange(url: URI, method: HttpMethod,
272-
requestEntity: HttpEntity<*>): ResponseEntity<T> =
276+
requestEntity: HttpEntity<*>? = null): ResponseEntity<T> =
273277
exchange(url, method, requestEntity, object : ParameterizedTypeReference<T>() {})
274278

275279
/**

spring-web/src/test/kotlin/org/springframework/web/client/RestOperationsExtensionsTests.kt

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,20 @@ class RestOperationsExtensionsTests {
112112
}
113113

114114
@Test
115-
fun `patchForObject with reified type parameters`() {
115+
fun `patchForObject with reified type parameters and String`() {
116116
val url = "https://spring.io"
117117
val body: Any = "body"
118118
template.patchForObject<Foo>(url, body)
119119
verify(template, times(1)).patchForObject(url, body, Foo::class.java)
120120
}
121121

122+
@Test
123+
fun `patchForObject with reified type parameters`() {
124+
val url = "https://spring.io"
125+
template.patchForObject<Foo>(url)
126+
verify(template, times(1)).patchForObject(url, null, Foo::class.java)
127+
}
128+
122129
@Test
123130
fun `postForObject with reified type parameters, String and varargs`() {
124131
val url = "https://spring.io"
@@ -139,13 +146,20 @@ class RestOperationsExtensionsTests {
139146
}
140147

141148
@Test
142-
fun `postForObject with reified type parameters`() {
149+
fun `postForObject with reified type parameters and String`() {
143150
val url = "https://spring.io"
144151
val body: Any = "body"
145152
template.postForObject<Foo>(url, body)
146153
verify(template, times(1)).postForObject(url, body, Foo::class.java)
147154
}
148155

156+
@Test
157+
fun `postForObject with reified type parameters`() {
158+
val url = "https://spring.io"
159+
template.postForObject<Foo>(url)
160+
verify(template, times(1)).postForObject(url, null, Foo::class.java)
161+
}
162+
149163
@Test
150164
fun `postForEntity with reified type parameters, String and varargs`() {
151165
val url = "https://spring.io"
@@ -166,13 +180,20 @@ class RestOperationsExtensionsTests {
166180
}
167181

168182
@Test
169-
fun `postForEntity with reified type parameters`() {
183+
fun `postForEntity with reified type parameters and String`() {
170184
val url = "https://spring.io"
171185
val body: Any = "body"
172186
template.postForEntity<Foo>(url, body)
173187
verify(template, times(1)).postForEntity(url, body, Foo::class.java)
174188
}
175189

190+
@Test
191+
fun `postForEntity with reified type parameters`() {
192+
val url = "https://spring.io"
193+
template.postForEntity<Foo>(url)
194+
verify(template, times(1)).postForEntity(url, null, Foo::class.java)
195+
}
196+
176197
@Test
177198
fun `exchange with reified type parameters, String, HttpMethod, HttpEntity and varargs`() {
178199
val url = "https://spring.io"
@@ -197,7 +218,7 @@ class RestOperationsExtensionsTests {
197218
}
198219

199220
@Test
200-
fun `exchange with reified type parameters, String, HttpMethod, HttpEntity`() {
221+
fun `exchange with reified type parameters, String, HttpMethod and HttpEntity`() {
201222
val url = "https://spring.io"
202223
val method = HttpMethod.GET
203224
val entity = mock<HttpEntity<Foo>>()
@@ -207,7 +228,16 @@ class RestOperationsExtensionsTests {
207228
}
208229

209230
@Test
210-
fun `exchange with reified type parameters, String, HttpEntity`() {
231+
fun `exchange with reified type parameters, String and HttpMethod`() {
232+
val url = "https://spring.io"
233+
val method = HttpMethod.GET
234+
template.exchange<List<Foo>>(url, method)
235+
verify(template, times(1)).exchange(url, method, null,
236+
object : ParameterizedTypeReference<List<Foo>>() {})
237+
}
238+
239+
@Test
240+
fun `exchange with reified type parameters, String and HttpEntity`() {
211241
val entity = mock<RequestEntity<Foo>>()
212242
template.exchange<List<Foo>>(entity)
213243
verify(template, times(1)).exchange(entity,

0 commit comments

Comments
 (0)