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

Enhance KotlinSerializer with value codecs for widening primitive conversion. #1301

Merged
merged 11 commits into from
Jul 16, 2024

Conversation

vbabanin
Copy link
Member

@vbabanin vbabanin requested review from a team and rozza and removed request for a team January 31, 2024 01:29
@vbabanin vbabanin self-assigned this Jan 31, 2024
@rozza
Copy link
Member

rozza commented Jan 31, 2024

Kotlinx Json has a configuration to allow for lenient parsing: https://github.com/Kotlin/kotlinx.serialization/blob/master/docs/json.md#lenient-parsing

Should this be configurable?

@jyemin
Copy link
Collaborator

jyemin commented Jun 17, 2024

@rozza I think in this case it's better to be consistent with other codecs and also with the way numeric values are treated in MongoDB is basically interchangeable and compatible with each other, so long as there is no loss of precision or magnitude.

@jyemin
Copy link
Collaborator

jyemin commented Jun 17, 2024

@vbabanin can this come out of Draft now?

JAVA-5303
@vbabanin vbabanin marked this pull request as ready for review July 1, 2024 22:42
<!-- MongoDB status: "False Positive", SpotBugs rank: 17 -->
<Class name="org.bson.codecs.kotlinx.KotlinSerializerCodec$Companion"/>
<Method name="~.*validateAnnotations.*"/>
<Bug pattern="BC_BAD_CAST_TO_ABSTRACT_COLLECTION"/>
Copy link
Member Author

@vbabanin vbabanin Jul 2, 2024

Choose a reason for hiding this comment

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

The warning is triggered by an implicit cast generated during Kotlin to Java bytecode compilation, which is not directly controllable in the source Kotlin code. As this casting is safely handled by the Kotlin compiler and does not reflect an actual error in our Kotlin source, we can suppress this warning.

Copy link
Member

@rozza rozza Jul 9, 2024

Choose a reason for hiding this comment

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

This still needed?

@@ -34,8 +34,18 @@ import org.bson.BsonReader
import org.bson.BsonReaderMark
import org.bson.BsonType
import org.bson.BsonValue
import org.bson.codecs.BooleanCodec
Copy link
Member

Choose a reason for hiding this comment

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

I thought we discussed not using codecs (so to not confuse the two concepts of codecs and kserializers. Instead use the NumberCodecHelper directly (which is what the numeric codecs are doing in decode anyway).

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the reminder. We did discuss prioritizing clarity by not mixing codecs with serializers. I've now implemented the changes to use the codec helpers directly as we discussed.

@vbabanin vbabanin requested a review from rozza July 9, 2024 01:04
Copy link
Member

@rozza rozza left a comment

Choose a reason for hiding this comment

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

Looking good - just need to check the bson types and error accordingly.

}

public static char decodeChar(final BsonReader reader) {
String string = reader.readString();
Copy link
Member

Choose a reason for hiding this comment

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

Lets add a check if the bsonType isnt a string type.

throw new BsonInvalidOperationException(format("Invalid string type, found: %s", bsonType));

Copy link
Member Author

@vbabanin vbabanin Jul 11, 2024

Choose a reason for hiding this comment

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

The method readString internally calls verifyBSONType, which already includes a type check and throws a BsonInvalidOperationException if the types don't match: link to code. The current exception message is: 'methodName can only be called when CurrentBSONType is X, not when CurrentBSONType is Y.'

Are you suggesting we add additional check to override this exception message?

Copy link
Member

Choose a reason for hiding this comment

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

Adding the check would make it inline with the NumberCodecHelper.

Copy link
Member Author

@vbabanin vbabanin Jul 12, 2024

Choose a reason for hiding this comment

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

Thank you for clarification! I have added a check for BsonType.STRING.

override fun decodeDouble(): Double = NumberCodecHelper.decodeDouble(reader)
override fun decodeInt(): Int = NumberCodecHelper.decodeInt(reader)
override fun decodeLong(): Long = NumberCodecHelper.decodeLong(reader)
override fun decodeString(): String = reader.readString()
Copy link
Member

Choose a reason for hiding this comment

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

Lets keep the readOrThrow({ reader.readString() }, BsonType.STRING) so that users know if an invalid type is present for a string field.

@@ -176,7 +181,7 @@ internal open class DefaultBsonDecoder(
return reader.state != AbstractBsonReader.State.END_OF_DOCUMENT && reader.currentBsonType != BsonType.NULL
}

override fun decodeObjectId(): ObjectId = readOrThrow({ reader.readObjectId() }, BsonType.OBJECT_ID)
override fun decodeObjectId(): ObjectId = reader.readObjectId()
Copy link
Member

Choose a reason for hiding this comment

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

Lets keep the readOrThrow({ reader.readObjectId() }, BsonType.OBJECT_ID) so that users know if an invalid type is present for the objectId field.

@vbabanin vbabanin requested a review from rozza July 11, 2024 07:43
Copy link
Member

@rozza rozza left a comment

Choose a reason for hiding this comment

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

Just a couple of minors

}

public static char decodeChar(final BsonReader reader) {
String string = reader.readString();
Copy link
Member

Choose a reason for hiding this comment

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

Adding the check would make it inline with the NumberCodecHelper.

@vbabanin vbabanin requested a review from rozza July 12, 2024 07:04
Copy link
Member

@rozza rozza left a comment

Choose a reason for hiding this comment

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

LGTM!

@vbabanin vbabanin merged commit 18a6c9c into mongodb:master Jul 16, 2024
59 checks passed
rozza pushed a commit to rozza/mongo-java-driver that referenced this pull request Jul 17, 2024
vbabanin added a commit to vbabanin/mongo-java-driver that referenced this pull request Aug 6, 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