diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/AbstractSerialGenerator.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/AbstractSerialGenerator.kt index dda81c7cc1a52..d3d5c2a03030f 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/AbstractSerialGenerator.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/AbstractSerialGenerator.kt @@ -46,11 +46,11 @@ abstract class AbstractSerialGenerator(val bindingContext: BindingContext, val c ).toSet() } - val additionalSerializersInScopeOfCurrentFile: Map by lazy { + val additionalSerializersInScopeOfCurrentFile: Map by lazy { getKClassListFromFileAnnotation(SerializationAnnotations.additionalSerializersFqName, currentDeclaration) .associateBy( { - it.supertypes().find(::isKSerializer)?.arguments?.firstOrNull()?.type.toClassDescriptor + it.supertypes().find(::isKSerializer)?.arguments?.firstOrNull()?.type ?: throw AssertionError("Argument for ${SerializationAnnotations.additionalSerializersFqName} does not implement KSerializer or does not provide serializer for concrete type") }, { it.toClassDescriptor!! } @@ -59,4 +59,4 @@ abstract class AbstractSerialGenerator(val bindingContext: BindingContext, val c protected fun ClassDescriptor.getFuncDesc(funcName: String): Sequence = unsubstitutedMemberScope.getDescriptorsFiltered { it == Name.identifier(funcName) }.asSequence().filterIsInstance() -} \ No newline at end of file +} diff --git a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/TypeUtil.kt b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/TypeUtil.kt index eab539e54a3d3..1fcb94c05d05e 100644 --- a/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/TypeUtil.kt +++ b/plugins/kotlin-serialization/kotlin-serialization-compiler/src/org/jetbrains/kotlinx/serialization/compiler/backend/common/TypeUtil.kt @@ -34,7 +34,7 @@ open class SerialTypeInfo( ) fun AbstractSerialGenerator.findAddOnSerializer(propertyType: KotlinType, module: ModuleDescriptor): ClassDescriptor? { - additionalSerializersInScopeOfCurrentFile[propertyType.toClassDescriptor]?.let { return it } + additionalSerializersInScopeOfCurrentFile[propertyType]?.let { return it } if (propertyType in contextualKClassListInCurrentFile) return module.getClassFromSerializationPackage(SpecialBuiltins.contextSerializer) if (propertyType.toClassDescriptor?.annotations?.hasAnnotation(SerializationAnnotations.polymorphicFqName) == true) @@ -123,9 +123,12 @@ fun AbstractSerialGenerator.findTypeSerializerOrContextUnchecked( ): ClassDescriptor? { val annotations = kType.annotations if (kType.isTypeParameter()) return null - if (kType.isMarkedNullable) return findTypeSerializerOrContextUnchecked(module, kType.makeNotNullable()) annotations.serializableWith(module)?.let { return it.toClassDescriptor } - additionalSerializersInScopeOfCurrentFile[kType.toClassDescriptor]?.let { return it } + additionalSerializersInScopeOfCurrentFile[kType]?.let { return it } + if (!kType.isMarkedNullable) { + additionalSerializersInScopeOfCurrentFile[kType.makeNullable()]?.let { return it } + } + if (kType in contextualKClassListInCurrentFile) return module.getClassFromSerializationPackage(SpecialBuiltins.contextSerializer) return analyzeSpecialSerializers(module, annotations) ?: findTypeSerializer(module, kType) }