I have a problem. I want to send a list of objects in the response of an API using Swagger. type
= "array" does not work for me.
I saw a topic Set List of Objects in Swagger API response , but it is an old version of lib. Annotation has changed. ApiResponse used to have responseContainer param, but now it is gone.
I have akka-http
server.
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"
)
I create get route and describe it with swagger annotations.
@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)
and it builds 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"
}
}
},