Skip to content

Commit

Permalink
Merge pull request #512 from cchantep/fix/sub-trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ignasi35 authored Nov 24, 2020
2 parents ed26694 + 571c49c commit 0215ac6
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ class JsMacroImpl(val c: blackbox.Context) {
case jsv => $json.JsObject(Seq("_value" -> jsv))
}

jso + ($config.discriminator -> $json.JsString($config.typeNaming(${t.typeSymbol.fullName})))
$json.JsObject(Map($config.discriminator -> $json.JsString($config.typeNaming(${t.typeSymbol.fullName})))) ++ jso
}"""
}
)
Expand Down
23 changes: 23 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 @@ -144,6 +144,14 @@ class MacroSpec extends AnyWordSpec with Matchers with org.scalatestplus.scalach
jsSimple.validate[Family].mustEqual(JsSuccess(simple))
jsOptional.validate[Family].mustEqual(JsSuccess(optional))
}

"subtype is a sealed trait itself" in {
val expected = Leaf2(1)
val expectedJs = Json.obj("_type" -> "play.api.libs.json.MacroSpec.Leaf2", "value" -> 1)

Json.toJson[TreeValue](expected) mustEqual expectedJs
expectedJs.validate[TreeValue] mustEqual JsSuccess(expected)
}
}

"be generated for a ValueClass" in {
Expand Down Expand Up @@ -666,6 +674,21 @@ class MacroSpec extends AnyWordSpec with Matchers with org.scalatestplus.scalach
*/
}

sealed trait TreeValue

sealed trait SubLevel extends TreeValue

case class Leaf1(value: String) extends TreeValue
case class Leaf2(value: Int) extends SubLevel

object TreeValue {
private implicit val leaf1: OFormat[Leaf1] = Json.format
private implicit val leaf2: OFormat[Leaf2] = Json.format
private implicit val subLevel: OFormat[SubLevel] = Json.format

implicit val format: OFormat[TreeValue] = Json.format
}

object Foo {
import shapeless.tag.@@

Expand Down

0 comments on commit 0215ac6

Please sign in to comment.