I was able to setup Swagger with Play and try Swagger-ui... and I must say it's really great.
Documenting the actions of my controller with ApiOperation
, ApiImplicitPara
, etc. was easy and it works as expected.
Nevertheless, due to my limited knowledge of Swagger, I've an issue when defining the schema for an implicit parameter of type body
. The class I want to map to the implicit parameter looks like this:
@ApiModel(value "User", description = "Represents an user")
class User private(private var json: JsValue) {
private def setValue(key: JsPath, value: JsValue) = {
value match {
case JsNull => json.transform(key.json.prune).map(t => json = t)
case _ => json.transform((__.json.update(key.json.put(value)))).map(t => json = t)
}
}
def asJson = json
@ApiModelProperty(value = "User's id", dataType = "String", required = false)
def id_= (v: Option[String]) = setValue((__ \ 'id), Json.toJson(v))
def id = json as (__ \ 'id).readNullable[String]
@ApiModelProperty(value = "User's email address", dataType = "String", required = true)
def email = json as (__ \ 'email).read[String]
def email_= (v: String) = setValue((__ \ 'email), Json.toJson(v))
@ApiModelProperty(value = "User's firstName", dataType = "String", required = true)
def firstName = json as (__ \ 'firstName).read[String]
def firatName_= (v: String) = setValue((__ \ 'firstName), Json.toJson(v))
...
}
object User {
def apply(
id: Option[String],
email: String,
firstName: String,
): JsResult[User] = apply(Json.obj(
"id" -> id,
"email" -> email,
"firstName" -> firstName,
...
))
}
The internal representation of my model classes is JSON... and then I just provide getters and setters that read/modify the internal JSON – this solution let me handle JSON very quickly and I can pass the object as-is to MongoDB.
The problem is that the model generated by Swagger for the class above is like this:
User {
json(JsValue),
id(String): User's id,
email(String): User's email,
firstName(String): User's first name,
...
}
How do I prevent Swagger from putting json
in the model?
Try this:
@ApiModelProperty(required = false, hidden = true)
def asJson = json
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With