swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

Support Scala Enumeration Types

Open gorgonblot opened this issue 12 years ago • 1 comments

This is related to spray-swagger Issue #1 (https://github.com/gettyimages/spray-swagger/issues/1)

The ApiModel generated from a Scala class with a field that is an Enumeration Value has two problems:

  • The type is shown as a $ref: Value
  • The set of values in the Enumeration are not used to set the allowableValues or enums in the Swagger spec.

Take for example the case class and Enumeration example below:

@ApiModel(description = "Scala model containing an Enumeration Value that is annotated with the dataType of the Enumeration class")
case class SModelWithEnum(
                     @(ApiModelProperty @field)(value = "Textual label") label: Option[String],
                     @(ApiModelProperty @field)(value = "Order Size") orderSize: OrderSize )

case object OrderSize extends Enumeration(0) {
  type OrderSize = Value
  val TALL = Value("TALL")
  val GRANDE = Value("GRANDE")
  val VENTI = Value("VENTI")
}

Currently, the Swagger Spec is generated as:

{
    "description": "Scala model containing an Enumeration Value", 
    "id": "SModelWithEnum", 
    "properties": {
        "label": {
            "description": "Textual label", 
            "type": "string"
        }, 
        "orderSize": {
            "$ref": "Value", 
            "description": "Order Size"
        }
    }
}

The desired behavior is to generate the Swagger Spec as below:

{
  "id" : "SModelWithEnum",
  "description" : "Scala model containing an Enumeration Value",
  "properties" : {
    "label" : {
      "type" : "string",
      "description" : "Textual label"
     },
  "orderSize" : {
    "type" : "string",
    "description" : "Order Size",
    "enum" : ["GRANDE","TALL","VENTI"]
   }
  }
}

Using reflection alone, I don't think it is possible to know that the Enumeration Value is a member of the specific Enumeration object. However, with an additional dataType annotation on the field, the desired Swagger Spec can be generated. For example:

@(ApiModelProperty @field)(value = "Order Size", dataType = "models.OrderSize$") orderSize

I will submit a pull request demonstrating this support.

gorgonblot avatar Nov 19 '14 16:11 gorgonblot

closing as #761 has been merged

fehguy avatar Dec 20 '14 20:12 fehguy