@ApiOperation responseClass as Class<T> support
Reopening #71
Could you use the type token pattern (aka Gafter's Gadget)? This is popular in Google Guice to register bindings with generics.
@GET @Path("/words")
@ApiResponse(responseClass = ListOfWords.class)
public List<Word> getWords() {...}
static final class ListOfWords extends TypeToken<List<Word>> {}
Yeah, this is highly desirable feature. Right now refactoring will invalidate all of these references. Class literals is certainly the way to go. As Ben points out, this is a popular idiom. Guice and Jackson both use this pattern.
+1
+1
This is great, but breaks compatibility with scala versions < 2.10, see here:
https://issues.scala-lang.org/browse/SI-2453
Not that becomes a showstopper, but a potential solution might be this:
@ApiResponse(
responseClass = Word.class,
responseContainer = "List"
)
The inner response type is now typesafe, and safe for refactoring. The container (List, Array, Set) are string values, which is not my favorite thing to do but I believe it'd solve most of the issues from the whole response class being a string.
Finally, Map is of course an issue since there are potentially two classes required (swagger tools don't support map for this very reason). For Map, I'd consider supporting keys of type String only, and setting the value in the responseClass.
Would that work well? I'm trying to push a 1.3 development version this week, and this is an important fix.
Damn you Scala! J/k :)
Well, if that's the best we can do, it's certainly an improvement.
Is it a requirement to support Scala < 2.10?
Actually, this pattern may be fine, and using the List container annotation could be a fallback for prehistoric versions of scala. Will test tonight with Type Token support in java
OK this is now in a 1.3-develop branch, please see here:
https://github.com/wordnik/swagger-core/tree/develop-1.3/samples/scala-jaxrs
You'll see the response annotations are now in this form:
@ApiOperation(value = "Find pet by ID",
notes = "Returns a pet based on ID",
response = classOf[Pet])
or in java:
@ApiOperation(value = "Find pet by ID",
notes = "Returns a pet based on ID",
response = Pet.class)
Should be pushed to master shortly. Feedback welcome! I also plan to support the Gafter's Gadget syntax as well before releasing 1.3
supported in 1.3.0-SNAPSHOT
Soooooo great!! Thanks alot!!
So, what happened to the responseContainer = "List" inside @ApiResponse I am using swagger-jersey2-jaxrs_2.10 version 1.3.5 and the annotation does not support it. I need to return a list of domain objects
@raspacorp it should work. What do you get?
@raspacorp responseContainer was never part of @ApiResponse but rather of @ApiOperation
Ah ok, I got confused as in this post they mention it. A question, then how do we set a list of objects as the response to an @ApiResponse annotation?
You don't. Currently, it is not supported by the spec. The next version of the spec organizes these things better and you'd be able to return a list of object as well. At most, what you can currently do is return an object (model) that contains a list.
use extend Response?
public static class UserResponse extends Response<User> {
@Override
public User getResult() {
return super.getResult();
}
@Override
public void setResult(User result) {
super.setResult(result);
}
}