Skip to content

Support discriminators not being the first field when decoding#1324

Merged
rozza merged 5 commits into
mongodb:masterfrom
rozza:JAVA-5304
Mar 15, 2024
Merged

Support discriminators not being the first field when decoding#1324
rozza merged 5 commits into
mongodb:masterfrom
rozza:JAVA-5304

Conversation

@rozza

@rozza rozza commented Mar 4, 2024

Copy link
Copy Markdown
Member

@rozza rozza requested review from a team, jyemin, stIncMale and vbabanin and removed request for a team and stIncMale March 4, 2024 15:14
}

@Test
fun testDataClassWithSimpleValuesFieldOrdering() {

Copy link
Copy Markdown
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 actual =
codec.decode(
JsonReader(
"""{"name": "string", "_id": {$oid: "${objectId.toHexString()}"},

Copy link
Copy Markdown
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.

configuration: BsonConfiguration
) : DefaultBsonDecoder(reader, serializersModule, configuration) {
private var index = 0
private var mark: BsonReaderMark? = reader.mark

Copy link
Copy Markdown
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
Copy Markdown
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.

@robbamberg

Copy link
Copy Markdown

@rozza this seems a solution for the polymorphic serializer on root level (with _id field), but this is only part of the fix. Currently the polymorphic serializer is not called while inserting a document to Mongo, so without that also having a fix, this won't do much.

In driver-core in the Operations class we see that during insert of a document that the function documentToBsonDocument is called. It fetches the Codec for the class that is inserted (data class), but it should fetch the Codec of collection (basically the getCodec function). This is causing the wrong serializer to be used.

@rozza

rozza commented Mar 5, 2024

Copy link
Copy Markdown
Member Author

Hi @robbamberg - thanks for the follow up, that is a slightly different issue to the one being fixed here. I've added #1327 for that fix.

Comment thread bson-kotlinx/src/main/kotlin/org/bson/codecs/kotlinx/BsonDecoder.kt Outdated
Comment thread bson-kotlinx/src/main/kotlin/org/bson/codecs/kotlinx/BsonDecoder.kt
@rozza rozza requested a review from jyemin March 14, 2024 10:47

@jyemin jyemin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looking good, just one DRY-related suggestion.

Comment thread bson-kotlinx/src/main/kotlin/org/bson/codecs/kotlinx/BsonDecoder.kt Outdated
Comment thread bson-kotlinx/src/main/kotlin/org/bson/codecs/kotlinx/BsonDecoder.kt
@rozza rozza requested a review from jyemin March 15, 2024 11:42
@rozza rozza merged commit dc6c38b into mongodb:master Mar 15, 2024
@rozza rozza deleted the JAVA-5304 branch March 15, 2024 14:36
rozza added a commit to rozza/mongo-java-driver that referenced this pull request Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants