Handle @Serializable classes that implement interfaces by delegation

by filtering out delegated properties that end up in irClass.properties() list

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/2157
This commit is contained in:
Leonid Startsev
2023-01-17 17:04:20 +01:00
committed by Space Team
parent 490970e65e
commit dc0cd61b6f
4 changed files with 38 additions and 2 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
@@ -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.*
@@ -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
@@ -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"
}
@@ -57,6 +57,12 @@ public class SerializationFirBlackBoxTestGenerated extends AbstractSerialization
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualFallback.kt");
}
@Test
@TestMetadata("delegatedInterface.kt")
public void testDelegatedInterface() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/delegatedInterface.kt");
}
@Test
@TestMetadata("enumsAreCached.kt")
public void testEnumsAreCached() throws Exception {
@@ -55,6 +55,12 @@ public class SerializationIrBoxTestGenerated extends AbstractSerializationIrBoxT
runTest("plugins/kotlinx-serialization/testData/boxIr/contextualFallback.kt");
}
@Test
@TestMetadata("delegatedInterface.kt")
public void testDelegatedInterface() throws Exception {
runTest("plugins/kotlinx-serialization/testData/boxIr/delegatedInterface.kt");
}
@Test
@TestMetadata("enumsAreCached.kt")
public void testEnumsAreCached() throws Exception {