When generating swagger documentation for lists of enums in our scala apps, the swagger definitions generated are only empty objects. When generating swagger documentation for enums everything is as expected, it is only when dealing with lists of enums.
In scala there are multiple ways of creating enums, our codebase uses this way:
sealed trait AnswerEnum { val value: String }
case object YES extends AnswerEnum { val value = "YES" }
case object NO extends AnswerEnum { val value = "NO" }
If we imagine that we have an API that only has one endpoint where the expected request looks something like this:
case class SomeRequest(answers: Seq[AnswerEnum])
And we want to generate some swagger so we annotate it so it looks like this:
@ApiModel(description = "Example Request")
case class SomeRequest(
@ApiModelProperty(value = "List of answers", required = true) answers: Seq[AnswerEnum]
)
We are then left with a definition in our swagger which looks like this:
"AnswerEnum": {
"type": "object"
}
While what we are looking to get is something like this:
"AnswerEnum": {
"type": "string",
"description": "",
"enum": ["YES", "NO"]
}
If we try to annotate the sealed trait above like this:
@ApiModel(description = "Enum for Yes/No answers")
sealed trait AnswerEnum {
val value: String
@ApiModelProperty(value = "Yes/No Answers", dataType = "string", allowableValues = "YES, NO")
def getAnswerEnum: String = value
}
We end up with a swagger definition looking closer to what we want but not how we want it or how it should be:
"AnswerEnum": {
"type": "object",
"properties": {
"AnswerEnum": {
"type": "string",
"description": "Yes/No Answers",
"enum": ["YES", "NO"]
}
},
"description": "Enum for Yes/No answers"
}
This is the closest we have gotten to getting it correct, but it is still very bad and not correct at all.
If we do something like this, we end up at our current solution, which is not a good solution, but it works:
@ApiModel(description = "Example Request")
case class SomeRequest(
@ApiModelProperty(value = "List of answers", dataType = "[Ljava.lang.String;", required = true, allowableValues = "YES, NO") answers: Seq[AnswerEnum]
)
The generated swagger for SomeRequest is at least somewhat correct, in that it generates something which says that the answers are a list of strings, but it doesn't say that it is an enum with the allowable values, however it looks like this:
"SomeRequest": {
"type": "object",
"required": [
"answers"
],
"properties": {
"answers": {
"type": "array",
"description": "List of answers",
"items": {
"type": "string"
}
}
},
"description": "Example Request"
}
We are using
- https://mvnrepository.com/artifact/com.github.swagger-akka-http/swagger-akka-http_2.12/1.0.0
- https://mvnrepository.com/artifact/io.swagger/swagger-jaxrs/1.5.21
to generate the swagger.