Skip to content

Commit 3cad3bc

Browse files
author
Roman Janusz
authored
Merge pull request #413 from pulewicz/accept-bson-ints-as-doubles
Accept ints as doubles in Bson inputs
2 parents 28f66d3 + 6d2bf0f commit 3cad3bc

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

commons-mongo/jvm/src/main/scala/com/avsystem/commons/mongo/BsonReaderInput.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,13 @@ class BsonReaderInput(br: BsonReader, override val legacyOptionEncoding: Boolean
3737
override def readTimestamp(): Long =
3838
expect(BsonType.DATE_TIME, br.readDateTime())
3939

40-
override def readDouble(): Double =
41-
expect(BsonType.DOUBLE, br.readDouble())
40+
override def readDouble(): Double = handleFailures {
41+
bsonType match {
42+
case BsonType.DOUBLE => br.readDouble()
43+
case BsonType.INT32 => br.readInt32().toDouble
44+
case _ => wrongType(BsonType.DOUBLE, BsonType.INT32)
45+
}
46+
}
4247

4348
override def readBinary(): Array[Byte] =
4449
expect(BsonType.BINARY, br.readBinaryData().getData)

commons-mongo/jvm/src/main/scala/com/avsystem/commons/mongo/BsonValueInput.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,13 @@ class BsonValueInput(bsonValue: BsonValue, override val legacyOptionEncoding: Bo
3333
override def readTimestamp(): Long =
3434
expect(BsonType.DATE_TIME, bsonValue.asDateTime().getValue)
3535

36-
def readDouble(): Double =
37-
expect(BsonType.DOUBLE, bsonValue.asDouble().getValue)
36+
override def readDouble(): Double = handleFailures {
37+
bsonType match {
38+
case BsonType.DOUBLE => bsonValue.asDouble().getValue
39+
case BsonType.INT32 => readInt().toDouble
40+
case _ => wrongType(BsonType.DOUBLE, BsonType.INT32)
41+
}
42+
}
3843

3944
def readBinary(): Array[Byte] =
4045
expect(BsonType.BINARY, bsonValue.asBinary().getData)

commons-mongo/jvm/src/test/scala/com/avsystem/commons/mongo/BsonInputOutputTest.scala

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import org.bson.types.Decimal128
99
import org.scalactic.source.Position
1010
import org.scalatest.funsuite.AnyFunSuite
1111
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
12-
1312
import java.nio.ByteBuffer
1413

14+
import com.avsystem.commons.serialization.GenCodec.ReadFailure
15+
1516
class BinaryBsonGenCodecRoundtripTest extends GenCodecRoundtripTest {
1617
type Raw = Array[Byte]
1718

@@ -75,6 +76,18 @@ class BsonValueGenCodecRoundtripTest extends GenCodecRoundtripTest {
7576
val result = input.readSimple().readLong()
7677
assert(result == 42L)
7778
}
79+
80+
test("Int32 to Double decoding") {
81+
val input = createInput(new BsonInt32(43))
82+
val result = input.readSimple().readDouble()
83+
assert(result == 43D)
84+
}
85+
86+
test("Int64 to Double decoding exception") {
87+
val input = createInput(new BsonInt64(44))
88+
89+
assertThrows[ReadFailure](input.readSimple().readDouble())
90+
}
7891
}
7992

8093
class BsonInputOutputTest extends AnyFunSuite with ScalaCheckPropertyChecks {
@@ -234,6 +247,26 @@ class BsonInputOutputTest extends AnyFunSuite with ScalaCheckPropertyChecks {
234247
}
235248
}
236249

250+
test("BsonBinaryReader Int types to Double decoding") {
251+
val document = new BsonDocument()
252+
.append("int", new BsonInt32(42))
253+
.append("long", new BsonInt64(42L))
254+
255+
val rawJson = document.toJson(JsonWriterSettings.builder.outputMode(JsonMode.EXTENDED).build)
256+
val input = new BsonReaderInput(new BsonBinaryReader(RawBsonDocument.parse(rawJson).getByteBuffer.asNIO()))
257+
258+
val objectInput = input.readObject()
259+
260+
val intField = objectInput.peekField("int")
261+
assert(intField.nonEmpty)
262+
assert(intField.get.readDouble() == 42D)
263+
264+
265+
val longField = objectInput.peekField("long")
266+
assert(longField.nonEmpty)
267+
assertThrows[ReadFailure](longField.get.readDouble())
268+
}
269+
237270
test("BsonBinaryReader type metadata") {
238271
forAll(SomethingPlain.gen) { sth =>
239272
val document = somethingToBson(sth)

0 commit comments

Comments
 (0)