[JAVA-3916] Driver decodes BsonInt64 as Integer Created: 21/Dec/20  Updated: 28/Oct/23  Resolved: 13/Jan/21

Status: Closed
Project: Java Driver
Component/s: Scala
Affects Version/s: 4.1.0
Fix Version/s: 4.2.0

Type: Bug Priority: Major - P3
Reporter: Andrii Abramov Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

```
docker run --rm -p 27017:27017 mongo:4.4.2
docker run --rm -p 27017:27017 nikore/mongo:4.0-v2
```

```
openjdk version "1.8.0_265"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_265-b01)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.265-b01, mixed mode)
```

```
Darwin MBP15-9814af 18.7.0 Darwin Kernel Version 18.7.0: Tue Nov 10 00:07:31 PST 2020; root:xnu-4903.278.51~1/RELEASE_X86_64 x86_64
```

```
scalaVersion 2.12.12
sbt 1.4.5
```


Attachments: PNG File image-2020-12-21-12-19-31-381.png    

 Description   

 

Driver fails to read `BsonInt64` as Long and results in ClassCastException:

 

Minimal reproducible example:

https://gist.github.com/aaabramov/89625595893d0ff513d1512b43a21caa

 

What happens here is that driver reads `field` as an `Integer` and puts it into `org.bson.Document#documentAsMap` then tries to class it as a `Long` in `org.bson.Document#getLong`.

 

And the interesting thing is we'll try to persist `coll.insertOne(Document("field" -> BsonInt64(Long.MaxValue))).toFuture()` everything would work just fine. So seems that driver chooses resulting type based on its value?

 

It was working fine with driver `"org.mongodb.scala" %% "mongo-scala-driver" % "2.7.0"`

 



 Comments   
Comment by Githook User [ 13/Jan/21 ]

Author:

{'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}

Message: Scala: Ensure implicit conversion keeps type fidelity (#627)

When converting from a BsonDocument to org.bson.Document
the scala driver previously used json. The implementation
should not rely on the json output format, but rather use
the Codecs to do the conversion.

JAVA-3916
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/36d0e7fcf8b8d90ca1a739caaaa53dfb69cad724

Comment by Ross Lawley [ 11/Jan/21 ]

https://github.com/mongodb/mongo-java-driver/pull/627

Comment by Jeffrey Yemin [ 21/Dec/20 ]

Hi aabrasha@gmail.com

Thanks for the bug report. We'll have a look soon and get back to you.

Thanks,
Jeff

Comment by Andrii Abramov [ 21/Dec/20 ]

Seems that type is lost during implicit conversion between scala & java `Document` in `org.mongodb.scala#documentToUntypedDocument`.

It is implemented with ScalaDocument=>JSON=>JavaDocument conversion and JsonReader decides whether to pick Long or Integer.

I would expect the driver to return strict type as it was before.

Generated at Thu Feb 08 09:00:46 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.