Skip to content

Commit

Permalink
feat: enable decoding for 256bit number (#1135)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ologbonowiwi authored Oct 20, 2024
1 parent 7066a54 commit 717ce1a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
12 changes: 8 additions & 4 deletions zio-json/shared/src/test/scala/zio/json/CodecSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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\""
Expand Down
15 changes: 12 additions & 3 deletions zio-json/shared/src/test/scala/zio/json/DecoderSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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") {
Expand Down

0 comments on commit 717ce1a

Please sign in to comment.