I am getting JsResultException(errors:List((/additional-info,List(JsonValidationError(List(error.path.missing),WrappedArray())))
error though the json
structure seems OK to me. What mistake am I making?
I am writing unit tests. The test case is that server should send error message if the client sends message without json
.
The server side code which sends the error is Future { Ok(Json.toJson(JsonResultError(messagesApi("error.incorrectBodyType")(langs.availables(0))))) }
The test case is
"User signup request with no body" should {
"return 400 (Bad Request) and the validation text must be 'Incorrect body type. Body type must be JSON'" in {
println("testing with mocked User value",user);
val request = FakeRequest("POST","ws/users/signup")
println("sending request",request)
val result:Future[Result] = controller.signupUser(request)
val bodyAsString = contentAsString(result)
println("body as String: "+bodyAsString)
val bodyAsJson = Json.toJson(bodyAsString)
println("body as json:"+bodyAsJson)
val jsonResultError:JsonResult = bodyAsJson.as[JsonResult]
println("jsonResultError: "+jsonResultError)
(status(result) mustBe OK )
(jsonResultError.message mustBe ("some Error "))
}
}
}
The print messages on console are
body as String: {"result":"error","additional-info":"error.incorrectBodyType"}
body as json:"{\"result\":\"error\",\"additional-info\":\"error.incorrectBodyType\"}"
JsResultException(errors:List((/additional-info,List(JsonValidationError(List(error.path.missing),WrappedArray()))), (/result,List(JsonValidationError(List(error.path.missing),WrappedArray())))))
play.api.libs.json.JsResultException: JsResultException(errors:List((/additional-info,List(JsonValidationError(List(error.path.missing),WrappedArray()))), (/result,List(JsonValidationError(List(error.path.missing),WrappedArray())))))
The implicit
conversions between the json
and my model is
sealed abstract class JsonResult (val result:String, val message:String)
case class JsonResultError(override val message:String) extends JsonResult(result="error", message)
case class JsonResultSuccess(override val message:String) extends JsonResult(result="success",message)
object JsonResult {
def apply(result:String,additionalInfo:String) = {
if(result== "error") JsonResultError(additionalInfo) else JsonResultSuccess(additionalInfo)
}
def unapply(jsonResult:JsonResult):Option[(String,String)] = {
if(jsonResult == null) None else Some (jsonResult.result,jsonResult.message)
}
}
object JSONMessagesImplicits {
implicit val JsonResultWrites:Writes[JsonResult] = {
((JsPath \ "result").write[String] and
(JsPath \ "additional-info").write[String])(unlift(JsonResult.unapply)) //I suppose `unlift` would convert Option[T] return value of unapply method of JsonResult Extractor object to T.
}
//read from jsvalue i.e. create model from jsvalue
implicit val JsonResultReads:Reads[JsonResult] = {
((JsPath \ "result").read[String] and
(JsPath \ "additional-info").read[String])(JsonResult.apply _)
}
}
It worked when I changed val bodyAsJson = Json.toJson(bodyAsString)
to val bodyAsJson = Json.parse(bodyAsString)