如何控制Swagger为类型生成模型/模式的方式

时间:2014-02-24 08:13:16

标签: scala swagger swagger-ui swagger-play2

我能够使用播放设置 Swagger 并尝试使用Swagger-ui ...我必须说它非常棒。

使用ApiOperationApiImplicitPara等记录我的控制器的操作非常简单,并且按预期工作。

然而,由于我对Swagger的了解有限,我在为body类型的隐式参数定义模式时遇到了问题。我想要映射到隐式参数的类如下所示:

@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,
    ...
  ))
}

我的模型类的内部表示是JSON ...然后我只提供读取/修改内部JSON的getter和setter - 这个解决方案让我可以非常快速地处理JSON并且可以将对象原样传递给MongoDB

问题是Swagger为上面的类生成的模型是这样的:

User {
  json(JsValue),
  id(String): User's id,
  email(String): User's email,
  firstName(String): User's first name,
  ...
}

如何阻止Swagger将json放入模型中?

1 个答案:

答案 0 :(得分:1)

试试这个:

 @ApiModelProperty(required = false, hidden = true) 
 def asJson = json