Skip to content

Commit 6d2bf0f

Browse files
committed
Disallow Int64 as double and add more tests
1 parent 6ff0029 commit 6d2bf0f

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,7 @@ class BsonReaderInput(br: BsonReader, override val legacyOptionEncoding: Boolean
4141
bsonType match {
4242
case BsonType.DOUBLE => br.readDouble()
4343
case BsonType.INT32 => br.readInt32().toDouble
44-
case BsonType.INT64 => br.readInt64().toDouble
45-
case _ => wrongType(BsonType.DOUBLE, BsonType.INT32, BsonType.INT64)
44+
case _ => wrongType(BsonType.DOUBLE, BsonType.INT32)
4645
}
4746
}
4847

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,7 @@ class BsonValueInput(bsonValue: BsonValue, override val legacyOptionEncoding: Bo
3737
bsonType match {
3838
case BsonType.DOUBLE => bsonValue.asDouble().getValue
3939
case BsonType.INT32 => readInt().toDouble
40-
case BsonType.INT64 => bsonValue.asInt64().getValue.toDouble
41-
case _ => wrongType(BsonType.DOUBLE, BsonType.INT32, BsonType.INT64)
40+
case _ => wrongType(BsonType.DOUBLE, BsonType.INT32)
4241
}
4342
}
4443

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

Lines changed: 25 additions & 4 deletions
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

@@ -82,10 +83,10 @@ class BsonValueGenCodecRoundtripTest extends GenCodecRoundtripTest {
8283
assert(result == 43D)
8384
}
8485

85-
test("Int64 to Double decoding") {
86+
test("Int64 to Double decoding exception") {
8687
val input = createInput(new BsonInt64(44))
87-
val result = input.readSimple().readDouble()
88-
assert(result == 44D)
88+
89+
assertThrows[ReadFailure](input.readSimple().readDouble())
8990
}
9091
}
9192

@@ -246,6 +247,26 @@ class BsonInputOutputTest extends AnyFunSuite with ScalaCheckPropertyChecks {
246247
}
247248
}
248249

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+
249270
test("BsonBinaryReader type metadata") {
250271
forAll(SomethingPlain.gen) { sth =>
251272
val document = somethingToBson(sth)

0 commit comments

Comments
 (0)