Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: EXPOSED-669 transform() broken for entities instantiated via wrapRow through an alias #2370

Merged
merged 2 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,9 @@ open class ColumnWithTransform<Unwrapped, Wrapped>(
}
}

internal fun unwrapColumnValues(values: Map<Column<*>, Any?>): Map<Column<*>, Any?> = values.mapValues { (col, value) ->
internal fun <T : Expression<*>>unwrapColumnValues(values: Map<T, Any?>): Map<T, Any?> = values.mapValues { (col, value) ->
if (col !is ExpressionWithColumnType<*>) return@mapValues value

value?.let { (col.columnType as? ColumnWithTransform<Any, Any>)?.unwrapRecursive(it) } ?: value
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,8 @@ abstract class EntityClass<ID : Any, out T : Entity<ID>>(
else -> exp to value
}
}.toMap()
return wrapRow(ResultRow.createAndFillValues(newFieldsMapping))

return wrapRow(ResultRow.createAndFillValues(unwrapColumnValues(newFieldsMapping)))
}

/**
Expand Down Expand Up @@ -265,7 +266,8 @@ abstract class EntityClass<ID : Any, out T : Entity<ID>>(
else -> exp to value
}
}.toMap()
return wrapRow(ResultRow.createAndFillValues(newFieldsMapping))

return wrapRow(ResultRow.createAndFillValues(unwrapColumnValues(newFieldsMapping)))
}

/**
Expand Down Expand Up @@ -1170,3 +1172,9 @@ abstract class ImmutableCachedEntityClass<ID : Any, out T : Entity<ID>>(
expireCache()
}
}

internal fun <T : Expression<*>> unwrapColumnValues(values: Map<T, Any?>): Map<T, Any?> = values.mapValues { (col, value) ->
if (col !is ExpressionWithColumnType<*>) return@mapValues value

value?.let { (col.columnType as? ColumnWithTransform<Any, Any>)?.unwrapRecursive(it) } ?: value
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.jetbrains.exposed.sql.tests.shared.dml

import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.entityCache
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IdTable
import org.jetbrains.exposed.dao.id.IntIdTable
Expand Down Expand Up @@ -374,4 +375,23 @@ class ColumnWithTransformTest : DatabaseTestsBase() {
assertEquals(2, tester.selectAll().first()[tester.v1].value)
}
}

@Test
fun testWrapRowWithAliases() {
withTables(TransformTable) {
TransformEntity.new {
simple = TransformDataHolder(10)
}
entityCache.clear()

val tableAlias = TransformTable.alias("table_alias")
val e2 = tableAlias.selectAll().map { TransformEntity.wrapRow(it, tableAlias) }.first()
assertEquals(10, e2.simple.value)
entityCache.clear()

val queryAlias = TransformTable.selectAll().alias("query_alias")
val e3 = queryAlias.selectAll().map { TransformEntity.wrapRow(it, queryAlias) }.first()
assertEquals(10, e3.simple.value)
}
}
}
Loading