Skip to content

Commit

Permalink
Handle @serializable classes that implement interfaces by delegation
Browse files Browse the repository at this point in the history
by filtering out delegated properties that end up in irClass.properties() list

Fixes Kotlin/kotlinx.serialization#2157

(cherry picked from commit dc0cd61)
  • Loading branch information
sandwwraith authored and qodana-bot committed Jan 23, 2023
1 parent d5e97c6 commit 2d1e8db
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package org.jetbrains.kotlinx.serialization.compiler.backend.ir
import org.jetbrains.kotlin.descriptors.DescriptorVisibilities
import org.jetbrains.kotlin.ir.ObsoleteDescriptorBasedAPI
import org.jetbrains.kotlin.ir.declarations.IrClass
import org.jetbrains.kotlin.ir.declarations.IrDeclarationOrigin
import org.jetbrains.kotlin.ir.declarations.IrProperty
import org.jetbrains.kotlin.ir.types.IrSimpleType
import org.jetbrains.kotlin.ir.util.*
Expand Down Expand Up @@ -78,7 +79,7 @@ internal fun serializablePropertiesForIrBackend(

val (primaryCtorSerializableProps, bodySerializableProps) = properties
.asSequence()
.filter { !it.isFakeOverride && !it.isDelegated }
.filter { !it.isFakeOverride && !it.isDelegated && it.origin != IrDeclarationOrigin.DELEGATED_MEMBER }
.filter(::isPropSerializable)
.map {
val isConstructorParameterWithDefault = primaryParamsAsProps[it] ?: false
Expand Down
23 changes: 23 additions & 0 deletions plugins/kotlinx-serialization/testData/boxIr/delegatedInterface.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// TARGET_BACKEND: JVM_IR

// WITH_STDLIB

import kotlinx.serialization.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.encoding.*
import kotlinx.serialization.json.*

@Serializable()
class Dto(
val data: Map<Int, Int>
) : Map<Int, Int> by data

fun box(): String {
val dto = Dto(mapOf(1 to 2))
val s = Json.encodeToString(dto)
if (s != """{"data":{"1":2}}""") return s
val d = Json.decodeFromString<Dto>(s)
if (d.size != 1) return "Delegation to Map failed"
if (d.data != dto.data) return "Equals failed ${d.data}"
return "OK"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2d1e8db

Please sign in to comment.