Swagger resource annoations @ApiResponse duplicate - how to reduce the duplication?

1,487 views
Skip to first unread message

Fan Zhao

unread,
May 12, 2014, 12:30:44 PM5/12/14
to swagger-sw...@googlegroups.com
Hello all,

Swagger is running well for our project currently. But since there are multiple methods, and they have very similar response codes and messages, we have to annotate each of them with the same set of @ApiResponse repetitively. The code is like:

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "XXXX", response = XXXX.class)
    @ApiResponses(value = {
            @ApiResponse(code = HttpURLConnection.HTTP_OK, message = MESSAGE_OK),
            @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = MESSAGE_INVALID_ID),
            @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = MESSAGE_ACCESS_DENIED),
            @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = MESSAGE_NO_TOPIC) })

    public XXX get(@ApiParam(value = "XXX id", required = true) @PathParam("id") final String id) {
        ...
    }

    @PUT
    @Path("/{id}")
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "XXXX", response = XXX.class)
    @ApiResponses(value = {
            @ApiResponse(code = HttpURLConnection.HTTP_OK, message = MESSAGE_OK),
            @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = MESSAGE_INVALID_ID),
            @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = MESSAGE_ACCESS_DENIED),
            @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = MESSAGE_NO_TOPIC) })

    public XXXX put(@ApiParam(value = "XXX id", required = true) @PathParam("id") String id, @ApiParam(value = "XXX object", required = true) @NotNull final XXXX xxxx) {
      ...
    }

There is a way that we can have something like an annotations wrapper to bundle the duplicate annotations so that we do only one annotation for each method?

It could be like @ApiResponses(value = {@ApiResponseBundle()}).

Thank you!

Ron

unread,
May 12, 2014, 12:32:54 PM5/12/14
to swagger-sw...@googlegroups.com


--
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.

Fan Zhao

unread,
May 12, 2014, 12:58:42 PM5/12/14
to swagger-sw...@googlegroups.com
Hi,

This is just for code clean because I have code cleanliness. I prefer not to have duplicate and cumbersome code indicated above highlighted in bold. Is there some way we can eliminate it?

@ApiResponses(value = {@ApiResponseBundle()})

equals

@ApiResponses(value = {
            @ApiResponse(code = HttpURLConnection.HTTP_OK, message = MESSAGE_OK),
            @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = MESSAGE_INVALID_ID),
            @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = MESSAGE_ACCESS_DENIED),
            @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = MESSAGE_NO_TOPIC) })

or we have a way to encapsulate

            @ApiResponse(code = HttpURLConnection.HTTP_OK, message = MESSAGE_OK),
            @ApiResponse(code = HttpURLConnection.HTTP_BAD_REQUEST, message = MESSAGE_INVALID_ID),
            @ApiResponse(code = HttpURLConnection.HTTP_FORBIDDEN, message = MESSAGE_ACCESS_DENIED),
            @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = MESSAGE_NO_TOPIC)

into a very simple one like a magic variable.



--
You received this message because you are subscribed to a topic in the Google Groups "Swagger" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/swagger-swaggersocket/Pck3jpmMmI8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to swagger-swaggers...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Fan Zhao or Phillip Zhao

Steven Goris

unread,
Feb 5, 2016, 9:18:04 AM2/5/16
to Swagger
I actually had the same question. Especially for the responses, but also for our headers in ApiImplicitParams. Has anyone found a solution already?

Op maandag 12 mei 2014 18:30:44 UTC+2 schreef Fan Zhao:

Ron Ratovsky

unread,
Feb 5, 2016, 9:27:08 AM2/5/16
to swagger-sw...@googlegroups.com
You should be able to define your own annotation that’s annotated with swagger-core’s annotations as required, and use only that.

Steven Goris

unread,
Feb 8, 2016, 9:13:32 AM2/8/16
to Swagger
I tried creating an annotation annotated with @ApiResponses, but this didn't work. They were just ignored, is this a bug with the swagger-maven-plugin?
For ApiImplicitParams, I couldn't even add those on my custom annotation because the targetType is METHOD.


Op maandag 12 mei 2014 18:30:44 UTC+2 schreef Fan Zhao:
Hello all,
Op maandag 12 mei 2014 18:30:44 UTC+2 schreef Fan Zhao:
Hello all,
Op maandag 12 mei 2014 18:30:44 UTC+2 schreef Fan Zhao:
Hello all,

Ron Ratovsky

unread,
Feb 8, 2016, 10:30:54 AM2/8/16
to swagger-sw...@googlegroups.com
No idea. You’d have to open a ticket on that project.

From: <swagger-sw...@googlegroups.com> on behalf of Steven Goris <sg.st...@gmail.com>
Reply-To: "swagger-sw...@googlegroups.com" <swagger-sw...@googlegroups.com>
Date: Monday, 8 February 2016 at 07:13
To: Swagger <swagger-sw...@googlegroups.com>
Subject: Re: Swagger resource annoations @ApiResponse duplicate - how to reduce the duplication?

Reply all
Reply to author
Forward
0 new messages