Restrict auto-implementing serializers methods to certain type of classes
This commit is contained in:
+6
@@ -34,6 +34,12 @@ import org.jetbrains.kotlin.types.typeUtil.supertypes
|
||||
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerialEntityNames.KSERIALIZER_CLASS
|
||||
import org.jetbrains.kotlinx.serialization.compiler.resolve.SerializationPackages.packageFqName
|
||||
|
||||
fun isAllowedToHaveAutoGeneratedSerializerMethods(classDescriptor: ClassDescriptor): Boolean {
|
||||
// it is either GeneratedSerializer implementation
|
||||
// or user implementation which does not have type parameters (to be able to correctly initialize descriptor)
|
||||
return classDescriptor.typeConstructor.supertypes.any(::isGeneratedKSerializer) ||
|
||||
(classDescriptor.typeConstructor.supertypes.any(::isKSerializer) && classDescriptor.declaredTypeParameters.isEmpty())
|
||||
}
|
||||
|
||||
fun isKSerializer(type: KotlinType?): Boolean =
|
||||
type != null && KotlinBuiltIns.isConstructedFromGivenClass(type, SerialEntityNames.KSERIALIZER_NAME_FQ)
|
||||
|
||||
+6
@@ -136,6 +136,9 @@ object KSerializerDescriptorResolver {
|
||||
name: Name,
|
||||
result: MutableSet<PropertyDescriptor>
|
||||
) {
|
||||
// Do not auto-generate anything for user serializers
|
||||
if (!isAllowedToHaveAutoGeneratedSerializerMethods(thisDescriptor)) return
|
||||
|
||||
val classDescriptor = getSerializableClassDescriptorBySerializer(thisDescriptor) ?: return
|
||||
if (name == SerialEntityNames.SERIAL_DESC_FIELD_NAME && result.none(thisDescriptor::checkSerializableClassPropertyResult) &&
|
||||
fromSupertypes.none { thisDescriptor.checkSerializableClassPropertyResult(it) && it.modality == Modality.FINAL }
|
||||
@@ -168,6 +171,9 @@ object KSerializerDescriptorResolver {
|
||||
name: Name,
|
||||
result: MutableCollection<SimpleFunctionDescriptor>
|
||||
) {
|
||||
// Do not auto-generate anything for user serializers
|
||||
if (!isAllowedToHaveAutoGeneratedSerializerMethods(thisDescriptor)) return
|
||||
|
||||
val classDescriptor = getSerializableClassDescriptorBySerializer(thisDescriptor) ?: return
|
||||
|
||||
fun shouldAddSerializerFunction(checkParameters: (FunctionDescriptor) -> Boolean): Boolean {
|
||||
|
||||
Reference in New Issue
Block a user