Swagger-Akka-Http: List of objects in the request body

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Swagger-Akka-Http: List of objects in the request body



I'm using swagger-akka-http to built Swagger docs for my Akka HTTP service.



My service has a POST method accepting a List of Characteristic.


List


Characteristic


@ApiOperation(value = "Fetch offerings by characteristics", httpMethod = "POST")
@ApiImplicitParams(Array(
new ApiImplicitParam(name = "characteristics", required = true,
dataTypeClass = classOf[List[Characteristic]], paramType = "body")
))
@ApiResponses(Array(
new ApiResponse(code = 200, response = classOf[Offering], responseContainer = "List")
))
def fetchOfferings: Route = post {
entity(as[List[Characteristic]]) { characteristics =>
// some logic
}
}



dataTypeClass = classOf[List[Characteristic]] in ApiImplicitParams is not working as expected. There is the following result in the generated Swagger YAML:


dataTypeClass = classOf[List[Characteristic]]


ApiImplicitParams


parameters:
- in: "body"
name: "body"
description: "Characteristics"
required: true
schema:
type: "array"
items:
type: "object"



How can I doccument a collection of objects in the request body?





why don't you do it this way case class Characteristics[List[Object]] and give type as Characteristics?
– Raman Mishra
13 mins ago




1 Answer
1



you can do it like this, to accept a list of objects in the post request.


@ApiModel(value = "Request object")
case class Request(
@(ApiModelProperty@field)(
value = "Name",
name = "name",
required = true,
dataType = "string",
example = "DE",
allowEmptyValue = false)
name: String,
@(ApiModelProperty@field)(
value = "Marks",
name = "marks",
required = true,
dataType = "List[integer]",
example = "[10]",
allowEmptyValue = false)
marks: List[Int])



On your post route you can do something like this.


@Path("/student")
@ApiOperation(
value = "Returns student information POST request",
nickname = "getStudentDetails",
httpMethod = "POST",
responseContainer = "List",
code = 200,
response = classOf[StudentDetails])
@ApiImplicitParams(Array(
new ApiImplicitParam(
name = "body",
required = true,
dataType = "Request",
paramType = "body")
))






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Visual Studio Code: How to configure includePath for better IntelliSense results

Spring cloud config client Could not locate PropertySource

Regex - How to capture all iterations of a repeating pattern?