Skip to content

Commit 385167b

Browse files
committed
Allow interpreting BSON Int32 as Long when reading
1 parent 72dd607 commit 385167b

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ class BsonReaderInput(br: BsonReader, override val legacyOptionEncoding: Boolean
1616
override def readString(): String = br.readString()
1717
override def readBoolean(): Boolean = br.readBoolean()
1818
override def readInt(): Int = br.readInt32()
19-
override def readLong(): Long = br.readInt64()
19+
override def readLong(): Long = {
20+
if (bsonType == BsonType.INT32) br.readInt32().toLong // allow to treat INT32 as Long
21+
else br.readInt64()
22+
}
2023
override def readTimestamp(): Long = br.readDateTime()
2124
override def readDouble(): Double = br.readDouble()
2225
override def readBigInt(): BigInt = BigInt(br.readBinaryData().getData)

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ class BsonValueInput(bsonValue: BsonValue, override val legacyOptionEncoding: Bo
2222
def readString(): String = handleFailures(bsonValue.asString().getValue)
2323
def readBoolean(): Boolean = handleFailures(bsonValue.asBoolean().getValue)
2424
def readInt(): Int = handleFailures(bsonValue.asInt32().getValue)
25-
def readLong(): Long = handleFailures(bsonValue.asInt64().getValue)
25+
def readLong(): Long = {
26+
if (bsonType == BsonType.INT32) handleFailures(bsonValue.asInt32().getValue.toLong) // allow to treat INT32 as Long
27+
else handleFailures(bsonValue.asInt64().getValue)
28+
}
2629
override def readTimestamp(): Long = handleFailures(bsonValue.asDateTime().getValue)
2730
def readDouble(): Double = handleFailures(bsonValue.asDouble().getValue)
2831
def readBigInt(): BigInt = handleFailures(BigInt(bsonValue.asBinary().getData))

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class BsonValueGenCodecRoundtripTest extends GenCodecRoundtripTest {
6868

6969
def createInput(raw: BsonValue): Input =
7070
new BsonValueInput(raw, legacyOptionEncoding)
71+
72+
test("Int32 to Long decoding") {
73+
val input = createInput(new BsonInt32(42))
74+
val result = input.readSimple().readLong()
75+
assert(result == 42L)
76+
}
7177
}
7278

7379
class BsonInputOutputTest extends AnyFunSuite with ScalaCheckPropertyChecks {
@@ -131,6 +137,14 @@ class BsonInputOutputTest extends AnyFunSuite with ScalaCheckPropertyChecks {
131137
forAll(SomethingComplex.gen)(valueEncoding(_, legacyOptionEncoding = true))
132138
}
133139

140+
test("Int32 to Long decoding") {
141+
val doc = new BsonDocument("value", new BsonInt32(42))
142+
val reader = new BsonDocumentReader(doc)
143+
val input = new BsonReaderInput(reader, false)
144+
val sth = SomethingLong.codec.read(input)
145+
assert(sth == SomethingLong(42))
146+
}
147+
134148
test("All encoding schemes with problematic map keys") {
135149
forAll(SomethingComplex.gen) { sth =>
136150
val sthBefore = sth.copy(

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,8 @@ object SomethingComplex {
8080

8181
implicit val codec: GenCodec[SomethingComplex] = GenCodec.materialize
8282
}
83+
84+
case class SomethingLong(value: Long)
85+
object SomethingLong {
86+
implicit val codec: GenCodec[SomethingLong] = GenCodec.materialize
87+
}

0 commit comments

Comments
 (0)