diff --git a/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt b/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt index ee6836d110..5fce306450 100644 --- a/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt +++ b/spring-jdbc/src/main/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensions.kt @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors + * Copyright 2002-2019 the original author or authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,7 +34,7 @@ inline fun JdbcOperations.queryForObject(sql: String): T? = * @author Mario Arias * @since 5.0 */ -fun JdbcOperations.queryForObject(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): T? = +fun JdbcOperations.queryForObject(sql: String, vararg args: Any, function: (ResultSet, Int) -> T): T? = queryForObject(sql, RowMapper { resultSet, i -> function(resultSet, i) }, *args) /** @@ -96,7 +96,7 @@ inline fun JdbcOperations.queryForList(sql: String, args: Arra * @author Mario Arias * @since 5.0 */ -inline fun JdbcOperations.query(sql: String, vararg args: Any, +inline fun JdbcOperations.query(sql: String, vararg args: Any, crossinline function: (ResultSet) -> T): T? = query(sql, ResultSetExtractor { function(it) }, *args) diff --git a/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt b/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt index 86933772c3..4b276622e5 100644 --- a/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt +++ b/spring-jdbc/src/test/kotlin/org/springframework/jdbc/core/JdbcOperationsExtensionsTests.kt @@ -46,6 +46,13 @@ class JdbcOperationsExtensionsTests { verify { template.queryForObject(eq(sql), any>(), eq(3)) } } + @Test // gh-22682 + fun `queryForObject with nullable RowMapper-like function`() { + val sql = "select age from customer where id = ?" + template.queryForObject(sql, 3) { _, _ -> null as Int? } + verify { template.queryForObject(eq(sql), any>(), eq(3)) } + } + @Test fun `queryForObject with reified type parameters and argTypes`() { val sql = "select age from customer where id = ?" @@ -97,6 +104,13 @@ class JdbcOperationsExtensionsTests { verify { template.query(eq(sql), any>(), eq(3)) } } + @Test // gh-22682 + fun `query with nullable ResultSetExtractor-like function`() { + val sql = "select age from customer where id = ?" + template.query(sql, 3) { _ -> null } + verify { template.query(eq(sql), any>(), eq(3)) } + } + @Suppress("RemoveExplicitTypeArguments") @Test fun `query with RowCallbackHandler-like function`() {