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 #836 - Scala3 macro nesting class #956

Merged
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 @@ -319,7 +319,6 @@ object JsMacroImpl { // TODO: debug
val types = tprElements.map(_._2)
val resolve = resolver[Reads, T](forwardExpr, debug)(readsTpe)
val compCls = tpr.typeSymbol.companionClass
val compMod = Ref(tpr.typeSymbol.companionModule)

val (optional, required) = tprElements.zipWithIndex
.map { case ((sym, rpt), i) =>
Expand All @@ -330,7 +329,7 @@ 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) =>
compMod.select(defaultSym).asExprOf[t]
Ref(tpr.typeSymbol.companionModule).select(defaultSym).asExprOf[t]
}

ReadableField(sym, i, pt, default)
Expand Down
22 changes: 22 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 @@ -505,6 +505,19 @@ class MacroSpec extends AnyWordSpec with Matchers with org.scalatestplus.scalach

reader.reads(jsPref2).mustEqual(JsSuccess(pref2))
}

"handle nesting class" in {
implicit val textIdFormat: Format[TextId] = Json.valueFormat[TextId]

val nesting = new NestingClass

val expected = nesting.Test(Some(new TextId("foo")))
val expectedJson = Json.obj("underlying" -> "foo")

Json.toJson(expected).mustEqual(expectedJson)

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

Expand Down Expand Up @@ -602,4 +615,13 @@ object MacroSpec {
}

case class Preference[V](key: String, kind: PrefKind.Aux[V], value: V)

class NestingClass {
case class Test(underlying: Option[TextId])

object Test {
implicit def format(implicit textIdFormat: Format[TextId]): Format[Test] =
Json.format[Test]
}
}
}
Loading