mongodb / mongodb/mongo-java-driver
Regression with serializing double values which could be represented as ints (e.g. 3.0)
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 2.7k
- Forks
- 1.5k
- Avg merge
- 2d 2h
- Merged PRs (30d)
- 10
Description
https://github.com/mongodb/mongo-java-driver/pull/1937 introduced new behavior which seems to have regressions when serializing double values like 3.0.
When reading these values back, the type seems to have magically changed to int.
class JsonBsonEncoderNumberTypeReproducerTest {
@Serializable
private data class Container(val payload: JsonObject)
@Test
fun `JSON numbers keep their BSON type`() {
//language=JSON
val json = """{"integral": 3.0, "fractional": 15.9, "scientific": 1.0E20}"""
val container = Container(Json.parseToJsonElement(json).jsonObject)
val codec = KotlinSerializerCodec.create(
Container::class,
serializer<Container>(),
EmptySerializersModule(),
BsonConfiguration(),
)
val encoded = BsonDocument()
codec.encode(BsonDocumentWriter(encoded), container, EncoderContext.builder().build())
val decoded: Container = codec.decode(BsonDocumentReader(encoded), DecoderContext.builder().build())
val payload: BsonDocument = encoded.getDocument("payload")
assertEquals(BsonType.DOUBLE, payload.getValue("integral").bsonType) //FAILS: INT32
assertEquals(BsonType.DOUBLE, payload.getValue("scientific").bsonType) //FAILS: DECIMAL128
assertEquals(BsonType.DOUBLE, payload.getValue("fractional").bsonType)
assertEquals(container, decoded) //FAILS: {"integral":3,"fractional":15.9,"scientific":1E+20}
}
}
If course I have a much more complex use case. I am serializing Kotlin data classes with double properties.
But this should be the smallest possible reproducer for the issue.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the JsonBsonEncoderNumberTypeReproducerTest example in this issue and compare the number handling introduced by pull request 1937. Reproduce the failing BSON type assertions and round-trip equality check; done means integral and scientific JSON numbers retain DOUBLE BSON types and the decoded container equals the original.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java, kotlin, mongodb
- Domain
- database
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 68/100