Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #971 – "Exception occurred while executing macro expansion." #994

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = Ref(tpr.typeSymbol.companionModule).select(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"
)
}
Loading