-
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 1 commit
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 | ||
|
|
@@ -125,7 +126,6 @@ internal open class DefaultBsonDecoder( | |
| return BsonArrayDecoder(reader, serializersModule, configuration) | ||
| } | ||
| is PolymorphicKind -> { | ||
| reader.readStartDocument() | ||
| return PolymorphicDecoder(reader, serializersModule, configuration) | ||
| } | ||
| is StructureKind.CLASS, | ||
|
|
@@ -213,13 +213,37 @@ private class PolymorphicDecoder( | |
| configuration: BsonConfiguration | ||
| ) : DefaultBsonDecoder(reader, serializersModule, configuration) { | ||
| private var index = 0 | ||
| private var mark: BsonReaderMark? = reader.mark | ||
|
||
|
|
||
| override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T = | ||
| deserializer.deserialize(DefaultBsonDecoder(reader, serializersModule, configuration)) | ||
| override fun <T> decodeSerializableValue(deserializer: DeserializationStrategy<T>): T { | ||
| mark?.let { | ||
|
Member
Author
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 -> { | ||
| 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.
Outdated
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 | ||
| } | ||
|
|
@@ -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() | ||
|
|
||
| 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() { | ||
|
Member
Author
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()}"}, | ||
|
Member
Author
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.