@@ -2,12 +2,12 @@ package zio.schema.codec
22
33import scala .reflect .{ ClassTag , classTag }
44
5- import org .bson ._
65import org .bson .codecs .configuration .CodecRegistry
76import org .bson .codecs .{ Codec => BCodec , DecoderContext , EncoderContext }
87import org .bson .conversions .Bson
98import org .bson .io .BasicOutputBuffer
10- import org .bson .types .ObjectId
9+ import org .bson .types .{ Decimal128 , ObjectId }
10+ import org .bson .{ BsonDecimal128 , _ }
1111
1212import zio .bson .BsonBuilder ._
1313import zio .bson ._
@@ -24,6 +24,13 @@ object BsonSchemaCodecSpec extends ZIOSpecDefault {
2424 implicit lazy val codec : BsonCodec [SimpleClass ] = BsonSchemaCodec .bsonCodec(schema)
2525 }
2626
27+ case class BigDecimalClass (value : BigDecimal )
28+
29+ object BigDecimalClass {
30+ implicit val schema : Schema [BigDecimalClass ] = DeriveSchema .gen
31+ implicit lazy val codec : BsonCodec [BigDecimalClass ] = BsonSchemaCodec .bsonCodec(schema)
32+ }
33+
2734 sealed trait Tree
2835
2936 object Tree {
@@ -79,6 +86,10 @@ object BsonSchemaCodecSpec extends ZIOSpecDefault {
7986 } yield Customer (id, name, age, friends)
8087 }
8188
89+ // Custom generator for BigDecimal values with rounding to ensure exact representation as Decimal128
90+ def genRoundedBigDecimal (scale : Int ): Gen [Any , BigDecimal ] =
91+ Gen .double.map(d => BigDecimal (d).setScale(scale, BigDecimal .RoundingMode .HALF_UP ))
92+
8293 def spec : Spec [TestEnvironment with Scope , Any ] = suite(" BsonSchemaCodecSpec" )(
8394 suite(" round trip" )(
8495 roundTripTest(" SimpleClass" )(
@@ -107,6 +118,12 @@ object BsonSchemaCodecSpec extends ZIOSpecDefault {
107118 Customer .example.invitedFriends.map(_.value.toBsonValue): _*
108119 )
109120 )
121+ ),
122+ roundTripTest(" BigDecimalClass" )(
123+ // 14 decimal places in the assert value below
124+ genRoundedBigDecimal(14 ).map(BigDecimalClass (_)),
125+ BigDecimalClass (BigDecimal (" 279.00000000000000" )),
126+ doc(" value" -> new BsonDecimal128 (Decimal128 .parse(" 279.00000000000000" )))
110127 )
111128 ),
112129 suite(" configuration" )(
0 commit comments