1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
/*
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/
@file:Suppress("RUNTIME_ANNOTATION_NOT_SUPPORTED")
package kotlinx.serialization
import kotlinx.serialization.builtins.*
import kotlinx.serialization.descriptors.*
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.*
import kotlinx.serialization.internal.*
import kotlinx.serialization.test.*
import kotlin.reflect.*
import kotlin.test.*
class SerializersLookupNamedCompanionTest {
@Serializable
class Plain(val i: Int) {
companion object Named
}
@Serializable
class Parametrized<T>(val value: T) {
companion object Named
}
@Serializer(forClass = PlainWithCustom::class)
object PlainSerializer
@Serializable(PlainSerializer::class)
class PlainWithCustom(val i: Int) {
companion object Named
}
class ParametrizedSerializer<T : Any>(val serializer: KSerializer<T>) : KSerializer<ParametrizedWithCustom<T>> {
override val descriptor: SerialDescriptor =
PrimitiveSerialDescriptor("parametrized (${serializer.descriptor})", PrimitiveKind.STRING)
override fun deserialize(decoder: Decoder): ParametrizedWithCustom<T> = TODO("Not yet implemented")
override fun serialize(encoder: Encoder, value: ParametrizedWithCustom<T>) = TODO("Not yet implemented")
}
@Serializable(ParametrizedSerializer::class)
class ParametrizedWithCustom<T>(val i: T) {
companion object Named
}
@Serializable
sealed interface SealedInterface {
companion object Named
}
@Serializable
sealed interface SealedInterfaceWithExplicitAnnotation {
@NamedCompanion
companion object Named
}
@Test
fun test() {
assertSame<KSerializer<*>>(Plain.serializer(), serializer(typeOf<Plain>()))
shouldFail<SerializationException>(beforeKotlin = "1.9.20", onJs = false, onNative = false, onWasm = false) {
assertSame<KSerializer<*>>(PlainSerializer, serializer(typeOf<PlainWithCustom>()))
}
shouldFail<SerializationException>(beforeKotlin = "1.9.20", onJs = false, onNative = false, onWasm = false) {
assertEquals(
Parametrized.serializer(Int.serializer()).descriptor.toString(),
serializer(typeOf<Parametrized<Int>>()).descriptor.toString()
)
}
shouldFail<SerializationException>(beforeKotlin = "1.9.20", onJs = false, onNative = false, onWasm = false) {
assertEquals(
ParametrizedWithCustom.serializer(Int.serializer()).descriptor.toString(),
serializer(typeOf<ParametrizedWithCustom<Int>>()).descriptor.toString()
)
}
shouldFail<SerializationException>(beforeKotlin = "1.9.20", onJs = false, onNative = false, onWasm = false) {
assertEquals(
SealedInterface.serializer().descriptor.toString(),
serializer(typeOf<SealedInterface>()).descriptor.toString()
)
}
// should fail because annotation @NamedCompanion will be placed again by the compilation plugin
// and they both will be placed into @Container annotation - thus they will be invisible to the runtime
shouldFail<SerializationException>(sinceKotlin = "1.9.20", onJs = false, onNative = false, onWasm = false) {
serializer(typeOf<SealedInterfaceWithExplicitAnnotation>())
}
}
}
|