Prioritize @Serializable annotation on type usage over class usage

In `val p: @Serializable(X) Foo` and `@Serializable(Y) class Foo` situation,
now X would be chosen as serializer.

Fixes https://github.com/Kotlin/kotlinx.serialization/issues/1895
This commit is contained in:
Leonid Startsev
2022-09-27 19:46:08 +02:00
committed by Space Team
parent d2d13c8765
commit 242b83edbd
7 changed files with 82 additions and 13 deletions
@@ -46,7 +46,7 @@ fun BaseIrGenerator.getIrSerialTypeInfo(property: IrSerializableProperty, ctx: S
val T = property.type
property.serializableWith(ctx)?.let { return SerializableInfo(it) }
findAddOnSerializer(T, ctx)?.let { return SerializableInfo(it) }
T.overridenSerializer?.let { return SerializableInfo(it) }
T.overriddenSerializer?.let { return SerializableInfo(it) }
return when {
T.isTypeParameter() -> IrSerialTypeInfo(property, if (property.type.isMarkedNullable()) "Nullable" else "", null)
T.isPrimitiveType() -> IrSerialTypeInfo(
@@ -112,7 +112,7 @@ fun analyzeSpecialSerializers(
fun findTypeSerializer(context: SerializationBaseContext, type: IrType): IrClassSymbol? {
type.overridenSerializer?.let { return it }
type.overriddenSerializer?.let { return it }
if (type.isTypeParameter()) return null
if (type.isArray()) return context.referenceClassId(referenceArraySerializerId)
if (type.isGeneratedSerializableObject()) return context.referenceClassId(objectSerializerId)
@@ -166,8 +166,9 @@ internal fun IrClass.polymorphicSerializerIfApplicableAutomatically(context: Ser
}
}
internal val IrType.overridenSerializer: IrClassSymbol?
internal val IrType.overriddenSerializer: IrClassSymbol?
get() {
annotations.serializableWith()?.let { return it }
val desc = this.classOrNull ?: return null
desc.owner.serializableWith?.let { return it }
return null
@@ -538,7 +538,7 @@ fun AbstractSerialGenerator.getSerialTypeInfo(property: SerializableProperty, ty
property.serializableWith?.toClassDescriptor?.let { return SerializableInfo(it) }
findAddOnSerializer(property.type, property.module)?.let { return SerializableInfo(it) }
property.type.overridenSerializer?.toClassDescriptor?.let { return SerializableInfo(it) }
property.type.overriddenSerializer(property.module)?.toClassDescriptor?.let { return SerializableInfo(it) }
if (property.type.isTypeParameter()) return JVMSerialTypeInfo(
property,