elbekD / kt-telegram-bot

Telegram Bot Library for Kotlin language
MIT License
104 stars 19 forks source link

Fixing ChatId descriptor bug #46

Closed nothingelsematters closed 2 years ago

nothingelsematters commented 2 years ago

Most of the time ChatId is serialized successfully, and the descriptor field of ChatIdSerializer is not used.

But I was trying editMessageReplyMarkup method and ChatId there is optional. \ Therefore, it stumbled upon the following line in kotlinx.serialization: kotlinx/serialization/encoding/Encoding.kt#L291 and there was stack overflow error trying to get the descriptor:

at com.elbekd.bot.model.internal.ChatIdSerializer.getDescriptor(serializers.kt:13)
at com.elbekd.bot.model.internal.ChatIdSerializer.getDescriptor(serializers.kt:13)

There is Content-based polymorphic deserialization in kotlinx.serialization that could be used here, but this would lead to more boilerplate code (because it would require ChatId.StringId and ChatId.IntegerId to be serializable and a custom serializer class for each). So I just copied the descriptor implementation from JsonContentPolymorphicSerializer class here and it works now.