Skip to content

Prohibited use of elements other than JsonObject in JsonTransformingSerializer with polymorphic serialization #2715

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package kotlinx.serialization


import kotlinx.serialization.json.*
import kotlinx.serialization.modules.SerializersModule
import kotlinx.serialization.modules.polymorphic
import kotlinx.serialization.test.*
import kotlin.test.*

class JsonElementPolymorphicErrorTest : JsonTestBase() {

@Serializable
abstract class Abstract

@Serializable
data class IntChild(val value: Int) : Abstract()

@Serializable
data class CollectionChild(val value: Int) : Abstract()

@Serializable
data class Holder(val value: Abstract)

private val format = Json {
prettyPrint = false
serializersModule = SerializersModule {
polymorphic(Abstract::class) {
subclass(IntChild::class, IntChildSerializer)
subclass(CollectionChild::class, CollectionChildSerializer)
}
}
}

object IntChildSerializer : JsonTransformingSerializer<IntChild>(serializer()) {
override fun transformSerialize(element: JsonElement): JsonElement {
return element.jsonObject.getValue("value")
}
}

object CollectionChildSerializer : JsonTransformingSerializer<CollectionChild>(serializer()) {
override fun transformSerialize(element: JsonElement): JsonElement {
val value = element.jsonObject.getValue("value")
return JsonArray(listOf(value))
}
}

@Test
fun test() = parametrizedTest { mode ->
assertFailsWithMessage<SerializationException>("Class with serial name kotlinx.serialization.JsonElementPolymorphicErrorTest.IntChild cannot be serialized polymorphically because it is represented as JsonLiteral. Make sure that its JsonTransformingSerializer returns JsonObject, so class discriminator can be added to it") {
format.encodeToString(
Holder.serializer(),
Holder(IntChild(42)),
mode
)
}

assertFailsWithMessage<SerializationException>("Class with serial name kotlinx.serialization.JsonElementPolymorphicErrorTest.CollectionChild cannot be serialized polymorphically because it is represented as JsonArray. Make sure that its JsonTransformingSerializer returns JsonObject, so class discriminator can be added to it") {
format.encodeToString(
Holder.serializer(),
Holder(CollectionChild(42)),
mode
)
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,7 @@ internal fun SerialDescriptor.classDiscriminator(json: Json): String {
return json.configuration.classDiscriminator
}

internal fun throwJsonElementPolymorphicException(serialName: String?, element: JsonElement): Nothing {
throw JsonEncodingException("Class with serial name $serialName cannot be serialized polymorphically because it is represented as ${element::class.simpleName}. Make sure that its JsonTransformingSerializer returns JsonObject, so class discriminator can be added to it.")
}

Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ internal class StreamingJsonEncoder(
}

override fun encodeJsonElement(element: JsonElement) {
if (polymorphicDiscriminator != null && element !is JsonObject) {
throwJsonElementPolymorphicException(polymorphicSerialName, element)
}
encodeSerializableValue(JsonElementSerializer, element)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ private sealed class AbstractJsonTreeEncoder(
descriptor.getJsonElementName(json, index)

override fun encodeJsonElement(element: JsonElement) {
if (polymorphicDiscriminator != null && element !is JsonObject) {
throwJsonElementPolymorphicException(polymorphicSerialName, element)
}
encodeSerializableValue(JsonElementSerializer, element)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,9 @@ private class DynamicObjectEncoder(
}

override fun encodeJsonElement(element: JsonElement) {
if (polymorphicDiscriminator != null && element !is JsonObject) {
throwJsonElementPolymorphicException(polymorphicSerialName, element)
}
encodeSerializableValue(JsonElementSerializer, element)
}

Expand Down