elbekD / kt-telegram-bot

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

Fixing ChatMember serialization bug #45

Closed nothingelsematters closed 2 years ago

nothingelsematters commented 2 years ago

We've tried to block and unblock bot and the logs of bot went crazy, there was one constant unstoppable error:

kotlinx.serialization.json.internal.JsonDecodingException: Polymorphic serializer was not found for missing class discriminator ('null')
JSON input: {"user":{"id":12...5,"is_bot":true,"first_name":"name","username":"username"},"status":"member"}

It reduced to the following code failing with the sam issue:

val json = "{\"user\":{\"id\":120,\"is_bot\":true,\"first_name\":\"name\",\"username\":\"username\"},\"status\":\"member\"}"
println(Json.decodeFromString<ChatMember>(json))

So the problem was that there was no information that can be used to determine which subclass of ChatMember to use in deserialization. This information contains in field status (in documentation: ChatMemberOwner, status - The member's status in the chat, always "creator"; and other "always" values for each type).

Now the above kotlin code runs successfully.