如何在生成的Swagger规范中包含超类中的字段(使用Scalatra + Swagger)?

时间:2017-07-04 10:42:58

标签: scala swagger scalatra

我有这个案例类正在扩展一个抽象类:

@ApiModel(description = "A price for an offer.")
case class OfferPrice(
                       override val amount: Double,
                       override val taxAmount: Double,
                       override val taxRate: Option[Double]
                     ) extends Price(amount, taxAmount, taxRate)

abstract class Price(
                      @(ApiModelProperty@field)(description = "The amount.") val amount: Double,
                      @(ApiModelProperty@field)(description = "The tax amount.") val taxAmount: Double,
                      @(ApiModelProperty@field)(description = "The tax rate.") val taxRate: Option[Double]
                    )

令人兴奋的东西,对吗?我的问题是生成的swagger.json文件中的定义如下所示:

"OfferPrice": {
  "properties": {

  }
}

它不包括抽象类中的字段。我怎样才能包含这些字段?

1 个答案:

答案 0 :(得分:1)

它不起作用,因为在超类中声明的字段和注释被子类中重写的字段隐藏。

我认为这是您模型的正确定义:

@ApiModel(description = "A price for an offer.")
case class OfferPrice(
  @ApiModelProperty(description = "The amount.") amount: Double,
  @ApiModelProperty(description = "The tax amount.") taxAmount: Double,
  @ApiModelProperty(description = "The tax rate.") taxRate: Option[Double]
) extends Price(amount, taxAmount, taxRate)

abstract class Price(
  amount: Double,
  taxAmount: Double,
  taxRate: Option[Double]
)

但目前Scalatra的Swagger 2.0支持中未呈现模型和属性的描述。它将在未来版本中得到支持。请参阅:https://github.com/scalatra/scalatra/issues/684