Skip to content

Commit

Permalink
fix: EXPOSED-501 Column.transform() ignores custom setParameter() log…
Browse files Browse the repository at this point in the history
…ic (#2214)

Adding transform() to a column with a type that relies on logic in setParameter()
leads to failure because the delegate's function is not invoked by the wrapping
ColumnWithTransform.

This adds an override to ColumnWithTransform that ensures the delegate's
function is entered.
  • Loading branch information
bog-walk committed Aug 26, 2024
1 parent 9824804 commit 8ca4d02
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions exposed-core/api/exposed-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ public class org/jetbrains/exposed/sql/ColumnWithTransform : org/jetbrains/expos
public fun nonNullValueToString (Ljava/lang/Object;)Ljava/lang/String;
public fun notNullValueToDB (Ljava/lang/Object;)Ljava/lang/Object;
public fun setNullable (Z)V
public fun setParameter (Lorg/jetbrains/exposed/sql/statements/api/PreparedStatementApi;ILjava/lang/Object;)V
public fun sqlType ()Ljava/lang/String;
public fun unwrap (Ljava/lang/Object;)Ljava/lang/Object;
public final fun unwrapRecursive (Ljava/lang/Object;)Ljava/lang/Object;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ open class ColumnWithTransform<Unwrapped : Any, Wrapped : Any>(
}

override var nullable = delegate.nullable

override fun setParameter(stmt: PreparedStatementApi, index: Int, value: Any?) {
return delegate.setParameter(stmt, index, value)
}
}

// Numeric columns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,4 +406,23 @@ class JsonColumnTests : DatabaseTestsBase() {
assertEquals(newData2, newResult?.get(tester.jsonColumn))
}
}

@Test
fun testJsonWithTransformer() {
val tester = object : Table("tester") {
val numbers: Column<DoubleArray> = json<IntArray>("numbers", Json.Default).transform(
wrap = { DoubleArray(it.size) { i -> 1.0 * it[i] } },
unwrap = { IntArray(it.size) { i -> it[i].toInt() } }
)
}

withTables(tester) {
val data = doubleArrayOf(1.0, 2.0, 3.0)
tester.insert {
it[numbers] = data
}

assertContentEquals(data, tester.selectAll().single()[tester.numbers])
}
}
}

0 comments on commit 8ca4d02

Please sign in to comment.