scalafunctional-programmingswagger-uiplayframework-2.6swagger-play2

Swagger-play2: How to allow array of objects as input in swagger ui using @ApiImplicitParams annotation?


I am trying to write swagger API for a method which takes an array of case class objects. I have done something as follows:

package models

import io.swagger.annotations.{ApiModel, ApiModelProperty}
import play.api.data.Form
import play.api.data.Forms.{mapping, nonEmptyText, seq}
import play.api.libs.json.{Json, Writes}
import spray.json.DefaultJsonProtocol

object Message extends DefaultJsonProtocol {

@ApiModel(value = "EmailMessage")
  case class EmailMessage(
                           @(ApiModelProperty@field)(position = 1, dataType = "array", required = true) emails: Seq[String],
                           @(ApiModelProperty@field)(position = 2, required = true) message: String
                         )

}

And on controller's request handler method

@ApiImplicitParams(Array(
    new ApiImplicitParam(value = "EmailMessage", dataType = "models.Message.EmailMessage",
      paramType = "body", name = "body", required = true)
  ))

Now when I am trying to access this API I am getting following exceptions:

[error] application - Failed to resolve 'models.Message.EmailMessage' into class
java.lang.ClassNotFoundException: models.Message.EmailMessage
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at play.modules.swagger.PlayReader.typeFromString(PlayReader.java:447)
    at play.modules.swagger.PlayReader.readImplicitParam(PlayReader.java:423)
    at play.modules.swagger.PlayReader.readImplicitParameters(PlayReader.java:396)
    at play.modules.swagger.PlayReader.read(PlayReader.java:199)
    at play.modules.swagger.PlayReader.read(PlayReader.java:64)
    at play.modules.swagger.PlayReader.read(PlayReader.java:58)
    at play.modules.swagger.ApiListingCache$$anonfun$listing$1.apply(ApiListingCache.scala:17)
[info] swagger - Swagger - initialization done.


Solution

  • Well i figured it out. What I did is I took out EmailMessage case class from Message object.

    
        @ApiModel(value = "EmailMessage")
          case class EmailMessage(
                                   @(ApiModelProperty@field)(position = 1, 
        dataType = "List[String]", required = true) emails: Seq[String],
                                   @(ApiModelProperty@field)(position = 2, 
        required = true) message: String
                                 )
    
        object Message extends DefaultJsonProtocol {
    
        val EmailMessageForm = Form(
            mapping(
              "emails" -> seq(nonEmptyText),
              "message" -> nonEmptyText
            )(EmailMessage.apply)(EmailMessage.unapply)
          )
    
        }
    
    

    And in @ApiImplicitParam, I changed dataType attribute's value as follows

        @ApiImplicitParams(Array(
            new ApiImplicitParam(value = "EmailMessage payload data", dataType 
        = "models.EmailMessage",
          paramType = "body", name = "body", required = true)
          ))
    

    Now app is running without any error.

    But I was not getting nice user input interface when I render swagger.json to OpenAPI. Please let me know if any one has any idea about it.