Search code examples
scalaswaggerscalatra

How can I include fields from a superclass in the generated Swagger spec (using Scalatra+Swagger)?


I have this case class that's extending an abstract class:

@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]
                    )

Exciting stuff, right? My problem is that the definition in the generated swagger.json file looks like this:

"OfferPrice": {
  "properties": {

  }
}

It's not including the fields from the abstract class. How can I include those fields as well?


Solution

  • It wouldn't work because fields and annotations declared in the super class are hidden by fields overridden in the subclass.

    I think this is correct definition of your model:

    @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]
    )
    

    but description of model and properties is not rendered in Scalatra's Swagger 2.0 support currently. It will be supported in the future release. See: https://github.com/scalatra/scalatra/issues/684