SprayJson:在编组期间缺少必需的成员

时间:2016-07-18 08:20:48

标签: scala spray scalatest spray-json

我有案例类

 case class CreateOffer(playerId: Option[PlayerId], modifier: Double, expiry: DateTime, start: DateTime, notification: String)

当我尝试测试以喷涂

val req = Post("/create", createOffer) ~>
      addHeader("token", playerToken) ~>
      handleExceptions(exceptionHandler)(router.route) ~>
      check {
       //
      }

我有例外

spray.json.DeserializationException: Object is missing required member 'modifier'

编组我使用

jsonFormat5(CreateOffer.apply)

在之前的测试中,我没有这个问题,但是在重构之后 它出现了

路线看起来

              path("create") {
                post {
                  traceName("create") {
                    handleWith { createOffer: CreateOffer =>
                      Future[CreateOffer] {
                        controller.createOffer(createOffer)
                      }
                    }
                  }
                }
              }

**************** UPDATE ****************

最后我可以在IntelliJ中调试我的测试,我发现问题可能出在ProductFormats.scala(spray.json)中。第一个参数是选项和阅读工作正常,但第二个是正常值,我有问题:

protected def fromField[T](value: JsValue, fieldName: String)
                                     (implicit reader: JsonReader[T]) = value match {
    case x: JsObject if
      (reader.isInstanceOf[OptionFormat[_]] &  //I think that in this line is problem 
        !x.fields.contains(fieldName)) =>
      None.asInstanceOf[T]
    case x: JsObject =>
      try reader.read(x.fields(fieldName))
      catch {
        case e: NoSuchElementException =>
          deserializationError("Object is missing required member '" + fieldName + "'", e, fieldName :: Nil)
        case DeserializationException(msg, cause, fieldNames) =>
          deserializationError(msg, cause, fieldName :: fieldNames)
      }
    case _ => deserializationError("Object expected in field '" + fieldName + "'", fieldNames = fieldName :: Nil)
  }

0 个答案:

没有答案