Skip to content

Commit

Permalink
Fix decoding error on extra members for nullary case classes and obje…
Browse files Browse the repository at this point in the history
…cts with MapBasedCodecs, closes #600
  • Loading branch information
sirthias committed Dec 3, 2022
1 parent 60c6f10 commit f8722da
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,12 @@ object MapBasedCodecs extends DerivationApi {
new Deriver[Decoder, T, quotes.type]:

def deriveForCaseObject(moduleTermSymbol: Symbol): Expr[Decoder[T]] =
'{ Decoder[T](r => r.readMapClose(r.readMapOpen(0), ${ Ref(moduleTermSymbol).asExprOf[T] })) }
'{
Decoder[T] { r =>
if (r.readMapOpen(0)) while (!r.tryReadBreak()) r.skipElement()
${ Ref(moduleTermSymbol).asExprOf[T] }
}
}

def deriveForCaseClass(
tpe: TypeRepr,
Expand Down Expand Up @@ -443,7 +448,7 @@ object MapBasedCodecs extends DerivationApi {
else fail("Case classes mit > 128 fields are not supported")
} else
'{
if (count < 0) $r.readBreak()
if (count < 0) while (! $r.tryReadBreak()) $r.skipElement()
${ companionApply(companion, typeArgs(tpe), Nil).asExprOf[T] }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,17 @@ class MiscSpec extends AbstractBorerSpec {
| 1| }
|""".stripMargin
}

test("Extra members") {
sealed trait Base
case class A(x: Int) extends Base
case class B() extends Base
case object C extends Base

implicit val baseCodecs: Decoder[Base] = MapBasedCodecs.deriveAllDecoders

verifyDecoding[Base]("""{"A":{"x":100, "extra": "should be ignored"}}""", A(100))
verifyDecoding[Base]("""{"B":{"extra": "should be ignored"}}""", B())
verifyDecoding[Base]("""{"C":{"extra": "should be ignored"}}""", C)
}
}

0 comments on commit f8722da

Please sign in to comment.