mongodb / mongodb/mongo-java-driver

Regression with serializing double values which could be represented as ints (e.g. 3.0)

Open
#2,031 0 comments 0 reactions 0 assignees View on GitHub

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.