0

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.

@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:

 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?

Alex Elkin
  • 574
  • 6
  • 11

1 Answers1

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")
  ))
Raman Mishra
  • 2,635
  • 2
  • 15
  • 32