Add declarations from serialization plugin to FIR metadata declarations provider.

These declarations should not be visible to users (and therefore are not added to FIR),
but plugin itself can reference them in already compiled serializable classes,
and therefore they should be available in metadata:

- synthetic deserialization constructor
- static write$Self function

See also:
^KT-55885
This commit is contained in:
Leonid Startsev
2023-02-15 18:47:37 +01:00
committed by Space Team
parent 8953b25c5b
commit c564dd973b
8 changed files with 215 additions and 19 deletions
@@ -1,4 +1,3 @@
// IGNORE_BACKEND_K2: JVM_IR
// TARGET_BACKEND: JVM_IR
// WITH_STDLIB
@@ -18,6 +17,15 @@ open class OpenBody {
@Serializable
abstract class AbstractConstructor(var optional: String = "foo")
// TODO: test fails for K2 if places of 'color' and 'name' are swapped
// because of https://youtrack.jetbrains.com/issue/KT-54792 (KT-20980)
// and serialization proto extension is not available in K2.
@Serializable
open class Vehicle {
var color: String? = null
var name: String? = null
}
// MODULE: app(lib)
// FILE: app.kt
@@ -35,6 +43,11 @@ class Test1: OpenBody()
@Serializable
class Test2: AbstractConstructor()
@Serializable
open class Car : Vehicle() {
var maxSpeed: Int = 100
}
fun test1() {
val string = Json.encodeToString(Test1.serializer(), Test1())
assertEquals("{}", string)
@@ -49,8 +62,29 @@ fun test2() {
assertEquals("foo", reconstructed.optional)
}
fun test3() {
val json = Json { allowStructuredMapKeys = true; encodeDefaults = true }
val car = Car()
car.maxSpeed = 100
car.name = "ford"
val s = json.encodeToString(Car.serializer(), car)
assertEquals("""{"color":null,"name":"ford","maxSpeed":100}""", s)
val restoredCar = json.decodeFromString(Car.serializer(), s)
assertEquals(100, restoredCar.maxSpeed)
assertEquals("ford", restoredCar.name)
assertEquals(null, restoredCar.color)
}
fun box(): String {
test1()
test2()
return "OK"
try {
test1()
test2()
test3()
return "OK"
} catch (e: Throwable) {
e.printStackTrace()
return e.message!!
}
}