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

Fix bson-kotlinx encodeNullableSerializableValue null handling #1453

Merged
merged 4 commits into from
Jul 29, 2024

Conversation

rozza
Copy link
Member

@rozza rozza commented Jul 17, 2024

Ensures that the deferredElement name is reset as it does in encodeSerializableValue.
Test case ported to bson-kotlin

JAVA-5524

Ensures that the deferredElement name is reset correctly.
Test case ported to bson-kotlin

JAVA-5524
@rozza rozza requested review from a team and stIncMale and removed request for a team July 17, 2024 11:57
@@ -156,6 +156,8 @@ internal class DefaultBsonEncoder(
if (value != null || configuration.explicitNulls) {
encodeName(it)
super.encodeNullableSerializableValue(serializer, value)
} else {
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems worth it to add tests for the corresponding else clause in the encodeSerializableValue method just above (I removed the clause and no tests fails, so it's not covered).

Copy link
Member Author

Choose a reason for hiding this comment

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

I can't concoct a test that triggers this. That said it doesn't guarantee that there isn't a scenario where it could be triggered.

As we are skipping this value it makes sense to reset the defferred name if it exists.
However, I think I can clean up the usage.

Copy link
Member Author

Choose a reason for hiding this comment

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

@jyemin I cleaned up the usage and encapsulated the handling of deferred element names.

@@ -133,6 +135,20 @@ class DataClassCodecTest {
assertDecodesTo(withStoredNulls, dataClass)
}

@Test
fun testDataClassWithListThatLastItemDefaultsToNull() {
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 is just a copy from bson-kotlinx - to ensure the bson-kotlin library also works as expected.

@rozza rozza requested review from jyemin and removed request for stIncMale July 24, 2024 15:43
}
writer.writeNull()
}
override fun encodeNull() = writer.writeNull()
Copy link
Member Author

@rozza rozza Jul 24, 2024

Choose a reason for hiding this comment

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

Not sure how this was missed in a PR - but encoding null will never require the deferred element name - as encode(Nullable)SerializableValue will be used

@@ -206,7 +200,6 @@ internal class DefaultBsonEncoder(

private fun encodeName(value: Any) {
writer.writeName(value.toString())
deferredElementName = null
Copy link
Member Author

Choose a reason for hiding this comment

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

No longer required - as its cleaned up in the SerializableValue usages

}
}
?: super.encodeSerializableValue(serializer, value)
deferredElementHandler.with(
Copy link
Contributor

Choose a reason for hiding this comment

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

It's unclear to me why value is

?: super.encodeSerializableValue(serializer, value)
deferredElementHandler.with(
{
if (value != null || configuration.explicitNulls) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a point of clarification:

It's unclear to me why value is ever null here. If it is, why isn't encodeNullableSerializableValue called instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this surprised me also. The only test that triggers this is via the use of generics which aren't marked as nullable.

@Serializable data class Box<T>(val boxed: T)

@Serializable data class DataClassWithNullableGeneric(val box: Box<String?>)

Encoding: DataClassWithNullableGeneric(Box(null)) causes this to branch to trigger.

Its questionable that this code is reasonable / bug free. However, it appears to be marked as designed: https://youtrack.jetbrains.com/issue/KT-66206

I've added a comment to the code.

@rozza rozza requested a review from jyemin July 25, 2024 09:15
Copy link
Contributor

@jyemin jyemin left a comment

Choose a reason for hiding this comment

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

Thanks. LGTM

@rozza rozza merged commit ab72460 into mongodb:master Jul 29, 2024
56 of 59 checks passed
rozza added a commit to rozza/mongo-java-driver that referenced this pull request Jul 29, 2024
…godb#1453)

Ensures that the deferredElement name is reset correctly.
Test case ported to bson-kotlin

JAVA-5524
rozza added a commit to rozza/mongo-java-driver that referenced this pull request Aug 21, 2024
…godb#1453)

Ensures that the deferredElement name is reset correctly.
Test case ported to bson-kotlin

JAVA-5524
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.

2 participants