From b8ab7ab21b76fb085497ab5b84f424647bf3e2dd Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Tue, 18 May 2021 12:27:47 +0800 Subject: [PATCH] Fix default argument handling for huge case classes (#350) --- core/src/upickle/core/Types.scala | 4 ++-- upickle/test/src-2/upickle/TooBigTests.scala | 19 +++++++++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/core/src/upickle/core/Types.scala b/core/src/upickle/core/Types.scala index b0c5f5b5c..61b124d84 100644 --- a/core/src/upickle/core/Types.scala +++ b/core/src/upickle/core/Types.scala @@ -229,8 +229,8 @@ trait Types{ types => } def visitKey(index: Int) = _root_.upickle.core.StringVisitor protected def storeValueIfNotFound(i: Int, v: Any) = { - if ((found(currentIndex / 64) & (1L << i)) == 0) { - found(currentIndex / 64) |= (1L << i) + if ((found(i / 64) & (1L << i)) == 0) { + found(i / 64) |= (1L << i) storeAggregatedValue(i, v) } } diff --git a/upickle/test/src-2/upickle/TooBigTests.scala b/upickle/test/src-2/upickle/TooBigTests.scala index d6a59c09f..37f77298b 100644 --- a/upickle/test/src-2/upickle/TooBigTests.scala +++ b/upickle/test/src-2/upickle/TooBigTests.scala @@ -91,6 +91,21 @@ object TooBigTests extends TestSuite { err.getMessage == "missing keys in dictionary: _2, _52, _102, _142 at index 1392" ) + + val readWithDefault1 = upickle.default.read[Big150]( + written150.replace(",\"_149\":149", "") + ) + assert(readWithDefault1 == b150.copy(_149 = -1337)) + + val readWithDefault2 = upickle.default.read[Big150]( + written150.replace("\"_0\":0,", "") + ) + assert(readWithDefault2 == b150.copy(_0 = 31337)) + + val readWithDefault3 = upickle.default.read[Big150]( + written150.replace("\"_0\":0,", "").replace(",\"_149\":149", "") + ) + assert(readWithDefault3 == b150.copy(_0 = 31337, _149 = -1337)) } } // test("hugeFile"){ @@ -158,7 +173,7 @@ case class Big65(_0: Byte, _1: Byte, _2: Byte, _3: Byte, _4: Byte, _5: Byte, _6: object Big65{ implicit val b65rw: upickle.default.ReadWriter[Big65] = upickle.default.macroRW } -case class Big150(_0: Int, _1: Int, _2: Int, _3: Int, _4: Int, _5: Int, _6: Int, _7: Int, +case class Big150(_0: Int = 31337, _1: Int, _2: Int, _3: Int, _4: Int, _5: Int, _6: Int, _7: Int, _8: Int, _9: Int, _10: Int, _11: Int, _12: Int, _13: Int, _14: Int, _15: Int, _16: Int, _17: Int, _18: Int, _19: Int, _20: Int, _21: Int, _22: Int, _23: Int, _24: Int, _25: Int, _26: Int, _27: Int, _28: Int, @@ -180,7 +195,7 @@ case class Big150(_0: Int, _1: Int, _2: Int, _3: Int, _4: Int, _5: Int, _6: Int, _129: Int, _130: Int, _131: Int, _132: Int, _133: Int, _134: Int, _135: Int, _136: Int, _137: Int, _138: Int, _139: Int, _140: Int, _141: Int, _142: Int, _143: Int, _144: Int, _145: Int, _146: Int, - _147: Int, _148: Int, _149: Int){ + _147: Int, _148: Int, _149: Int = -1337 /*default*/){ // Workaround for https://github.com/scala/scala/pull/9635 override def equals(other: Any): Boolean = other match{