sebi
sebi

Reputation: 1841

Specifying Swagger response type

I have the following controller method exposed as a REST service with Play Framework. I am using Swagger to generate a description of the service.

@ApiOperation(
nickname = "..",
value = "..",
response = classOf[???], 
httpMethod = "GET")
def dateBasedCampaignsSummaryReport(
  profileId: Int,
  start: Int,
  size: Int,
  filterZeroSent: Boolean = false) = {

  import JsonConverters.dateBasedAutomationSummaryWrites

  automationService
    .dateBasedCampaignsSummaryReport(profileId, start, size, filterZeroSent)
    .map { res => Ok(Json.toJson(res)) }
}

This method transforms an object of type DateBasedAutomationSummary, that is returned by automationService, into a json using an implicit Write:

implicit def dateBasedAutomationSummaryWrites: Writes[DateBasedAutomationSummary] =
(
  (__ \ 'name).write[String] and
  (__ \ 'dateTime).write[String] and
  (__ \ 'isInProgress).write[Boolean])(as =>
    (
      as.name,
      dateTime2String(as.scheduledDateTime, "d MMM YYYY 'at' HH:mm"),       
      as.isInProgress))

Swagger asks me for a type to use with the response attribute. Because I use a Write object to create my json, the structure of my original type (DateBasedAutomationSummary) gets changed and I can not use it. I would like to use the tuple that I am using inside the dateBasedAutomationSummaryWrites method . Or something similar. Is there a way to do this?

Note

  1. DateBasedAutomationSummary is a simple class and the json structure is similar. I do have other classes that are more complex and get changed far more.
  2. I would not like to create a new layer of view model classes for all json representations.

Thanks!

Upvotes: 0

Views: 1173

Answers (1)

Ron
Ron

Reputation: 14810

You can register custom converters for your types - take a look here - https://github.com/swagger-api/swagger-core/wiki/overriding-models#excluding-fields-with-a-custom-model-converter-131-and-greater.

You may be able to use the Write for the custom converter as well, otherwise you'd have to add the business logic explicitly.

Upvotes: 1

Related Questions