From 2b7b13ec6af5a83cf718023c3a47062587c88e5f Mon Sep 17 00:00:00 2001 From: Jocelyne Date: Wed, 26 Apr 2023 16:26:14 +0200 Subject: [PATCH] fix: EXPOSED-29 Cannot set nullable composite column in InsertStatement Remove the upper bound generic constraint for the function `set(column: CompositeColumn, value: S)` https://kotlinlang.org/docs/generics.html#upper-bounds:~:text=The%20default%20upper%20bound%20(if%20there%20was%20none%20specified)%20is%20Any%3F --- .../exposed/sql/statements/UpdateBuilder.kt | 4 ++-- .../jetbrains/exposed/sql/money/MoneyTests.kt | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/UpdateBuilder.kt b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/UpdateBuilder.kt index f709481b2b..2bd36a79a9 100644 --- a/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/UpdateBuilder.kt +++ b/exposed-core/src/main/kotlin/org/jetbrains/exposed/sql/statements/UpdateBuilder.kt @@ -28,7 +28,7 @@ abstract class UpdateBuilder(type: StatementType, targets: List) : } @LowPriorityInOverloadResolution - open operator fun < S> set(column: Column, value: S) { + open operator fun set(column: Column, value: S) { require(column.columnType.nullable || (value != null && value !is Op.NULL)) { "Trying to set null to not nullable column $column" } @@ -66,7 +66,7 @@ abstract class UpdateBuilder(type: StatementType, targets: List
) : open operator fun set(column: Column, value: Query) = update(column, wrapAsExpression(value)) - open operator fun set(column: CompositeColumn, value: S) { + open operator fun set(column: CompositeColumn, value: S) { column.getRealColumnsWithValues(value).forEach { (realColumn, itsValue) -> set(realColumn as Column, itsValue) } } diff --git a/exposed-money/src/test/kotlin/org/jetbrains/exposed/sql/money/MoneyTests.kt b/exposed-money/src/test/kotlin/org/jetbrains/exposed/sql/money/MoneyTests.kt index 8a0102c685..ebfec11473 100644 --- a/exposed-money/src/test/kotlin/org/jetbrains/exposed/sql/money/MoneyTests.kt +++ b/exposed-money/src/test/kotlin/org/jetbrains/exposed/sql/money/MoneyTests.kt @@ -78,6 +78,24 @@ open class MoneyBaseTest : DatabaseTestsBase() { } } + @Test + fun testNullableCompositeColumnInsertAndSelect() { + val table = object : IntIdTable("Table") { + val composite_money = compositeMoney(8, AMOUNT_SCALE, "composite_money").nullable() + } + + withTables(table) { + val id = table.insertAndGetId { + it[composite_money] = null + } + + val resultRow = table.select { table.id.eq(id) }.single() + val result = resultRow[table.composite_money] + + assertEquals(null, result) + } + } + private fun testInsertedAndSelect(toInsert: Money?) { withTables(Account) { val accountID = Account.insertAndGetId {