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

3xiyfsfu  于 2022-11-23  发布在  其他
关注(0)|答案(2)|浏览(117)

我遇到了一个问题。我想使用Swagger在API的响应中发送一个对象列表。type =“array”对我不起作用。我看到了一个主题Set List of Objects in Swagger API response,但它是一个旧版本的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"
)

我创建了getroute并使用swagger注解对其进行了描述。

@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"
      }
    }
  },
ddrv8njm

ddrv8njm1#

我找到了解决办法:

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

rryofs0p2#

这似乎适用于我从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
    )
  )

相关问题