RocketChat / Rocket.Chat.Kotlin.SDK

Rocket.Chat's Kotlin SDK (REST & WebSocket abstractions)
MIT License
66 stars 62 forks source link

Nesting Problem when sending messages with attachment #265

Open GJKrupa opened 4 years ago

GJKrupa commented 4 years ago

Using v3.5.1, I'm sending a message after connection using the following Kotlin code:

                client.chatRooms().update.find { it.name == "bot-test" }?.id?.also {
                    client.sendMessage(
                        UUID.randomUUID().toString(),
                        roomId = it,
                        message = "[BOT] Connnected",
                        emoji = ":robot:",
                        alias = "bot",
                        attachments = listOf(Attachment("Hello"))
                    )
                }

This fails with the following exception from Moshi:

Exception in thread "DefaultDispatcher-worker-2" java.lang.IllegalStateException: Nesting problem.
    at com.squareup.moshi.JsonUtf8Writer.close(JsonUtf8Writer.java:133)
    at com.squareup.moshi.JsonUtf8Writer.endArray(JsonUtf8Writer.java:90)
    at com.squareup.moshi.CollectionJsonAdapter.toJson(CollectionJsonAdapter.java:87)
    at com.squareup.moshi.CollectionJsonAdapter$2.toJson(CollectionJsonAdapter.java:53)
    at com.squareup.moshi.JsonAdapter$2.toJson(JsonAdapter.java:144)
    at chat.rocket.core.internal.model.KotshiSendMessageBodyJsonAdapter.toJson(KotshiSendMessageBodyJsonAdapter.java:59)
    at chat.rocket.core.internal.model.KotshiSendMessageBodyJsonAdapter.toJson(KotshiSendMessageBodyJsonAdapter.java:19)
    at com.squareup.moshi.ClassJsonAdapter$FieldBinding.write(ClassJsonAdapter.java:201)
    at com.squareup.moshi.ClassJsonAdapter.toJson(ClassJsonAdapter.java:170)
    at com.squareup.moshi.JsonAdapter$2.toJson(JsonAdapter.java:144)
    at com.squareup.moshi.JsonAdapter.toJson(JsonAdapter.java:52)
    at com.squareup.moshi.JsonAdapter.toJson(JsonAdapter.java:58)
    at chat.rocket.core.internal.rest.MessageKt$sendMessage$2.invokeSuspend(Message.kt:53)
    at chat.rocket.core.internal.rest.MessageKt$sendMessage$2.invoke(Message.kt)
    at kotlinx.coroutines.intrinsics.UndispatchedKt.startUndispatchedOrReturn(Undispatched.kt:91)
    at kotlinx.coroutines.BuildersKt__Builders_commonKt.withContext(Builders.common.kt:146)
    at kotlinx.coroutines.BuildersKt.withContext(Unknown Source)
    at chat.rocket.core.internal.rest.MessageKt.sendMessage(Message.kt:48)
    at chat.rocket.core.internal.rest.MessageKt.sendMessage$default(Message.kt:47)
    at uk.gov.homeoffice.themis.connectors.RocketChat$chatClient$job$1$1.invokeSuspend(RocketChat.kt:63)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(Dispatched.kt:233)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:594)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.access$runSafely(CoroutineScheduler.kt:60)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:742)

As this is a bot rather than an Android app, I'm also importing org.json:json:20190722

GJKrupa commented 3 years ago

This appears to be due to a missing call to endObject() in AttachmentAdapter.toJson()