1

我在貘创建一个端点,它需要动态状态码来输出错误。

我已经声明了一个密封特性FailureResponse,并且我创建了扩展这个特性的案例类。类似于显示的示例https://tapir-scala.readthedocs.io/en/latest/endpoint/statuscodes.html

模型

object AuthenticationModel {

  case class Request(productId: String)

  case class SuccessResponse(success: Boolean, status: Int, data: Token, message: String)

  sealed trait FailureResponse
  case class FailureResponse400(success: Boolean, status: Int = 400, errors: Array[Error], message: String) extends FailureResponse
  case class FailureResponse401(success: Boolean, status: Int = 401, errors: Array[Error], message: String) extends FailureResponse
  case class FailureResponse422(success: Boolean, status: Int = 422, errors: Array[Error], message: String) extends FailureResponse
  case class FailureResponse500(success: Boolean, status: Int = 500, errors: Array[Error], message: String) extends FailureResponse

  case class Token(token: String)
  case class Error(code: String, message: String)

}

端点

object Auth {

  val authenticationEndpoint : Endpoint[AuthenticationModel.Request, AuthenticationModel.FailureResponse, AuthenticationModel.SuccessResponse, Unit] = endpoint
    .tag("Auth")
    .description("Generates an idcheck token using product id")
    .post
    .in("auth")
    .in(jsonBody[AuthenticationModel.Request])
    .out(jsonBody[AuthenticationModel.SuccessResponse])
    .errorOut(oneOf(
      statusMapping(StatusCode.BadRequest, jsonBody[AuthenticationModel.FailureResponse400]),
      statusMapping(StatusCode.Unauthorized, jsonBody[AuthenticationModel.FailureResponse401]),
      statusMapping(StatusCode.UnprocessableEntity, jsonBody[AuthenticationModel.FailureResponse422]),
      statusMapping(StatusCode.InternalServerError, jsonBody[AuthenticationModel.FailureResponse500]),
    ))

}

我已将错误输出参数类型设置为AuthenticationModel.FailureResponse.

但是我得到一个类型不匹配

必需的 AuthenticationModel.FailureResponse 发现 AuthenticationModel.FailureResponse 与产品与可序列化

4

1 回答 1

0

解决方案在错误消息中:

改变你的

sealed trait FailureResponse

sealed trait FailureResponse extends Product with Serializable

我希望你已经设法解决了这个问题/

于 2020-03-23T14:46:03.513 回答