Skip to content

Commit 00c7f4a

Browse files
committed
Fixed issue 1746 - Api and ApiImplicitParam annotations not read when they are in a parent of the resource class
Also credit to mohitmutha for finding a solution to a similar problem and captdestrukto to provide a pull request for that one
1 parent b3b11b8 commit 00c7f4a

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

modules/swagger-jaxrs/src/main/java/io/swagger/jaxrs/Reader.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ private Swagger read(Class<?> cls, String parentPath, String parentMethod, boole
187187
String[] produces = new String[0];
188188
final Set<Scheme> globalSchemes = EnumSet.noneOf(Scheme.class);
189189

190-
Api api = (Api) cls.getAnnotation(Api.class);
190+
Api api = ReflectionUtils.getAnnotation(cls, Api.class);
191191

192192
boolean hasPathAnnotation = (ReflectionUtils.getAnnotation(cls, javax.ws.rs.Path.class) != null);
193193
boolean hasApiAnnotation = (api != null);
@@ -410,7 +410,7 @@ private Swagger read(Class<?> cls, String parentPath, String parentMethod, boole
410410
}
411411

412412
private void readImplicitParameters(Method method, Operation operation) {
413-
ApiImplicitParams implicitParams = method.getAnnotation(ApiImplicitParams.class);
413+
ApiImplicitParams implicitParams = ReflectionUtils.getAnnotation(method, ApiImplicitParams.class);
414414
if (implicitParams != null && implicitParams.value().length > 0) {
415415
for (ApiImplicitParam param : implicitParams.value()) {
416416
Parameter p = readImplicitParam(param);

modules/swagger-jaxrs/src/test/java/io/swagger/ReaderTest.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.swagger.jaxrs.Reader;
44
import io.swagger.models.Operation;
55
import io.swagger.models.Swagger;
6+
import io.swagger.models.Tag;
67
import io.swagger.models.parameters.*;
78
import io.swagger.resources.*;
89
import org.testng.annotations.Test;
@@ -316,6 +317,27 @@ public void scanDeclaredExceptionsAndCombineWithMethodResponsesClassLevel() {
316317

317318
}
318319

320+
@Test(description = "scan resource (impl) which has the Api annotations only declared in its interface")
321+
public void scanApiAnnotationWhichAreOnlyPresentInInterfaceAndNotInImplementation() {
322+
Swagger swagger = getSwagger(ResourceWithAnnotationsOnlyInInterfaceImpl.class);
323+
assertNotNull(swagger);
324+
325+
final List<Tag> tags = swagger.getTags();
326+
assertEquals(tags.size(), 1);
327+
assertEquals(tags.get(0).getName(), "someTag");
328+
}
329+
330+
@Test(description = "scan resource (impl) which has the ApiParam annotations only declared in its interface")
331+
public void scanApiImplicitParamAnnotationWhichAreOnlyPresentInInterfaceAndNotInImplementation() {
332+
Swagger swagger = getSwagger(ResourceWithAnnotationsOnlyInInterfaceImpl.class);
333+
assertNotNull(swagger);
334+
335+
List<Parameter> parameters = getGet(swagger, "/pet/randomPet").getParameters();
336+
assertNotNull(parameters);
337+
assertEquals(parameters.size(), 1);
338+
assertEquals(parameters.get(0).getName(), "petImplicitIdParam");
339+
}
340+
319341
private Swagger getSwagger(Class<?> cls) {
320342
return new Reader(new Swagger()).read(cls);
321343
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.swagger.resources;
2+
3+
import io.swagger.annotations.Api;
4+
import io.swagger.annotations.ApiImplicitParam;
5+
import io.swagger.annotations.ApiImplicitParams;
6+
import io.swagger.annotations.ApiOperation;
7+
8+
import javax.ws.rs.GET;
9+
import javax.ws.rs.Path;
10+
11+
@Path("/pet")
12+
@Api(tags = "someTag")
13+
public interface ResourceWithAnnotationsOnlyInInterface {
14+
15+
@GET
16+
@Path("/randomPet")
17+
@ApiOperation(value = "getRandomPet")
18+
@ApiImplicitParams({ @ApiImplicitParam(name = "petImplicitIdParam", paramType = "query", dataType = "string") })
19+
String getRandomPet();
20+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.swagger.resources;
2+
3+
public class ResourceWithAnnotationsOnlyInInterfaceImpl implements ResourceWithAnnotationsOnlyInInterface {
4+
5+
@Override
6+
public String getRandomPet() {
7+
return "No pet today..";
8+
}
9+
}

0 commit comments

Comments
 (0)