Skip to content

Commit 51f79db

Browse files
cmelchiorSpace Team
authored andcommitted
Fix error message subclass serialization
The @SerialName annotation are not inherited by subclasses that override the property. This is by design. So this PR makes sure that the annotation is present on all relevant classes. Merge-request: KDS-MR-75 Merged-by: Christian Melchior <[email protected]>
1 parent 6a0d8c6 commit 51f79db

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

jupyter-lib/shared-compiler/src/main/kotlin/org/jetbrains/kotlinx/jupyter/messaging/Common.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ sealed class MessageReplyContent(
4242
@Serializable
4343
abstract class OkReplyContent : MessageReplyContent(MessageStatus.OK)
4444

45+
// @SerialName annotations are not inherited by subclasses, they must be manually added on every subclass
46+
// They are just here for documentation purposes. See https://github.com/Kotlin/kotlinx.serialization/issues/2054
4547
@Serializable
4648
abstract class ErrorReplyContent : MessageReplyContent(MessageStatus.ERROR) {
4749
@SerialName("ename")

jupyter-lib/shared-compiler/src/main/kotlin/org/jetbrains/kotlinx/jupyter/messaging/MessageTypes.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ class ExecuteAbortReply : AbortReplyContent(), ExecuteReply
195195
class ExecuteErrorReply(
196196
@SerialName("execution_count")
197197
val executionCount: ExecutionCount,
198+
@SerialName("ename")
198199
override val name: String,
200+
@SerialName("evalue")
199201
override val value: String,
200202
/**
201203
* The full error, line by line, that will be displayed to the user.
@@ -248,7 +250,9 @@ class CompleteRequest(
248250

249251
@Serializable
250252
class CompleteErrorReply(
253+
@SerialName("ename")
251254
override val name: String,
255+
@SerialName("evalue")
252256
override val value: String,
253257
override val traceback: List<String>,
254258
) : ErrorReplyContent()
@@ -323,7 +327,9 @@ class UpdateClientMetadataSuccessReply : OkReplyContent(), UpdateClientMetadataR
323327

324328
@Serializable
325329
class UpdateClientMetadataErrorReply(
330+
@SerialName("ename")
326331
override val name: String,
332+
@SerialName("evalue")
327333
override val value: String,
328334
override val traceback: List<String>,
329335
) : ErrorReplyContent(), UpdateClientMetadataReply {

src/test/kotlin/org/jetbrains/kotlinx/jupyter/test/protocol/SerializerTests.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jetbrains.kotlinx.jupyter.test.protocol
22

33
import io.kotest.matchers.shouldBe
4+
import io.kotest.matchers.string.shouldContain
45
import io.kotest.matchers.types.shouldBeTypeOf
56
import kotlinx.serialization.json.Json
67
import org.jetbrains.kotlinx.jupyter.messaging.UpdateClientMetadataErrorReply
@@ -16,6 +17,10 @@ class SerializerTests {
1617
val exception = Exception("BOOM")
1718
val originalMessage = UpdateClientMetadataErrorReply(exception)
1819
val json = Json.encodeToString(UpdateClientMetadataReplySerializer, originalMessage)
20+
21+
json shouldContain "\"ename\":\"Exception\""
22+
json shouldContain "\"evalue\":\"BOOM\""
23+
1924
val deserializedMessage = Json.decodeFromString(UpdateClientMetadataReplySerializer, json)
2025
deserializedMessage.shouldBeTypeOf<UpdateClientMetadataErrorReply>()
2126
originalMessage.status shouldBe deserializedMessage.status

0 commit comments

Comments
 (0)