Change order in SerializableProperties initialization logic

in that way so binding context would be checked only after resolve is triggered (via .getContributed descriptors).
Empty binding context led to some subtle bugs.
Fixes https://github.com/Kotlin/kotlinx.serialization/issues/508
This commit is contained in:
Leonid Startsev
2019-07-16 14:54:19 +03:00
parent 822b4556e3
commit 6494227c64
@@ -27,22 +27,37 @@ class SerializableProperties(private val serializableClass: ClassDescriptor, val
private val primaryConstructorParameters: List<ValueParameterDescriptor> = private val primaryConstructorParameters: List<ValueParameterDescriptor> =
serializableClass.unsubstitutedPrimaryConstructor?.valueParameters ?: emptyList() serializableClass.unsubstitutedPrimaryConstructor?.valueParameters ?: emptyList()
private val primaryConstructorProperties: Map<PropertyDescriptor, Boolean> = val serializableProperties: List<SerializableProperty>
val isExternallySerializable: Boolean
private val primaryConstructorProperties: Map<PropertyDescriptor, Boolean>
init {
val descriptorsSequence = serializableClass.unsubstitutedMemberScope.getContributedDescriptors(DescriptorKindFilter.VARIABLES)
.asSequence()
// call to any BindingContext.get should be only AFTER MemberScope.getContributedDescriptors
primaryConstructorProperties =
primaryConstructorParameters.asSequence() primaryConstructorParameters.asSequence()
.map { parameter -> bindingContext[BindingContext.VALUE_PARAMETER_AS_PROPERTY, parameter] to parameter.declaresDefaultValue() } .map { parameter -> bindingContext[BindingContext.VALUE_PARAMETER_AS_PROPERTY, parameter] to parameter.declaresDefaultValue() }
.mapNotNull { (a, b) -> if (a == null) null else a to b } .mapNotNull { (a, b) -> if (a == null) null else a to b }
.toMap() .toMap()
val isExternallySerializable: Boolean = fun isPropSerializable(it: PropertyDescriptor) =
primaryConstructorParameters.size == primaryConstructorProperties.size if (serializableClass.isInternalSerializable) !it.annotations.serialTransient
else !Visibilities.isPrivate(it.visibility) && ((it.isVar && !it.annotations.serialTransient) || primaryConstructorProperties.contains(
it
))
val serializableProperties: List<SerializableProperty> = serializableProperties = descriptorsSequence.filterIsInstance<PropertyDescriptor>()
serializableClass.unsubstitutedMemberScope.getContributedDescriptors(DescriptorKindFilter.VARIABLES)
.asSequence()
.filterIsInstance<PropertyDescriptor>()
.filter { it.kind == CallableMemberDescriptor.Kind.DECLARATION } .filter { it.kind == CallableMemberDescriptor.Kind.DECLARATION }
.filter(this::isPropSerializable) .filter(::isPropSerializable)
.map { prop -> SerializableProperty(prop, primaryConstructorProperties[prop] ?: false, prop.hasBackingField(bindingContext)) } .map { prop ->
SerializableProperty(
prop,
primaryConstructorProperties[prop] ?: false,
prop.hasBackingField(bindingContext)
)
}
.filterNot { it.transient } .filterNot { it.transient }
.partition { primaryConstructorProperties.contains(it.descriptor) } .partition { primaryConstructorProperties.contains(it.descriptor) }
.run { .run {
@@ -52,12 +67,8 @@ class SerializableProperties(private val serializableClass: ClassDescriptor, val
else else
SerializableProperties(supers, bindingContext).serializableProperties + first + second SerializableProperties(supers, bindingContext).serializableProperties + first + second
} }
isExternallySerializable = primaryConstructorParameters.size == primaryConstructorProperties.size
private fun isPropSerializable(it: PropertyDescriptor) = }
if (serializableClass.isInternalSerializable) !it.annotations.serialTransient
else !Visibilities.isPrivate(it.visibility) && ((it.isVar && !it.annotations.serialTransient) || primaryConstructorProperties.contains(
it
))
val serializableConstructorProperties: List<SerializableProperty> = val serializableConstructorProperties: List<SerializableProperty> =
serializableProperties.asSequence() serializableProperties.asSequence()