首页 > 解决方案 > JsResultException error.path.missing 错误

问题描述

尽管结构对我来说似乎没问题,但我遇到了JsResultException(errors:List((/additional-info,List(JsonValidationError(List(error.path.missing),WrappedArray())))错误。json我犯了什么错误?

我正在编写单元测试。测试用例是如果客户端发送没有json.

发送错误的服务器端代码是Future { Ok(Json.toJson(JsonResultError(messagesApi("error.incorrectBodyType")(langs.availables(0))))) }

测试用例是

   "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 "))

        }
      }
    }

控制台上的打印消息是

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())))))

和我的模型之间的implicit转换是json

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 _)
  }

}

标签: scalatestplayframework-2.6

解决方案


当我val bodyAsJson = Json.toJson(bodyAsString)改为val bodyAsJson = Json.parse(bodyAsString)


推荐阅读