Skip to content
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 @@ -20,6 +20,7 @@ import org.jetbrains.kotlin.fir.declarations.utils.isEnumClass
import org.jetbrains.kotlin.fir.expressions.FirAnonymousFunctionExpression
import org.jetbrains.kotlin.fir.expressions.FirCall
import org.jetbrains.kotlin.fir.expressions.FirFunctionCall
import org.jetbrains.kotlin.fir.expressions.FirQualifiedAccessExpression
import org.jetbrains.kotlin.fir.expressions.arguments
import org.jetbrains.kotlin.fir.expressions.explicitReceiver
import org.jetbrains.kotlin.fir.expressions.toReference
Expand Down Expand Up @@ -96,10 +97,13 @@ class KMapperFirMappingChecker(val collector: MessageCollector, private val sess
val propSymbol = call.explicitReceiver
?.toReference(session)
?.toResolvedPropertySymbol()
val propName = propSymbol?.name
?: (call.explicitReceiver as? FirQualifiedAccessExpression)
?.calleeReference?.let { it as? FirNamedReference }?.name
val valueExpr = call.arguments.firstOrNull()
if (propSymbol != null && valueExpr != null) {
if (propName != null && valueExpr != null) {
Field(
name = propSymbol.name,
name = propName,
type = valueExpr.resolvedType,
hasDefaultValue = false,
fields = valueExpr.resolvedType.resolveConstructorFields()
Expand All @@ -110,9 +114,12 @@ class KMapperFirMappingChecker(val collector: MessageCollector, private val sess
val propSymbol = call.extensionReceiver
?.toReference(session)
?.toResolvedPropertySymbol()
if (propSymbol != null) {
val propName = propSymbol?.name
?: (call.extensionReceiver as? FirQualifiedAccessExpression)
?.calleeReference?.let { it as? FirNamedReference }?.name
if (propName != null) {
// Find the matching toField to include its type info
toFields.find { it.name == propSymbol.name }
toFields.find { it.name == propName }
} else null
}
else -> null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,65 @@ class SerializableTest {
}
}

@Test
fun shouldCompile_explicitMappingWithSerializableTargetInSeparateFile() {
IntegrationTest(options)
.file("Dto.kt") {
$$"""
|package sample
|
|import kotlinx.serialization.Serializable
|import kotlinx.serialization.SerialName
|
|@Serializable
|@SerialName("RunAnnotationDataDto")
|data class RunAnnotationDataDto(
| val runId: String,
| val displayId: String,
| val inputHash: String,
| val outputJson: String,
|)
|
""".trimMargin()
}
.file("App.kt") {
$$"""
|package sample
|
|import community.flock.kmapper.mapper
|
|data class RunAnnotationData(
| val runId: String,
| val sequenceNumber: Int,
| val inputHash: String,
| val outputJson: String,
|)
|
|fun RunAnnotationData.toDto(): RunAnnotationDataDto = mapper {
| displayId = it.sequenceNumber.toString()
|}
|
|fun main() {
| val data = RunAnnotationData(
| runId = "run-1",
| sequenceNumber = 42,
| inputHash = "abc123",
| outputJson = "{}"
| )
| val dto = data.toDto()
| println(dto)
|}
|
""".trimMargin()
}
.compileSuccess { output ->
assertTrue(
output.contains("RunAnnotationDataDto(runId=run-1, displayId=42, inputHash=abc123, outputJson={})"),
"Expected RunAnnotationDataDto(runId=run-1, displayId=42, inputHash=abc123, outputJson={}) in output"
)
}
}

@Test
fun shouldCompile_allFieldsAutoMappedWithSerializable() {
IntegrationTest(options)
Expand Down
Loading