Skip to content

Commit

Permalink
Fix #971 – "Exception occurred while executing macro expansion."
Browse files Browse the repository at this point in the history
  • Loading branch information
sgodbillon committed Mar 3, 2024
1 parent 10a8ae1 commit 9b3b4d7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,13 @@ object JsMacroImpl { // TODO: debug
val default: Option[Expr[t]] =
compCls.declaredMethod(f"$$lessinit$$greater$$default$$" + (i + 1)).headOption.collect {
case defaultSym if sym.flags.is(Flags.HasDefault) =>
Ref(tpr.typeSymbol.companionModule).select(defaultSym).asExprOf[t]
val select = Select(Ref(tpr.typeSymbol.companionModule), defaultSym)
val tree =
TypeRepr.of[T].typeArgs match {
case Nil => select
case typeArgs => select.appliedToTypes(typeArgs)
}
tree.asExprOf[t]
}

ReadableField(sym, i, pt, default)
Expand Down
25 changes: 25 additions & 0 deletions play-json/shared/src/test/scala/play/api/libs/json/MacroSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,26 @@ class MacroSpec extends AnyWordSpec with Matchers with org.scalatestplus.scalach

nesting.Test.format.reads(expectedJson).mustEqual(JsSuccess(expected))
}

"handle case class with generic type and default field" in {
implicit val format: Format[GenericCaseClassWithDefault[Int]] = Json.format[GenericCaseClassWithDefault[Int]]

val expected = GenericCaseClassWithDefault(3)
val expectedJson = Json.obj("data" -> 3, "descr" -> "something")

Json.toJson(GenericCaseClassWithDefault(3)).mustEqual(expectedJson)
Json.fromJson(expectedJson).mustEqual(JsSuccess(expected))
}

"handle case class with generic type and overridden default field" in {
implicit val format: Format[GenericCaseClassWithDefault[Int]] = Json.format[GenericCaseClassWithDefault[Int]]

val expected = GenericCaseClassWithDefault(3, "foo")
val expectedJson = Json.obj("data" -> 3, "descr" -> "foo")

Json.toJson(GenericCaseClassWithDefault(3, "foo")).mustEqual(expectedJson)
Json.fromJson(expectedJson).mustEqual(JsSuccess(expected))
}
}
}

Expand Down Expand Up @@ -624,4 +644,9 @@ object MacroSpec {
Json.format[Test]
}
}

case class GenericCaseClassWithDefault[A](
data: A,
descr: String = "something"
)
}

0 comments on commit 9b3b4d7

Please sign in to comment.