-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Support discriminators not being the first field when decoding #1324
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
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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() | ||
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) { | ||
|
@@ -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() | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
|
@@ -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 | ||
|
@@ -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() | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -75,6 +79,41 @@ class KotlinSerializerCodecProviderTest { | |
assertEquals(DataClassWithSimpleValues::class.java, codec.encoderClass) | ||
} | ||
|
||
@Test | ||
fun testDataClassWithSimpleValuesFieldOrdering() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()}"}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() { | ||
|
Uh oh!
There was an error while loading. Please reload this page.