Skip to content

Commit

Permalink
Correctly skip map values when skipping a component with incorrect key
Browse files Browse the repository at this point in the history
  • Loading branch information
0ffz committed Feb 11, 2024
1 parent d127e5e commit 1f7fcb3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ abstract class CustomMapSerializer {
return decodeSerializableElement(newDescriptor, newIndex, valueSerializer)
}

fun CompositeDecoder.skipMapValue() {
decodeElementIndex(MapSerializer(keySerializer, ContextualSerializer(Any::class)).descriptor)
}

fun deserialize(decoder: Decoder) {
var size = 0
val compositeDecoder = decoder.beginStructure(descriptor)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,22 @@ open class PolymorphicListAsMapSerializer<T : Any>(
}

else -> {
val componentSerializer =
runCatching {
findSerializerFor(
compositeDecoder.serializersModule,
namespaces,
key
)
}.onFailure {
when (onMissingSerializer) {
OnMissing.ERROR -> throw it
OnMissing.WARN -> geary.logger.w("No serializer found for $key in namespaces $namespaces, ignoring")
OnMissing.IGNORE -> return@decode
}
}.getOrNull() ?: return
components += compositeDecoder.decodeMapValue(componentSerializer)
runCatching {
findSerializerFor(
compositeDecoder.serializersModule,
namespaces,
key
)
}.onSuccess { componentSerializer ->
components += compositeDecoder.decodeMapValue(componentSerializer)
}.onFailure {
when (onMissingSerializer) {
OnMissing.ERROR -> throw it
OnMissing.WARN -> geary.logger.w("No serializer found for $key in namespaces $namespaces, ignoring")
OnMissing.IGNORE -> Unit
}
compositeDecoder.skipMapValue()
}
}
}
}
Expand Down

0 comments on commit 1f7fcb3

Please sign in to comment.