Reputation: 1841
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
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.Thanks!
Upvotes: 0
Views: 1173
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