Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefer Contextual serializer of default Polymorphic serializer. #2026

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/commonMain/src/kotlinx/serialization/Serializers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private fun SerializersModule.serializerByKTypeImpl(
val typeArguments = type.arguments
.map { requireNotNull(it.type) { "Star projections in type arguments are not allowed, but had $type" } }
val result: KSerializer<Any>? = when {
typeArguments.isEmpty() -> rootClass.serializerOrNull() ?: getContextual(rootClass)
typeArguments.isEmpty() -> getContextual(rootClass) ?: rootClass.serializerOrNull()
else -> builtinSerializer(typeArguments, rootClass, failOnMissingTypeArgSerializer)
}?.cast()
return result?.nullable(isNullable)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package kotlinx.serialization.modules

import kotlinx.serialization.*
import kotlinx.serialization.encoding.*
import kotlin.test.*

open class ContextualInterfaceTest {
sealed interface Dummy {
val data: String
}

@Serializable
data class DummyImpl(override val data: String) : Dummy

object DummySerializer : KSerializer<Dummy> {
override val descriptor = DummyImpl.serializer().descriptor

override fun serialize(encoder: Encoder, value: Dummy) =
DummyImpl.serializer().serialize(encoder, DummyImpl(value.data))

override fun deserialize(decoder: Decoder): Dummy = DummyImpl.serializer().deserialize(decoder)
}

private val module = SerializersModule { contextual(Dummy::class, DummySerializer) }

@Test
fun testContextualWinsOverCompiled() {
val serializer = module.serializer<Dummy>()
assertEquals(DummySerializer, serializer, "Contextual serializer should win over polymorphicSerializer.")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,24 +197,6 @@ class SerializersLookupTest : JsonTestBase() {
assertEquals("[[null]]", Json.encodeToString(serializer, listOf(listOf<IntBox?>(null))))
}

@Test
fun testCompiledWinsOverContextual() {
val contextual = object : KSerializer<Int> {
override val descriptor: SerialDescriptor = Int.serializer().descriptor

override fun serialize(encoder: Encoder, value: Int) {
fail()
}

override fun deserialize(decoder: Decoder): Int {
fail()
}
}
val json = Json { serializersModule = SerializersModule { contextual(contextual) } }
assertEquals("[[1]]", json.encodeToString(listOf(listOf<Int>(1))))
assertEquals("42", json.encodeToString(42))
}

class NonSerializable

class NonSerializableBox<T>(val boxed: T)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,6 @@ class SerializerByTypeTest {
assertEquals("[[42]]", Json.encodeToString(serializer, listOf(listOf(IntBox(1)))))
}

@Test
fun testCompiledWinsOverContextual() {
val contextual = object : KSerializer<Int> {
override val descriptor: SerialDescriptor = Int.serializer().descriptor

override fun serialize(encoder: Encoder, value: Int) {
fail()
}

override fun deserialize(decoder: Decoder): Int {
fail()
}
}
val module = SerializersModule { contextual(contextual) }
val serializer = module.serializer(typeTokenOf<List<List<Int>>>())
assertEquals("[[1]]", Json.encodeToString(serializer, listOf(listOf<Int>(1))))
assertEquals("42", Json.encodeToString(module.serializer(typeTokenOf<Int>()), 42))
}

class NonSerializable

class NonSerializableBox<T>(val boxed: T)
Expand Down