-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Call a specified function in intrinsic if polymorphic serializer is p…
…rovided for interface. This prioritizes module contents over default polymorphic serializer. See Kotlin/kotlinx.serialization#2060 and Kotlin/kotlinx.serialization#2565
- Loading branch information
1 parent
e72bac3
commit a2c51df
Showing
4 changed files
with
163 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
plugins/kotlinx-serialization/testData/boxIr/intrinsicsPolymorphicPriority.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// TARGET_BACKEND: JVM_IR | ||
|
||
// WITH_STDLIB | ||
|
||
// FILE: stub.kt | ||
|
||
@file:JvmName("SerializersKt") | ||
|
||
package kotlinx.serialization | ||
|
||
import kotlin.reflect.KClass | ||
import kotlinx.serialization.modules.* | ||
|
||
// Copy of runtime function from kotlinx-serialization 1.7.0 | ||
fun moduleThenPolymorphic(module: SerializersModule, kClass: KClass<*>): KSerializer<*> { | ||
return module.getContextual(kClass) ?: PolymorphicSerializer(kClass) | ||
} | ||
|
||
fun moduleThenPolymorphic(module: SerializersModule, kClass: KClass<*>, argSerializers: Array<KSerializer<*>>): KSerializer<*> { | ||
return module.getContextual(kClass, argSerializers.asList()) ?: PolymorphicSerializer(kClass) | ||
} | ||
|
||
// FILE: test.kt | ||
|
||
package a | ||
|
||
import kotlinx.serialization.* | ||
import kotlinx.serialization.descriptors.* | ||
import kotlinx.serialization.encoding.* | ||
import kotlinx.serialization.modules.* | ||
import kotlin.reflect.KClass | ||
import kotlin.test.* | ||
|
||
interface IApiError { | ||
val code: Int | ||
} | ||
|
||
object MyApiErrorSerializer : KSerializer<IApiError> { | ||
override val descriptor: SerialDescriptor = PrimitiveSerialDescriptor("IApiError", PrimitiveKind.INT) | ||
|
||
override fun serialize(encoder: Encoder, value: IApiError) { | ||
TODO() | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): IApiError { | ||
TODO() | ||
} | ||
} | ||
|
||
interface Parametrized<T> { | ||
val param: List<T> | ||
} | ||
|
||
class PSer<T>(val tSer: KSerializer<T>) : KSerializer<Parametrized<T>> { | ||
override val descriptor: SerialDescriptor | ||
get() = buildClassSerialDescriptor("PSer<${tSer.descriptor.serialName}>") | ||
|
||
override fun serialize(encoder: Encoder, value: Parametrized<T>) { | ||
TODO("Not yet implemented") | ||
} | ||
|
||
override fun deserialize(decoder: Decoder): Parametrized<T> { | ||
TODO("Not yet implemented") | ||
} | ||
} | ||
|
||
fun testParametrized() { | ||
val md = SerializersModule { | ||
contextual(Parametrized::class) { PSer(it[0]) } | ||
} | ||
assertEquals("PSer<kotlin.String>", md.serializer<Parametrized<String>>().descriptor.serialName) | ||
} | ||
|
||
fun box(): String { | ||
val module = serializersModuleOf(IApiError::class, MyApiErrorSerializer) | ||
assertSame(MyApiErrorSerializer, module.serializer<IApiError>() as KSerializer<IApiError>) | ||
assertEquals( | ||
MyApiErrorSerializer.descriptor, | ||
module.serializer<List<IApiError>>().descriptor.elementDescriptors.first() | ||
) | ||
testParametrized() | ||
return "OK" | ||
} |
6 changes: 6 additions & 0 deletions
6
...tbrains/kotlinx/serialization/runners/SerializationFirLightTreeBlackBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 6 additions & 0 deletions
6
...ests-gen/org/jetbrains/kotlinx/serialization/runners/SerializationIrBoxTestGenerated.java
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.