swagger-core icon indicating copy to clipboard operation
swagger-core copied to clipboard

@ApiOperation responseClass as Class<T> support

Open ben-manes opened this issue 13 years ago • 15 comments

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>> {}

ben-manes avatar May 01 '13 00:05 ben-manes

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.

btiernay avatar May 12 '13 18:05 btiernay

+1

gaffa avatar May 15 '13 06:05 gaffa

+1

leewin12 avatar May 18 '13 13:05 leewin12

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.

fehguy avatar May 23 '13 00:05 fehguy

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?

btiernay avatar May 23 '13 01:05 btiernay

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

fehguy avatar May 23 '13 01:05 fehguy

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

fehguy avatar May 28 '13 04:05 fehguy

supported in 1.3.0-SNAPSHOT

fehguy avatar May 31 '13 16:05 fehguy

Soooooo great!! Thanks alot!!

leewin12 avatar May 31 '13 17:05 leewin12

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 avatar Jul 14 '14 17:07 raspacorp

@raspacorp it should work. What do you get?

webron avatar Jul 14 '14 17:07 webron

@raspacorp responseContainer was never part of @ApiResponse but rather of @ApiOperation

webron avatar Jul 14 '14 18:07 webron

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?

raspacorp avatar Jul 14 '14 20:07 raspacorp

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.

webron avatar Jul 14 '14 20:07 webron

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);
        }
    }

zhouxuan323 avatar Sep 15 '18 08:09 zhouxuan323