У меня странная проблема с генерацией файла Swagger в моем приложении Akka HTTP. Когда я закомментирую private object internal
в классе пользовательских маршрутов, кажется, что все работает нормально, но всякий раз, когда у меня есть код, подобный классу (UserRoutes) ниже, он не работает. Кажется, я должен настроить Swagger, чтобы проверить также частные объекты. У вас есть подсказка?
Вот мой класс Swagger
object Swagger extends SwaggerHttpService {
override val apiClasses: Set[Class[_]] = Set(classOf[UserRoutes], classOf[AuthRoutes])
override val host = "localhost:8080" //the url of your api, not swagger's json endpoint
// override val apiDocsPath = "api-docs" //where you want the swagger-json endpoint exposed
override val info: Info = Info(title = "Akka/HTTP/Streams test") //provides license and other description details
override val schemes = List("http")
override def routes: Route = super.routes ~ get {
pathPrefix("") {
pathEndOrSingleSlash {
getFromResource("swagger-ui/index.html")
}
} ~
getFromResourceDirectory("swagger-ui")
}
}
Вот мой объект Routes
trait Routes extends Version with Directives with FailFastCirceSupport
object Routes extends Directives {
def buildRoutes(dependencies: Dependencies): Route =
Swagger.routes ~ new UserRoutes(dependencies.userService).userRoutes ~
new AuthRoutes(dependencies.authService).authRoutes
def buildErrorMapper(serviceErrorMapper: PartialFunction[ServiceError, HttpError]): ErrorMapper[ServiceError, HttpError] =
(e: ServiceError) =>
serviceErrorMapper
.applyOrElse(
e,
(_: ServiceError) => InternalErrorHttp("Unexpected error")
)
}
, а вот UserRoutes.class
class UserRoutes(val userService: UserService) extends Routes with SecuredRoutes {
val userRoutes: Route = internal.routes
val authorizationList = List("admin", "developer")
private object internal {
implicit val httpErrorMapper: ErrorMapper[ServiceError, HttpError] =
Routes.buildErrorMapper(ServiceError.httpErrorMapper)
implicit class ErrorOps[E <: ServiceError, A](result: Future[Either[E, A]]) {
def toRestError[G <: HttpError](implicit errorMapper: ErrorMapper[E, G]): Future[Either[G, A]] = result.map {
case Left(error) => Left(errorMapper(error))
case Right(value) => Right(value)
}
}
def routes: Route =
pathPrefix("api" / version)(
userManagement
)
def userManagement: Route =
pathPrefix("users") {
authorized(authorizationList)(_ => userActions ~ getUsers ~ postUser)
}
@GET
@Path("user")
@Operation(
summary = "Return current status for this name"
)
@ApiResponse(
responseCode = "200",
description = "Status response",
content = Array(new Content(schema = new Schema(implementation = classOf[UserCreate])))
)
@ApiResponse(responseCode = "404", description = "Name not found")
def getUsers: Route =
get(
onComplete(userService.getUsers) {
case Success(future) => completeEither(StatusCodes.OK, future)
case Failure(ex) =>
complete((StatusCodes.InternalServerError, s"An error occurred: ${ex.getMessage}"))
}
)
}
}