Skip to content

Commit 7803487

Browse files
committed
updated tag support for #841
1 parent 7b27439 commit 7803487

File tree

3 files changed

+83
-6
lines changed

3 files changed

+83
-6
lines changed

modules/swagger-jaxrs/src/main/java/com/wordnik/swagger/jaxrs/Reader.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public Swagger read(Class cls) {
8282
}
8383

8484
if(api != null) {
85-
List<String> tagStrings = new ArrayList<String>();
85+
Set<String> tagStrings = new HashSet<String>();
8686
// the value will be used as a tag for 2.0 UNLESS a Tags annotation is present
8787
com.wordnik.swagger.annotations.Tag[] tags = api.tags();
8888
boolean hasExplicitTags = false;
@@ -155,6 +155,27 @@ public Swagger read(Class cls) {
155155
break;
156156

157157
Operation operation = parseMethod(method);
158+
159+
ApiOperation op = (ApiOperation) method.getAnnotation(ApiOperation.class);
160+
if(op != null) {
161+
com.wordnik.swagger.annotations.Tag[] operationTags = op.tags();
162+
boolean hasExplicitTag = false;
163+
for(com.wordnik.swagger.annotations.Tag tag : operationTags) {
164+
if(!"".equals(tag.value())) {
165+
operation.tag(tag.value());
166+
if(!tagStrings.contains(tag.value())) {
167+
Tag tagObject = new Tag()
168+
.name(tag.value())
169+
.description(tag.description());
170+
171+
if(tag.externalDocs() != null && !"".equals(tag.externalDocs().value()))
172+
tagObject.externalDocs(
173+
new ExternalDocs(tag.externalDocs().value(), tag.externalDocs().url()));
174+
swagger.tag(tagObject);
175+
}
176+
}
177+
}
178+
}
158179
if(operation != null) {
159180
if(operation.getConsumes() == null)
160181
for(String mediaType: apiConsumes)
@@ -163,8 +184,10 @@ public Swagger read(Class cls) {
163184
for(String mediaType: apiProduces)
164185
operation.produces(mediaType);
165186

166-
for(String tagString : tagStrings)
167-
operation.tag(tagString);
187+
if(operation.getTags() == null) {
188+
for(String tagString : tagStrings)
189+
operation.tag(tagString);
190+
}
168191
for(SecurityRequirement security : securities)
169192
operation.security(security);
170193
Path path = swagger.getPath(operationPath);
@@ -198,6 +221,8 @@ String getPath(javax.ws.rs.Path classLevelPath, javax.ws.rs.Path methodLevelPath
198221
b.append(methodPath);
199222
}
200223
String output = b.toString();
224+
if(!output.startsWith("/"))
225+
output = "/" + output;
201226
if(output.endsWith("/") && output.length() > 1)
202227
return output.substring(0, output.length() - 1);
203228
else
@@ -217,9 +242,11 @@ public Operation parseMethod(Method method) {
217242
if(apiOperation != null) {
218243
if(!"".equals(apiOperation.nickname()))
219244
operationId = method.getName();
245+
220246
operation
221247
.summary(apiOperation.value())
222248
.description(apiOperation.notes());
249+
223250
if(apiOperation.response() != null && !Void.class.equals(apiOperation.response())) {
224251
responseClass = apiOperation.response();
225252
}

modules/swagger-jaxrs/src/test/scala/SimpleScannerTest.scala

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import com.wordnik.swagger.models.Swagger
88
import com.wordnik.swagger.jaxrs.Reader
99
import com.wordnik.swagger.util.Json
1010

11+
import scala.collection.JavaConverters._
12+
1113
import org.junit.runner.RunWith
1214
import org.scalatest.junit.JUnitRunner
1315
import org.scalatest.FlatSpec
@@ -81,7 +83,6 @@ class SimpleScannerTest extends FlatSpec with Matchers {
8183
it should "scan a resource with Response.Status return type per 877" in {
8284
val swagger = new Reader(new Swagger()).read(classOf[Resource877])
8385
val path = swagger.getPaths().get("external/info")
84-
Json.prettyPrint(swagger)
8586

8687
swagger.getTags() should not be (null)
8788
swagger.getTags().size() should be (1)
@@ -90,15 +91,30 @@ class SimpleScannerTest extends FlatSpec with Matchers {
9091
tag.getName() should equal ("externalinfo")
9192
tag.getDescription() should equal ("it's an api")
9293
tag.getExternalDocs() should be (null)
94+
}
9395

96+
it should "scan a resource with tags" in {
97+
val swagger = new Reader(new Swagger()).read(classOf[TaggedResource])
9498
Json.prettyPrint(swagger)
9599
}
96100
}
97101

98102
@RunWith(classOf[JUnitRunner])
99103
class SimpleScannerTest2 extends FlatSpec with Matchers {
100104
it should "scan a resource with tags" in {
101-
val swagger = new Reader(new Swagger()).read(classOf[TaggedResource])
102-
Json.prettyPrint(swagger)
105+
val swagger = new Reader(new Swagger()).read(classOf[Resource841])
106+
swagger.getTags().size() should be (3)
107+
108+
val rootTags = swagger.getPaths().get("/fun").getGet().getTags()
109+
rootTags.size() should be (2)
110+
(rootTags.asScala.toList.toSet & Set("tag1", "tag2")).size should be (2)
111+
112+
val thisTags = swagger.getPaths().get("/fun/this").getGet().getTags()
113+
thisTags.size() should be (1)
114+
(thisTags.asScala.toList.toSet & Set("tag1")).size should be (1)
115+
116+
val thatTags = swagger.getPaths().get("/fun/that").getGet().getTags()
117+
thatTags.size() should be (1)
118+
(thatTags.asScala.toList.toSet & Set("tag2")).size should be (1)
103119
}
104120
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package resources;
2+
3+
import com.wordnik.swagger.annotations.*;
4+
5+
import javax.ws.rs.*;
6+
import javax.ws.rs.core.Response;
7+
8+
import java.util.*;
9+
10+
@Api(value = "/external/info/")
11+
@Path("fun")
12+
public class Resource841 {
13+
@ApiOperation(value="this", tags={@Tag(value = "tag1", description = "the tag 1")})
14+
@GET
15+
@Path("/this")
16+
public Response getThis(@ApiParam(value = "test") ArrayList<String> tenantId) {
17+
return Response.ok().build();
18+
}
19+
20+
@ApiOperation(value="that", tags={@Tag(value = "tag2", description = "the tag 2")})
21+
@GET
22+
@Path("/that")
23+
public Response getThat(@ApiParam(value = "test") ArrayList<String> tenantId) {
24+
return Response.ok().build();
25+
}
26+
27+
@ApiOperation(value="everything", tags={
28+
@Tag(value = "tag1", description = "the tag 1"),
29+
@Tag(value = "tag2", description = "the tag 2")})
30+
@GET
31+
public Response getEverything(@ApiParam(value = "test") ArrayList<String> tenantId) {
32+
return Response.ok().build();
33+
}
34+
}

0 commit comments

Comments
 (0)