Skip to content

Commit 0ca8428

Browse files
committed
Fix lambda nullability in JdbcOperations extensions
Closes gh-22682
1 parent fd6b64b commit 0ca8428

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors
2+
* Copyright 2002-2019 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.
@@ -34,7 +34,7 @@ inline fun <reified T : Any> JdbcOperations.queryForObject(sql: String): T? =
3434
* @author Mario Arias
3535
* @since 5.0
3636
*/
37-
fun <T : Any> JdbcOperations.queryForObject(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): T? =
37+
fun <T : Any?> JdbcOperations.queryForObject(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): T? =
3838
queryForObject(sql, RowMapper { resultSet, i -> function(resultSet, i) }, *args)
3939

4040
/**
@@ -96,7 +96,7 @@ inline fun <reified T : Any> JdbcOperations.queryForList(sql: String, args: Arra
9696
* @author Mario Arias
9797
* @since 5.0
9898
*/
99-
inline fun <reified T : Any> JdbcOperations.query(sql: String, vararg args: Any,
99+
inline fun <reified T : Any?> JdbcOperations.query(sql: String, vararg args: Any,
100100
crossinline function: (ResultSet) -> T): T? =
101101
query(sql, ResultSetExtractor { function(it) }, *args)
102102

spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors
2+
* Copyright 2002-2019 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.
@@ -51,6 +51,13 @@ class JdbcOperationsExtensionsTests {
5151
verify(template, times(1)).queryForObject(eq(sql), any<RowMapper<Int>>(), eq(3))
5252
}
5353

54+
@Test // gh-22682
55+
fun `queryForObject with nullable RowMapper-like function`() {
56+
val sql = "select age from customer where id = ?"
57+
template.queryForObject(sql, 3) { _, _ -> null as Int? }
58+
verify(template, times(1)).queryForObject(eq(sql), any<RowMapper<Int?>>(), eq(3))
59+
}
60+
5461
@Test
5562
fun `queryForObject with reified type parameters and argTypes`() {
5663
val sql = "select age from customer where id = ?"
@@ -102,6 +109,13 @@ class JdbcOperationsExtensionsTests {
102109
verify(template, times(1)).query(eq(sql), any<ResultSetExtractor<Int>>(), eq(3))
103110
}
104111

112+
@Test // gh-22682
113+
fun `query with nullable ResultSetExtractor-like function`() {
114+
val sql = "select age from customer where id = ?"
115+
template.query<Int?>(sql, 3) { _ -> null }
116+
verify(template, times(1)).query(eq(sql), any<ResultSetExtractor<Int?>>(), eq(3))
117+
}
118+
105119
@Test
106120
fun `query with RowCallbackHandler-like function`() {
107121
val sql = "select age from customer where id = ?"

0 commit comments

Comments
 (0)