|
| 1 | +/* |
| 2 | + * Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package kotlinx.serialization |
| 6 | + |
| 7 | +import kotlinx.serialization.builtins.* |
| 8 | +import kotlinx.serialization.descriptors.* |
| 9 | +import kotlinx.serialization.encoding.* |
| 10 | +import kotlinx.serialization.modules.* |
| 11 | +import kotlinx.serialization.test.* |
| 12 | +import kotlin.reflect.* |
| 13 | +import kotlin.test.* |
| 14 | + |
| 15 | +// Imagine this is a 3rd party interface |
| 16 | +interface IApiError { |
| 17 | + val code: Int |
| 18 | +} |
| 19 | + |
| 20 | +@Serializable(CustomSer::class) |
| 21 | +interface HasCustom |
| 22 | + |
| 23 | + |
| 24 | +object CustomSer: KSerializer<HasCustom> { |
| 25 | + override val descriptor: SerialDescriptor |
| 26 | + get() = TODO("Not yet implemented") |
| 27 | + |
| 28 | + override fun serialize(encoder: Encoder, value: HasCustom) { |
| 29 | + TODO("Not yet implemented") |
| 30 | + } |
| 31 | + |
| 32 | + override fun deserialize(decoder: Decoder): HasCustom { |
| 33 | + TODO("Not yet implemented") |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +@Suppress("UNCHECKED_CAST") |
| 38 | +class InterfaceContextualSerializerTest { |
| 39 | + |
| 40 | + object MyApiErrorSerializer : KSerializer<IApiError> { |
| 41 | + override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("IApiError", PrimitiveKind.INT) |
| 42 | + |
| 43 | + override fun serialize(encoder: Encoder, value: IApiError) { |
| 44 | + encoder.encodeInt(value.code) |
| 45 | + } |
| 46 | + |
| 47 | + override fun deserialize(decoder: Decoder): IApiError { |
| 48 | + val code = decoder.decodeInt() |
| 49 | + return object : IApiError { |
| 50 | + override val code: Int = code |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private inline fun <reified T> SerializersModule.doTest(block: (KSerializer<T>) -> Unit) { |
| 56 | + block(this.serializer<T>()) |
| 57 | + block(this.serializer(typeOf<T>()) as KSerializer<T>) |
| 58 | + } |
| 59 | + |
| 60 | + // Native, WASM - can't retrieve serializer (no .isInterface) |
| 61 | + @Test |
| 62 | + fun testDefault() { |
| 63 | + if (isNative() || isWasm()) return |
| 64 | + assertEquals(PolymorphicKind.OPEN, serializer<IApiError>().descriptor.kind) |
| 65 | + assertEquals(PolymorphicKind.OPEN, serializer(typeOf<IApiError>()).descriptor.kind) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun testCustom() { |
| 70 | + assertSame(CustomSer, serializer<HasCustom>()) |
| 71 | + assertSame(CustomSer, serializer(typeOf<HasCustom>()) as KSerializer<HasCustom>) |
| 72 | + } |
| 73 | + |
| 74 | + // JVM - intrinsics kick in |
| 75 | + @Test |
| 76 | + fun testContextual() { |
| 77 | + val module = serializersModuleOf(IApiError::class, MyApiErrorSerializer) |
| 78 | + assertSame(MyApiErrorSerializer, module.serializer(typeOf<IApiError>()) as KSerializer<IApiError>) |
| 79 | + shouldFail<AssertionError>(beforeKotlin = "2.0.0", onJvm = true, onWasm = false, onNative = false, onJs = false ) { |
| 80 | + assertSame(MyApiErrorSerializer, module.serializer<IApiError>()) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + // JVM - intrinsics kick in |
| 85 | + @Test |
| 86 | + fun testInsideList() { |
| 87 | + val module = serializersModuleOf(IApiError::class, MyApiErrorSerializer) |
| 88 | + assertEquals(MyApiErrorSerializer.descriptor, module.serializer(typeOf<List<IApiError>>()).descriptor.elementDescriptors.first()) |
| 89 | + shouldFail<AssertionError>(beforeKotlin = "2.0.0", onWasm = false, onNative = false, onJs = false ) { |
| 90 | + assertEquals( |
| 91 | + MyApiErrorSerializer.descriptor, |
| 92 | + module.serializer<List<IApiError>>().descriptor.elementDescriptors.first() |
| 93 | + ) |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + fun testWithNullability() { |
| 99 | + val module = serializersModuleOf(IApiError::class, MyApiErrorSerializer) |
| 100 | + assertEquals(MyApiErrorSerializer.nullable.descriptor, module.serializer(typeOf<IApiError?>()).descriptor) |
| 101 | + shouldFail<AssertionError>(beforeKotlin = "2.0.0", onWasm = false, onNative = false, onJs = false ) { |
| 102 | + assertEquals(MyApiErrorSerializer.nullable.descriptor, module.serializer<IApiError?>().descriptor) |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun testWithNullabilityInsideList() { |
| 108 | + val module = serializersModuleOf(IApiError::class, MyApiErrorSerializer) |
| 109 | + assertEquals(MyApiErrorSerializer.nullable.descriptor, module.serializer(typeOf<List<IApiError?>>()).descriptor.elementDescriptors.first()) |
| 110 | + shouldFail<AssertionError>(beforeKotlin = "2.0.0", onWasm = false, onNative = false, onJs = false ) { |
| 111 | + assertEquals( |
| 112 | + MyApiErrorSerializer.nullable.descriptor, |
| 113 | + module.serializer<List<IApiError?>>().descriptor.elementDescriptors.first() |
| 114 | + ) |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + class Unrelated |
| 119 | + |
| 120 | + object UnrelatedSerializer: KSerializer<Unrelated> { |
| 121 | + override val descriptor: SerialDescriptor |
| 122 | + get() = TODO("Not yet implemented") |
| 123 | + |
| 124 | + override fun serialize(encoder: Encoder, value: Unrelated) { |
| 125 | + TODO("Not yet implemented") |
| 126 | + } |
| 127 | + |
| 128 | + override fun deserialize(decoder: Decoder): Unrelated { |
| 129 | + TODO("Not yet implemented") |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + fun interfaceSerializersAreCachedInsideIfModuleIsNotFilledWithInterface() = jvmOnly { |
| 135 | + // Caches are implemented on JVM |
| 136 | + val module = serializersModuleOf(Unrelated::class, UnrelatedSerializer) |
| 137 | + val p1 = module.serializer(typeOf<List<IApiError>>()) |
| 138 | + assertEquals(PolymorphicKind.OPEN, p1.descriptor.elementDescriptors.first().kind) |
| 139 | + val p2 = module.serializer(typeOf<List<IApiError>>()) |
| 140 | + assertSame(p1, p2) |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + fun interfaceSerializersAreCachedTopLevelIfModuleIsNotFilledWithInterface() = jvmOnly { |
| 145 | + val module = serializersModuleOf(Unrelated::class, UnrelatedSerializer) |
| 146 | + val p1 = module.serializer(typeOf<IApiError>()) |
| 147 | + assertEquals(PolymorphicKind.OPEN, p1.descriptor.kind) |
| 148 | + val p2 = module.serializer(typeOf<IApiError>()) |
| 149 | + assertSame(p1, p2) |
| 150 | + } |
| 151 | + |
| 152 | + interface Parametrized<T> { |
| 153 | + val param: List<T> |
| 154 | + } |
| 155 | + |
| 156 | + class PSer<T>(val tSer: KSerializer<T>): KSerializer<Parametrized<T>> { |
| 157 | + override val descriptor: SerialDescriptor |
| 158 | + get() = buildClassSerialDescriptor("PSer<${tSer.descriptor.serialName}>") |
| 159 | + |
| 160 | + override fun serialize(encoder: Encoder, value: Parametrized<T>) { |
| 161 | + TODO("Not yet implemented") |
| 162 | + } |
| 163 | + |
| 164 | + override fun deserialize(decoder: Decoder): Parametrized<T> { |
| 165 | + TODO("Not yet implemented") |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + fun testParametrizedInterface() { |
| 171 | + if (!isNative() && !isWasm()) { |
| 172 | + assertEquals(PolymorphicKind.OPEN, serializer(typeOf<Parametrized<String>>()).descriptor.kind) |
| 173 | + } |
| 174 | + val md = SerializersModule { |
| 175 | + contextual(Parametrized::class) { PSer(it[0]) } |
| 176 | + } |
| 177 | + assertEquals("PSer<kotlin.String>", md.serializer(typeOf<Parametrized<String>>()).descriptor.serialName) |
| 178 | + shouldFail<AssertionError>(beforeKotlin = "2.0.0", onWasm = false, onNative = false, onJs = false ) { |
| 179 | + assertEquals("PSer<kotlin.String>", md.serializer<Parametrized<String>>().descriptor.serialName) |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments