From 717ce1a38a5e2bf42f7a2b12d240b10db115317b Mon Sep 17 00:00:00 2001 From: ologbonowiwi Date: Sun, 20 Oct 2024 14:12:41 -0300 Subject: [PATCH] feat: enable decoding for 256bit number (#1135) * test: ensure >256 bit does not work but 256 works * feat: update max bit to 256 * style: format file * test(CodecSpec): update big int size --- .../src/main/scala/zio/json/internal/lexer.scala | 2 +- .../src/test/scala/zio/json/CodecSpec.scala | 12 ++++++++---- .../src/test/scala/zio/json/DecoderSpec.scala | 15 ++++++++++++--- 3 files changed, 21 insertions(+), 8 deletions(-) diff --git a/zio-json/shared/src/main/scala/zio/json/internal/lexer.scala b/zio-json/shared/src/main/scala/zio/json/internal/lexer.scala index c4e66505c..39dc7fd2a 100644 --- a/zio-json/shared/src/main/scala/zio/json/internal/lexer.scala +++ b/zio-json/shared/src/main/scala/zio/json/internal/lexer.scala @@ -26,7 +26,7 @@ object Lexer { // TODO need a variant that doesn't skip whitespace, so that attack vectors // consisting of an infinite stream of space can exit early. - val NumberMaxBits: Int = 128 + val NumberMaxBits: Int = 256 // True if we got a string (implies a retraction), False for } def firstField(trace: List[JsonError], in: RetractReader): Boolean = diff --git a/zio-json/shared/src/test/scala/zio/json/CodecSpec.scala b/zio-json/shared/src/test/scala/zio/json/CodecSpec.scala index 1cd755c73..468017ecc 100644 --- a/zio-json/shared/src/test/scala/zio/json/CodecSpec.scala +++ b/zio-json/shared/src/test/scala/zio/json/CodecSpec.scala @@ -7,6 +7,7 @@ import zio.test.Assertion._ import zio.test.TestAspect.jvmOnly import zio.test._ +import java.math.BigInteger import scala.collection.immutable object CodecSpec extends ZIOSpecDefault { @@ -37,10 +38,13 @@ object CodecSpec extends ZIOSpecDefault { }, test("primitives") { val exampleBDString = "234234.234" - // this big integer consumes more than 128 bits - assert("170141183460469231731687303715884105728".fromJson[java.math.BigInteger])( - isLeft(equalTo("(expected a 128 bit BigInteger)")) - ) && assert(exampleBDString.fromJson[BigDecimal])(isRight(equalTo(BigDecimal(exampleBDString)))) + // this big integer consumes more than 256 bits + assert( + "170141183460469231731687303715884105728489465165484668486513574864654818964653168465316546851" + .fromJson[java.math.BigInteger] + )( + isLeft(equalTo("(expected a 256 bit BigInteger)")) + ) }, test("java.util.Currency") { val exampleValue = "\"USD\"" diff --git a/zio-json/shared/src/test/scala/zio/json/DecoderSpec.scala b/zio-json/shared/src/test/scala/zio/json/DecoderSpec.scala index 7f65c7523..fd7088738 100644 --- a/zio-json/shared/src/test/scala/zio/json/DecoderSpec.scala +++ b/zio-json/shared/src/test/scala/zio/json/DecoderSpec.scala @@ -7,6 +7,7 @@ import zio.test.Assertion._ import zio.test.TestAspect.jvmOnly import zio.test._ +import java.math.BigInteger import java.time.{ Duration, OffsetDateTime, ZonedDateTime } import java.util.UUID import scala.collection.{ SortedMap, immutable, mutable } @@ -19,10 +20,18 @@ object DecoderSpec extends ZIOSpecDefault { test("BigDecimal") { assert("123".fromJson[BigDecimal])(isRight(equalTo(BigDecimal(123)))) }, - test("BigInteger too large") { - // this big integer consumes more than 128 bits + test("256 bit BigInteger") { assert("170141183460469231731687303715884105728".fromJson[java.math.BigInteger])( - isLeft(equalTo("(expected a 128 bit BigInteger)")) + isRight(equalTo(new BigInteger("170141183460469231731687303715884105728"))) + ) + }, + test("BigInteger too large") { + // this big integer consumes more than 256 bits + assert( + "170141183460469231731687303715884105728489465165484668486513574864654818964653168465316546851" + .fromJson[java.math.BigInteger] + )( + isLeft(equalTo("(expected a 256 bit BigInteger)")) ) }, test("collections") {