swagger-akka-http 2.x 在响应中设置对象列表



>我有一个问题。我想使用 Swagger 在 API 的响应中发送对象列表。type="数组"对我不起作用。 我看到一个主题 在 Swagger API 响应中设置对象列表 ,但它是旧版本的 lib。批注已更改。ApiResponse 曾经有 responseContainer 参数,但现在它消失了。 我有akka-http服务器。

val akkaVersion = "2.5.17"
val akkaHttpVersion = "10.1.5"
libraryDependencies ++= Seq(
"javax.ws.rs" % "javax.ws.rs-api" % "2.0.1",
"com.github.swagger-akka-http" %% "swagger-akka-http" % "2.0.0",
"com.github.swagger-akka-http" %% "swagger-scala-module" % "2.0.2",
"com.typesafe.akka" %% "akka-http" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-http-spray-json" % akkaHttpVersion,
"com.typesafe.akka" %% "akka-actor" % akkaVersion,
"com.typesafe.akka" %% "akka-stream" % akkaVersion,
"com.typesafe.akka" %% "akka-slf4j" % akkaVersion,
"ch.megard" %% "akka-http-cors" % "0.3.0",
"org.slf4j" % "slf4j-simple" % "1.7.25"
)

我创建了 get route 并用大摇大摆的注释来描述它。

@GET
@Path("offer-statuses/all")
@Produces(Array("application/json"))
@Operation(
tags = Array("offers"),
summary = "update periods",
responses = Array(
new ApiResponse(
responseCode = "200",
description = "OfferName response",
content = Array(
new Content(schema = new Schema(`type` = "array", implementation = classOf[EnumRow])))
),
new ApiResponse(responseCode = "400",
description = "Bad Request",
content = Array(new Content(schema = new Schema(implementation = classOf[BadRequest])))),
new ApiResponse(responseCode = "403",
description = "Forbidden",
content = Array(new Content(schema = new Schema(implementation = classOf[String]))))
)
)
def allOfferStatuses: Route = {
path("offers" / "offer-statuses" / "all") {
get {
applicationEnumsService.listAllOfferStatuses()
}
}
}
def listAllOfferStatuses(): List[EnumRow]
case class EnumRow(id: Int, name: String)

它构建了 json:

"/api/v1/offers/offer-statuses/all" : {
"get" : {
"tags" : [ "offers" ],
"summary" : "update periods",
"operationId" : "allOfferStatuses",
"responses" : {
"200" : {
"description" : "OfferName response",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/EnumRow"
}
}
}
},
"400" : {
"description" : "Bad Request",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/BadRequest"
}
}
}
},
"403" : {
"description" : "Forbidden",
"content" : {
"application/json" : {
"schema" : {
"type" : "string"
}
}
}
}
}
}
}
},
"EnumRow" : {
"required" : [ "id", "name" ],
"type" : "object",
"properties" : {
"id" : {
"type" : "integer",
"format" : "int32"
},
"name" : {
"type" : "string"
}
}
},

我找到了一个解决方案:

new ArraySchema(schema = new Schema(implementation = classOf[EnumRow]))

这似乎适用于我从 swagger 上传的单个文件:

import java.io.File
import akka.http.scaladsl.model.StatusCodes
import akka.http.scaladsl.server.Route
import akka.http.scaladsl.server.directives.FileInfo
import com.demo.erp.routes.SwaggerUi.{complete, path, post, storeUploadedFile}
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.media.{Content, Schema}
import io.swagger.v3.oas.annotations.parameters.RequestBody
import io.swagger.v3.oas.annotations.responses.ApiResponse
import io.swagger.v3.oas.annotations.tags.Tag
import javax.ws.rs.core.MediaType
import javax.ws.rs.{Consumes, POST, Path}
@Tag(name = "File Upload")
class FileUploadRouter {
//just for swagger
case class FileUpload(@Schema(`type` = "string", format = "binary", description = "file") file: File)
def tempDestination(fileInfo: FileInfo): File = File.createTempFile(fileInfo.fileName, ".tmp")
val routes: Route = fileUpload
@POST
@Consumes(Array(MediaType.MULTIPART_FORM_DATA))
@Path("upload")
@Operation(
summary = "File upload",
description = "Upload file",
requestBody = new RequestBody(
description = "File",
content = Array(
new Content(
mediaType = MediaType.MULTIPART_FORM_DATA,
schema = new Schema(implementation = classOf[FileUpload])
)
)
),
responses = Array(
new ApiResponse(
responseCode = "200",
description = "File uploaded",
),
new ApiResponse(responseCode = "400", description = "Bad request"),
new ApiResponse(responseCode = "500", description = "Internal server error")
)
)
def fileUpload: Route = post {
path("upload") {
storeUploadedFile("file", tempDestination) {
case (metadata, file) =>
// do something with the file and file metadata ...
println("Metadata: " + metadata)
println("File: " + file)
complete(StatusCodes.OK, "Uploaded")
}
}
}
}

使用依赖项:

lazy val akkaHttpVersion = "10.2.1"
lazy val akkaVersion = "2.6.10"
lazy val swaggerVersion = "2.1.5"
lazy val jacksonVersion = "2.11.3"
lazy val root = (project in file("."))
.enablePlugins(JavaAppPackaging)
.settings(
inThisBuild(List(organization := "com.demo.erp", scalaVersion := "2.13.4")),
name := "demo-erp",
libraryDependencies ++= Seq(
"com.typesafe.akka"            %% "akka-http"                 % akkaHttpVersion,
"com.typesafe.akka"            %% "akka-http-spray-json"      % akkaHttpVersion,
"com.typesafe.akka"            %% "akka-actor-typed"          % akkaVersion,
"com.typesafe.akka"            %% "akka-stream"               % akkaVersion,
"com.typesafe.play"            %% "play-json"                 % "2.9.1",
"org.mongodb.scala"            %% "mongo-scala-driver"        % "4.1.1",
"ch.qos.logback"               % "logback-classic"            % "1.2.3",
"ch.rasc"                      % "bsoncodec"                  % "1.0.1",
"com.github.pjfanning"         %% "scala-faker"               % "0.5.0",
"javax.ws.rs"                  % "javax.ws.rs-api"            % "2.1.1",
"com.github.swagger-akka-http" %% "swagger-akka-http"         % "2.2.0",
"com.github.swagger-akka-http" %% "swagger-scala-module"      % "2.1.3",
"com.github.swagger-akka-http" %% "swagger-enumeratum-module" % "2.0.0",
"com.fasterxml.jackson.module" %% "jackson-module-scala"      % jacksonVersion,
"pl.iterators"                 %% "kebs-spray-json"           % "1.8.1",
"io.swagger.core.v3"           % "swagger-core"               % swaggerVersion,
"io.swagger.core.v3"           % "swagger-annotations"        % swaggerVersion,
"io.swagger.core.v3"           % "swagger-models"             % swaggerVersion,
"io.swagger.core.v3"           % "swagger-jaxrs2"             % swaggerVersion,
"ch.megard"                    %% "akka-http-cors"            % "1.1.0",
"com.typesafe.akka"            %% "akka-http-testkit"         % akkaHttpVersion % Test,
"com.typesafe.akka"            %% "akka-actor-testkit-typed"  % akkaVersion % Test,
"org.scalatest"                %% "scalatest"                 % "3.2.3" % Test
)
)

最新更新