Increase priority of overridden serializer on type

Fixes Kotlin/kotlinx.serialization/252
This commit is contained in:
Leonid Startsev
2018-10-30 18:24:23 +03:00
parent 127ceaca34
commit a3e34af2f1
@@ -49,10 +49,14 @@ open class SerialTypeInfo(
val unit: Boolean = false val unit: Boolean = false
) )
@Suppress("FunctionName", "LocalVariableName")
fun AbstractSerialGenerator.getSerialTypeInfo(property: SerializableProperty): SerialTypeInfo { fun AbstractSerialGenerator.getSerialTypeInfo(property: SerializableProperty): SerialTypeInfo {
fun SerializableInfo(serializer: ClassDescriptor?) =
SerialTypeInfo(property, if (property.type.isMarkedNullable) "Nullable" else "", serializer)
val T = property.type val T = property.type
val serializableWith = property.serializableWith?.toClassDescriptor T.overridenSerializer?.toClassDescriptor?.let { return SerializableInfo(it) }
if (serializableWith != null) return SerialTypeInfo(property, if (property.type.isMarkedNullable) "Nullable" else "", serializableWith) property.serializableWith?.toClassDescriptor?.let { return SerializableInfo(it) }
return when { return when {
T.isTypeParameter() -> SerialTypeInfo(property, if (property.type.isMarkedNullable) "Nullable" else "", null) T.isTypeParameter() -> SerialTypeInfo(property, if (property.type.isMarkedNullable) "Nullable" else "", null)
T.isPrimitiveNumberType() or T.isBoolean() -> SerialTypeInfo( T.isPrimitiveNumberType() or T.isBoolean() -> SerialTypeInfo(
@@ -67,16 +71,16 @@ fun AbstractSerialGenerator.getSerialTypeInfo(property: SerializableProperty): S
val serializer = property.serializableWith?.toClassDescriptor ?: property.module.findClassAcrossModuleDependencies( val serializer = property.serializableWith?.toClassDescriptor ?: property.module.findClassAcrossModuleDependencies(
referenceArraySerializerId referenceArraySerializerId
) )
SerialTypeInfo(property, if (property.type.isMarkedNullable) "Nullable" else "", serializer) SerializableInfo(serializer)
} }
T.toClassDescriptor?.kind == ClassKind.ENUM_CLASS -> { T.toClassDescriptor?.kind == ClassKind.ENUM_CLASS -> {
val serializer = property.module.findClassAcrossModuleDependencies(enumSerializerId) val serializer = property.module.findClassAcrossModuleDependencies(enumSerializerId)
SerialTypeInfo(property, if (property.type.isMarkedNullable) "Nullable" else "", serializer) SerializableInfo(serializer)
} }
else -> { else -> {
val serializer = val serializer =
findTypeSerializerOrContext(property.module, property.type, property.descriptor.annotations, property.descriptor.findPsi()) findTypeSerializerOrContext(property.module, property.type, property.descriptor.annotations, property.descriptor.findPsi())
SerialTypeInfo(property, if (property.type.isMarkedNullable) "Nullable" else "", serializer) SerializableInfo(serializer)
} }
} }
} }