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 {
@@ -536,6 +574,38 @@ class MacroSpec extends WordSpec with MustMatchers
536574 formatter.writes(Obj ) mustEqual jsObj
537575 formatter.reads(jsObj) mustEqual JsSuccess (Obj )
538576 }
577+
578+ " fails due to forward reference to Writes" in {
579+ implicit def writes : Writes [Lorem [Simple ]] =
580+ InvalidForwardResolution .simpleLoremWrites
581+
582+ try {
583+ Json .toJson(Lorem (age = 11 , ipsum = Simple (bar = " foo" )))
584+ } catch {
585+ case NonFatal (npe : NullPointerException ) => {
586+ val expected = " Invalid implicit resolution"
587+ npe.getMessage.take(expected.size) mustEqual expected
588+ }
589+
590+ case NonFatal (cause) => throw cause
591+ }
592+ }
593+
594+ " fails due to forward reference to Format" in {
595+ implicit def format : Format [Lorem [Simple ]] =
596+ InvalidForwardResolution .simpleLoremFormat
597+
598+ try {
599+ Json .toJson(Lorem (age = 11 , ipsum = Simple (bar = " foo" )))
600+ } catch {
601+ case NonFatal (npe : NullPointerException ) => {
602+ val expected = " Invalid implicit resolution"
603+ npe.getMessage.take(expected.size) mustEqual expected
604+ }
605+
606+ case NonFatal (cause) => throw cause
607+ }
608+ }
539609 }
540610
541611 // ---
@@ -557,6 +627,16 @@ class MacroSpec extends WordSpec with MustMatchers
557627 */
558628 }
559629
630+ object InvalidForwardResolution {
631+ // Invalids as forward references to `simpleX`
632+ val simpleLoremReads = Json .reads[Lorem [Simple ]]
633+ val simpleLoremWrites = Json .writes[Lorem [Simple ]]
634+ val simpleLoremFormat = Json .format[Lorem [Simple ]]
635+
636+ implicit val simpleReads : Reads [Simple ] = Json .reads
637+ implicit val simpleWrites : OWrites [Simple ] = Json .writes[Simple ]
638+ }
639+
560640 object Foo {
561641 import shapeless .tag .@@
562642
0 commit comments