Swagger with APIs that use Java Generics

8,797 views
Skip to first unread message

ZenM

unread,
Feb 11, 2014, 8:06:26 AM2/11/14
to swagger-sw...@googlegroups.com
Hi,

I have a problem that is very much like the one posted here: https://groups.google.com/forum/#!msg/swagger-swaggersocket/eO2d6qD9K9g/cOjfc2tzMVsJ . However, I am not able to find the final solution talked about in that thread. Since it is an old thread, I am reposting the question here.

I am trying to use Swagger for documenting the APIs of my Dropwizard WebService. Both the responses as well as requests in the API endpoints of my webservice use Java generics. For example, one of my "list" API returns a response of type PagedResponse<T>. In the Swagger UI, this shows up as follows under the Response Model heading:

PagedResponse {
   totalEntries (integer, optional),
   message (string, optional),
   data (T, optional),
   status (string, optional)
}

The object type of "data" field will vary depending upon the resource that is listed, hence I'd like to find a way to tell Swagger this information so that it can accordingly show the Response Model. From reading other threads, it seems like the only way to do this is to "manually map the class to a concrete representation.". Can someone please tell me how to go about doing that for Java generics in a Dropwizard WS?

Any help is greatly appreciated. Thanks in advance.




tony tam

unread,
Feb 13, 2014, 10:26:13 AM2/13/14
to swagger-sw...@googlegroups.com
Hi, I'd suggest looking at this wiki page:


Because of type erasure, it's impossible in some cases to build the model for generics.

ZenM

unread,
Feb 14, 2014, 1:07:40 AM2/14/14
to swagger-sw...@googlegroups.com
Hi Tony,

Thanks for the reply. I did look at that page before I asked the question. I think my situation is like the one that you talk about and I don't think overriding models would help in this case as I intend to use the said object as a wrapper for many of my responses and the "data" field will be a different class in each resource I support in my APIs.

I was wondering, instead of depending on the class to get the type (which makes it difficult to get the correct type due to type erasure), can Swagger support something like the following:

Instead of specifying:

response=Response.class

inside the annotations, wouldn't it be better if this took a String value instead where we could specify the type parameter classes like:

response="Response<Pet>" or response="Response<List<Pet>>"

This way swagger could get the generic class from the String value instead of having to depend on the class file.

I see something like this being discussed in this link: https://github.com/wordnik/swagger-core/pull/210/files but it seems this is not present in the Swagger 2.10-1.3.0 jar which is what is specified on the Swagger Dropwizard Quickstart page.

Thanks for your inputs.

Ron

unread,
Feb 15, 2014, 12:38:00 PM2/15/14
to swagger-sw...@googlegroups.com
Hi,

While not impossible to implement (obviously), this can lead to a whole set of issues with regards to parsing.
I definitely agree we should find a way to solve this use case, I'm just not sure this is the way.


--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

ZenM

unread,
Feb 18, 2014, 1:07:20 AM2/18/14
to swagger-sw...@googlegroups.com
Hi Ron,

Please excuse my delayed reply. I didn't have access to the internet for a couple of days.

Sure. I can see some problems as well.

At the moment, I've removed the Swagger intergration from my Dropwizard project and opted for the no-server route. We've kind of hand-coded (or rather generated the JSONs and modified them) the JSONs that will be used by the Swagger UI.

Thanks for the answers folks! And for the great product!

Ioannis Sermetziadis

unread,
Jul 17, 2014, 10:12:39 AM7/17/14
to swagger-sw...@googlegroups.com
Hello,

I had the same issue, so I found a work-around that I would like to share.

I had a class with generic type named CommonResponse<T>, that I wanted to use as the response type for swagger.
I defined an extra class, named UserResponse that extends CommonResponse<User> and provides a getter for User instance variable, and it seems to work as expected now.

Good luck!

Ron

unread,
Jul 17, 2014, 10:16:11 AM7/17/14
to swagger-sw...@googlegroups.com
Thanks for sharing!


--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages