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 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
110 changes: 87 additions & 23 deletions bson-kotlinx/src/main/kotlin/org/bson/codecs/kotlinx/BsonDecoder.kt
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 @@ -119,29 +120,14 @@ internal open class DefaultBsonDecoder(

@Suppress("ReturnCount")
override fun beginStructure(descriptor: SerialDescriptor): CompositeDecoder {
when (descriptor.kind) {
is StructureKind.LIST -> {
reader.readStartArray()
return BsonArrayDecoder(reader, serializersModule, configuration)
}
is PolymorphicKind -> {
reader.readStartDocument()
jyemin marked this conversation as resolved.
Show resolved Hide resolved
return PolymorphicDecoder(reader, serializersModule, configuration)
}
return when (descriptor.kind) {
is StructureKind.LIST -> BsonArrayDecoder(descriptor, reader, serializersModule, configuration)
is PolymorphicKind -> PolymorphicDecoder(descriptor, reader, serializersModule, configuration)
is StructureKind.CLASS,
StructureKind.OBJECT -> {
val current = reader.currentBsonType
if (current == null || current == BsonType.DOCUMENT) {
reader.readStartDocument()
}
}
is StructureKind.MAP -> {
reader.readStartDocument()
return BsonDocumentDecoder(reader, serializersModule, configuration)
}
StructureKind.OBJECT -> BsonDocumentDecoder(descriptor, reader, serializersModule, configuration)
is StructureKind.MAP -> MapDecoder(descriptor, reader, serializersModule, configuration)
else -> throw SerializationException("Primitives are not supported at top-level")
}
return DefaultBsonDecoder(reader, serializersModule, configuration)
}

override fun endStructure(descriptor: SerialDescriptor) {
Expand Down Expand Up @@ -194,10 +180,23 @@ internal open class DefaultBsonDecoder(

@OptIn(ExperimentalSerializationApi::class)
private class BsonArrayDecoder(
descriptor: SerialDescriptor,
reader: AbstractBsonReader,
serializersModule: SerializersModule,
configuration: BsonConfiguration
) : DefaultBsonDecoder(reader, serializersModule, configuration) {

init {
reader.currentBsonType?.let {
if (it != BsonType.ARRAY) {
throw SerializationException(
"Invalid data for `${descriptor.kind}` expected a bson array found: " + "${reader.currentBsonType}")
}
}

reader.readStartArray()
}

private var index = 0
override fun decodeElementIndex(descriptor: SerialDescriptor): Int {
val nextType = reader.readBsonType()
Expand All @@ -208,18 +207,52 @@ private class BsonArrayDecoder(

@OptIn(ExperimentalSerializationApi::class)
private class PolymorphicDecoder(
descriptor: SerialDescriptor,
reader: AbstractBsonReader,
serializersModule: SerializersModule,
configuration: BsonConfiguration
) : DefaultBsonDecoder(reader, serializersModule, configuration) {
private var index = 0
private var mark: BsonReaderMark?

override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T =
deserializer.deserialize(DefaultBsonDecoder(reader, serializersModule, configuration))
init {
mark = reader.mark
reader.currentBsonType?.let {
if (it != BsonType.DOCUMENT) {
throw SerializationException(
"Invalid data for `${descriptor.serialName}` expected a bson document found: " +
"${reader.currentBsonType}")
}
}
reader.readStartDocument()
}

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 -> {
while (reader.readBsonType() != BsonType.END_OF_DOCUMENT) {
if (reader.readName() == configuration.classDiscriminator) {
found = true
break
}
reader.skipValue()
}
if (!found) {
throw SerializationException(
"Missing required discriminator field `${configuration.classDiscriminator}` " +
"for polymorphic class: `${descriptor.serialName}`.")
}
index++
}
1 -> index++
else -> DECODE_DONE
}
Expand All @@ -228,6 +261,26 @@ private class PolymorphicDecoder(

@OptIn(ExperimentalSerializationApi::class)
private class BsonDocumentDecoder(
descriptor: SerialDescriptor,
reader: AbstractBsonReader,
serializersModule: SerializersModule,
configuration: BsonConfiguration
) : DefaultBsonDecoder(reader, serializersModule, configuration) {
init {
reader.currentBsonType?.let {
if (it != BsonType.DOCUMENT) {
throw SerializationException(
"Invalid data for `${descriptor.serialName}` expected a bson document found: " +
"${reader.currentBsonType}")
}
}
reader.readStartDocument()
}
}

@OptIn(ExperimentalSerializationApi::class)
private class MapDecoder(
descriptor: SerialDescriptor,
reader: AbstractBsonReader,
serializersModule: SerializersModule,
configuration: BsonConfiguration
Expand All @@ -236,6 +289,17 @@ private class BsonDocumentDecoder(
private var index = 0
private var isKey = false

init {
reader.currentBsonType?.let {
jyemin marked this conversation as resolved.
Show resolved Hide resolved
if (it != BsonType.DOCUMENT) {
throw SerializationException(
"Invalid data for `${descriptor.kind}` expected a bson document found: " +
"${reader.currentBsonType}")
}
}
reader.readStartDocument()
}

override fun decodeString(): String {
return if (isKey) {
reader.readName()
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,23 @@ 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)
@Suppress("LargeClass")
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 @@ -668,17 +671,49 @@ class KotlinSerializerCodecTest {
codec?.decode(BsonDocumentReader(data), DecoderContext.builder().build())
}

assertThrows<MissingFieldException>("Invalid complex types") {
val data = BsonDocument.parse("""{"_id": "myId", "embedded": 123}""")
val codec = KotlinSerializerCodec.create<DataClassWithEmbedded>()
codec?.decode(BsonDocumentReader(data), DecoderContext.builder().build())
}

assertThrows<IllegalArgumentException>("Failing init") {
val data = BsonDocument.parse("""{"id": "myId"}""")
val codec = KotlinSerializerCodec.create<DataClassWithFailingInit>()
codec?.decode(BsonDocumentReader(data), DecoderContext.builder().build())
}

var exception =
assertThrows<SerializationException>("Invalid complex types - document") {
val data = BsonDocument.parse("""{"_id": "myId", "embedded": 123}""")
val codec = KotlinSerializerCodec.create<DataClassWithEmbedded>()
codec?.decode(BsonDocumentReader(data), DecoderContext.builder().build())
}
assertEquals(
"Invalid data for `org.bson.codecs.kotlinx.samples.DataClassEmbedded` " +
"expected a bson document found: INT32",
exception.message)

exception =
assertThrows<SerializationException>("Invalid complex types - list") {
val data = BsonDocument.parse("""{"_id": "myId", "nested": 123}""")
val codec = KotlinSerializerCodec.create<DataClassListOfDataClasses>()
codec?.decode(BsonDocumentReader(data), DecoderContext.builder().build())
}
assertEquals("Invalid data for `LIST` expected a bson array found: INT32", exception.message)

exception =
assertThrows<SerializationException>("Invalid complex types - map") {
val data = BsonDocument.parse("""{"_id": "myId", "nested": 123}""")
val codec = KotlinSerializerCodec.create<DataClassMapOfDataClasses>()
codec?.decode(BsonDocumentReader(data), DecoderContext.builder().build())
}
assertEquals("Invalid data for `MAP` expected a bson document found: INT32", exception.message)

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