You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@Test
fun testContextualByType() {
val json = Json { serializersModule = serializersModuleOf(UserData::class, UserSerializer) }
val type = typeOf<UserData>()
val serializer = json.serializersModule.serializerOrNull(type)
assertEquals(UserSerializer, serializer)
}
@Serializable
public data class UserData(val id: Int, val name: String)
public object UserSerializer : KSerializer<UserData> {
override val descriptor: SerialDescriptor = buildClassSerialDescriptor("UserData")
override fun deserialize(decoder: Decoder): UserData = TODO()
override fun serialize(encoder: Encoder, value: UserData) = TODO()
}
Expected behavior
The test should pass.
I believe that the problem is in these lines inside serializerByKTypeImpl function.
val result: KSerializer<Any>? = when {
typeArguments.isEmpty() -> rootClass.serializerOrNull() ?: getContextual(rootClass)
else -> builtinSerializer(typeArguments, rootClass, failOnMissingTypeArgSerializer)
}?.cast()
The expression rootClass.serializerOrNull() ?: getContextual(rootClass) should be the opposite: first try to get contextual and if it's not found, fallback to default serializer.
Environment
Kotlin version: [e.g. 1.6.10]
Library version: [e.g. 1.3.0]
The text was updated successfully, but these errors were encountered:
I think we won't change the semantics of the existing stable function in such an unpredictable way for users (see also #2026). Whether we should provide a new function that prioritizes module over built-in is an open question.
Describe the bug
To Reproduce
Expected behavior
The test should pass.
I believe that the problem is in these lines inside
serializerByKTypeImpl
function.The expression
rootClass.serializerOrNull() ?: getContextual(rootClass)
should be the opposite: first try to get contextual and if it's not found, fallback to default serializer.Environment
The text was updated successfully, but these errors were encountered: