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