EntitiesContextBuilder wrong cast type for field parameters
Closed this issue · 3 comments
Describe the bug
If it is necessary to send multiple EntityType in one message, an error occurs and the message is not sent.
To Reproduce
Steps to reproduce the behavior:
- Create simple CommandHandler:
@CommandHandler(value = ["/stop"])
suspend fun stop(user: User, bot: TelegramBot) {
message { "test message" - bold { " test" } }.send(user, bot) // The message will be sent successfully
message { "test message" - bold { " test" } - bold { " test2" } }.send(user, bot) // It will not be sent, an error will occur
}
- See error
java.lang.ClassCastException: kotlinx.serialization.json.JsonArray cannot be cast to kotlin.collections.MutableList
at eu.vendeli.tgbot.utils.builders.EntitiesContextBuilder$DefaultImpls.minus(EntitiesContextBuilder.kt:162)
at eu.vendeli.tgbot.api.message.SendMessageAction.minus(Message.kt:17)
at ru.ship.bot.BotMainKt$stop$3.invoke(BotMain.kt:64)
at ru.ship.bot.BotMainKt$stop$3.invoke(BotMain.kt:64)
at eu.vendeli.tgbot.api.message.SendMessageAction.<init>(Message.kt:33)
at eu.vendeli.tgbot.api.message.MessageKt.message(Message.kt:56)
at ru.ship.bot.BotMainKt.stop(BotMain.kt:64)
at ru.ship.bot.BotMainKt$stop$1.invokeSuspend(BotMain.kt)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:104)
at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:111)
at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:99)
at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:585)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:802)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:706)
at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:693)
Expected behavior
The message will be sent with the specified multiple EntityType in one message.
Additional context
This problem occurs because when getting the key value from the parameters field, if it is there, then JsonElements
is explicitly cast to the MutableList<JsonElement>
type, which is absolutely wrong. When the value is not yet in parameters by key, then everything is successful.
The wrong type conversion is happening here
The type of value for TgAction is here
I would like to propose such a solution for this place
(parameters[fieldName] ?: JsonArray(listOf())).jsonArray.toMutableList()
).also { it.add(entity.encodeWith(MessageEntity.serializer())) }.let { JsonArray(it) }
Thank your report!
Yeah few days ago I also found this bug,
already fixed in dev branch but since was busy last days I had no time to release :)
released in 5.3.2
now