44
55package play .api .libs .json
66
7+ import scala .util .control .NonFatal
8+
79object TestFormats {
810 implicit def eitherReads [A : Reads , B : Reads ] = Reads [Either [A , B ]] { js =>
911 implicitly[Reads [A ]].reads(js) match {
@@ -142,6 +144,42 @@ class MacroSpec extends WordSpec with MustMatchers
142144 jsOptional.validate[Family ].get mustEqual optional
143145 }
144146 }
147+
148+ " fails due to forward reference to Reads" in {
149+ implicit def reads : Reads [Lorem [Simple ]] =
150+ InvalidForwardResolution .simpleLoremReads
151+
152+ val jsLorem = Json .obj(" age" -> 11 , " ipsum" -> Json .obj(" bar" -> " foo" ))
153+
154+ try {
155+ jsLorem.validate[Lorem [Simple ]]
156+ } catch {
157+ case NonFatal (npe : NullPointerException ) => {
158+ val expected = " Invalid implicit resolution"
159+ npe.getMessage.take(expected.size) mustEqual expected
160+ }
161+
162+ case NonFatal (cause) => throw cause
163+ }
164+ }
165+
166+ " fails due to forward reference to Format" in {
167+ implicit def format : Format [Lorem [Simple ]] =
168+ InvalidForwardResolution .simpleLoremFormat
169+
170+ val jsLorem = Json .obj(" age" -> 11 , " ipsum" -> Json .obj(" bar" -> " foo" ))
171+
172+ try {
173+ jsLorem.validate[Lorem [Simple ]]
174+ } catch {
175+ case NonFatal (npe : NullPointerException ) => {
176+ val expected = " Invalid implicit resolution"
177+ npe.getMessage.take(expected.size) mustEqual expected
178+ }
179+
180+ case NonFatal (cause) => throw cause
181+ }
182+ }
145183 }
146184
147185 " Writes" should {
@@ -523,6 +561,38 @@ class MacroSpec extends WordSpec with MustMatchers
523561 jsOptional.validate(Json .reads[Optional ]).
524562 get mustEqual (optional)
525563 }
564+
565+ " fails due to forward reference to Writes" in {
566+ implicit def writes : Writes [Lorem [Simple ]] =
567+ InvalidForwardResolution .simpleLoremWrites
568+
569+ try {
570+ Json .toJson(Lorem (age = 11 , ipsum = Simple (bar = " foo" )))
571+ } catch {
572+ case NonFatal (npe : NullPointerException ) => {
573+ val expected = " Invalid implicit resolution"
574+ npe.getMessage.take(expected.size) mustEqual expected
575+ }
576+
577+ case NonFatal (cause) => throw cause
578+ }
579+ }
580+
581+ " fails due to forward reference to Format" in {
582+ implicit def format : Format [Lorem [Simple ]] =
583+ InvalidForwardResolution .simpleLoremFormat
584+
585+ try {
586+ Json .toJson(Lorem (age = 11 , ipsum = Simple (bar = " foo" )))
587+ } catch {
588+ case NonFatal (npe : NullPointerException ) => {
589+ val expected = " Invalid implicit resolution"
590+ npe.getMessage.take(expected.size) mustEqual expected
591+ }
592+
593+ case NonFatal (cause) => throw cause
594+ }
595+ }
526596 }
527597
528598 // ---
@@ -544,6 +614,16 @@ class MacroSpec extends WordSpec with MustMatchers
544614 */
545615 }
546616
617+ object InvalidForwardResolution {
618+ // Invalids as forward references to `simpleX`
619+ val simpleLoremReads = Json .reads[Lorem [Simple ]]
620+ val simpleLoremWrites = Json .writes[Lorem [Simple ]]
621+ val simpleLoremFormat = Json .format[Lorem [Simple ]]
622+
623+ implicit val simpleReads : Reads [Simple ] = Json .reads
624+ implicit val simpleWrites : OWrites [Simple ] = Json .writes[Simple ]
625+ }
626+
547627 object Foo {
548628 import shapeless .tag .@@
549629
0 commit comments