Skip to content
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

Support discriminators not being the first field when decoding #1324

Merged
merged 5 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -31,6 +31,7 @@ import kotlinx.serialization.modules.SerializersModule
import org.bson.AbstractBsonReader
import org.bson.BsonInvalidOperationException
import org.bson.BsonReader
import org.bson.BsonReaderMark
import org.bson.BsonType
import org.bson.BsonValue
import org.bson.codecs.BsonValueCodec
Expand Down Expand Up @@ -125,7 +126,6 @@ internal open class DefaultBsonDecoder(
return BsonArrayDecoder(reader, serializersModule, configuration)
}
is PolymorphicKind -> {
reader.readStartDocument()
jyemin marked this conversation as resolved.
Show resolved Hide resolved
return PolymorphicDecoder(reader, serializersModule, configuration)
}
is StructureKind.CLASS,
Expand Down Expand Up @@ -213,13 +213,37 @@ private class PolymorphicDecoder(
configuration: BsonConfiguration
) : DefaultBsonDecoder(reader, serializersModule, configuration) {
private var index = 0
private var mark: BsonReaderMark? = reader.mark
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here a mark is set allowing the PolymorphicDecoder to iterate to the discriminator field instead of expecting it to be the first field.


override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T =
deserializer.deserialize(DefaultBsonDecoder(reader, serializersModule, configuration))
override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T {
mark?.let {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clears the mark on the first decoding of values.

it.reset()
mark = null
}
return deserializer.deserialize(DefaultBsonDecoder(reader, serializersModule, configuration))
}

override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
var found = false
return when (index) {
0 -> index++
0 -> {
reader.readStartDocument()
reader.readBsonType()
while (reader.state != AbstractBsonReader.State.END_OF_DOCUMENT) {
if (reader.readName() == configuration.classDiscriminator) {
found = true
break
}
reader.skipValue()
reader.readBsonType()
jyemin marked this conversation as resolved.
Show resolved Hide resolved
}
if (!found) {
throw SerializationException(
"Missing required discriminator field `${configuration.classDiscriminator}` " +
"for polymorphic class: `${descriptor.serialName}`.")
}
index++
}
1 -> index++
else -> DECODE_DONE
}
Expand Down Expand Up @@ -251,6 +275,8 @@ private class BsonDocumentDecoder(
"Invalid key type for ${descriptor.serialName}. Expected STRING or ENUM but found: `${keyKind}`")
}

System.err.println(descriptor.serialName)

if (!isKey) {
isKey = true
val nextType = reader.readBsonType()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ import org.bson.codecs.kotlinx.samples.DataClassOpen
import org.bson.codecs.kotlinx.samples.DataClassOpenA
import org.bson.codecs.kotlinx.samples.DataClassOpenB
import org.bson.codecs.kotlinx.samples.DataClassParameterized
import org.bson.codecs.kotlinx.samples.DataClassSealedInterface
import org.bson.codecs.kotlinx.samples.DataClassWithSimpleValues
import org.bson.codecs.kotlinx.samples.SealedInterface
import org.bson.conversions.Bson
import org.bson.json.JsonReader
import org.bson.types.ObjectId
import org.junit.jupiter.api.Test

class KotlinSerializerCodecProviderTest {
Expand Down Expand Up @@ -75,6 +79,41 @@ class KotlinSerializerCodecProviderTest {
assertEquals(DataClassWithSimpleValues::class.java, codec.encoderClass)
}

@Test
fun testDataClassWithSimpleValuesFieldOrdering() {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functionality was already supported but added a regression test.

val codec = MongoClientSettings.getDefaultCodecRegistry().get(DataClassWithSimpleValues::class.java)
val expected = DataClassWithSimpleValues('c', 0, 1, 22, 42L, 4.0f, 4.2, true, "String")

val numberLong = "\$numberLong"
val actual =
codec.decode(
JsonReader(
"""{"boolean": true, "byte": 0, "char": "c", "double": 4.2, "float": 4.0, "int": 22,
|"long": {"$numberLong": "42"}, "short": 1, "string": "String"}"""
.trimMargin()),
DecoderContext.builder().build())

assertEquals(expected, actual)
}

@Test
fun testDataClassSealedFieldOrdering() {
val codec = MongoClientSettings.getDefaultCodecRegistry().get(SealedInterface::class.java)

val objectId = ObjectId("111111111111111111111111")
val oid = "\$oid"
val expected = DataClassSealedInterface(objectId, "string")
val actual =
codec.decode(
JsonReader(
"""{"name": "string", "_id": {$oid: "${objectId.toHexString()}"},
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here all the fields are out of order and this confirms it does support the scenario where the discriminator isn't the first field.

|"_t": "org.bson.codecs.kotlinx.samples.DataClassSealedInterface"}"""
.trimMargin()),
DecoderContext.builder().build())

assertEquals(expected, actual)
}

@OptIn(ExperimentalSerializationApi::class)
@Test
fun shouldAllowOverridingOfSerializersModuleAndBsonConfigurationInConstructor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,20 +84,22 @@ import org.bson.codecs.kotlinx.samples.DataClassWithSequence
import org.bson.codecs.kotlinx.samples.DataClassWithSimpleValues
import org.bson.codecs.kotlinx.samples.DataClassWithTriple
import org.bson.codecs.kotlinx.samples.Key
import org.bson.codecs.kotlinx.samples.SealedInterface
import org.bson.codecs.kotlinx.samples.ValueClass
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

@OptIn(ExperimentalSerializationApi::class)
class KotlinSerializerCodecTest {
private val numberLong = "\$numberLong"
private val oid = "\$oid"
private val emptyDocument = "{}"
private val altConfiguration =
BsonConfiguration(encodeDefaults = false, classDiscriminator = "_t", explicitNulls = true)

private val allBsonTypesJson =
"""{
| "id": {"${'$'}oid": "111111111111111111111111"},
| "id": {"$oid": "111111111111111111111111"},
| "arrayEmpty": [],
| "arraySimple": [{"${'$'}numberInt": "1"}, {"${'$'}numberInt": "2"}, {"${'$'}numberInt": "3"}],
| "arrayComplex": [{"a": {"${'$'}numberInt": "1"}}, {"a": {"${'$'}numberInt": "2"}}],
Expand Down Expand Up @@ -679,6 +681,17 @@ class KotlinSerializerCodecTest {
val codec = KotlinSerializerCodec.create<DataClassWithFailingInit>()
codec?.decode(BsonDocumentReader(data), DecoderContext.builder().build())
}

val exception =
assertThrows<SerializationException>("Missing discriminator") {
val data = BsonDocument.parse("""{"_id": {"$oid": "111111111111111111111111"}, "name": "string"}""")
val codec = KotlinSerializerCodec.create<SealedInterface>()
codec?.decode(BsonDocumentReader(data), DecoderContext.builder().build())
}
assertEquals(
"Missing required discriminator field `_t` for polymorphic class: " +
"`org.bson.codecs.kotlinx.samples.SealedInterface`.",
exception.message)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ data class DataClassOptionalBsonValues(

@Serializable @SerialName("C") data class DataClassSealedC(val c: String) : DataClassSealed()

@Serializable
sealed interface SealedInterface {
val name: String
}

@Serializable
data class DataClassSealedInterface(@Contextual @SerialName("_id") val id: ObjectId, override val name: String) :
SealedInterface

@Serializable data class DataClassListOfSealed(val items: List<DataClassSealed>)

interface DataClassOpen
Expand Down