Skip to content

Commit e682287

Browse files
committed
Merge pull request #1747 from janhoeve/master
Fix #1746, Fix #1506 - Api, ApiImplicitParam and Param annotations not read when they are in a parent of the resource class
2 parents 1fdf54c + 00c7f4a commit e682287

File tree

5 files changed

+83
-3
lines changed

5 files changed

+83
-3
lines changed

modules/swagger-core/src/main/java/io/swagger/util/ReflectionUtils.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.swagger.util;
22

33
import com.fasterxml.jackson.databind.type.TypeFactory;
4+
5+
import org.apache.commons.lang3.ArrayUtils;
46
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
68

@@ -209,7 +211,34 @@ public static <A extends Annotation> A getAnnotation(Class<?> cls, Class<A> anno
209211
}
210212
return annotation;
211213
}
214+
215+
public static Annotation[][] getParameterAnnotations(Method method) {
216+
Annotation[][] methodAnnotations = method.getParameterAnnotations();
217+
Method overriddenmethod = getOverriddenMethod(method);
218+
219+
if (overriddenmethod != null) {
220+
Annotation[][] overriddenAnnotations = overriddenmethod
221+
.getParameterAnnotations();
212222

223+
for (int i = 0; i < methodAnnotations.length; i++) {
224+
List<Type> types = new ArrayList<Type>();
225+
for (int j = 0; j < methodAnnotations[i].length; j++) {
226+
types.add(methodAnnotations[i][j].annotationType());
227+
}
228+
for (int j = 0; j < overriddenAnnotations[i].length; j++) {
229+
if (!types.contains(overriddenAnnotations[i][j]
230+
.annotationType())) {
231+
methodAnnotations[i] = ArrayUtils.add(
232+
methodAnnotations[i],
233+
overriddenAnnotations[i][j]);
234+
}
235+
}
236+
237+
}
238+
}
239+
return methodAnnotations;
240+
}
241+
213242
/**
214243
* Checks if the type is void.
215244
*

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

Lines changed: 3 additions & 3 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);
@@ -858,7 +858,7 @@ private Operation parseMethod(Class<?> cls, Method method, List<Parameter> globa
858858
}
859859

860860
Type[] genericParameterTypes = method.getGenericParameterTypes();
861-
Annotation[][] paramAnnotations = method.getParameterAnnotations();
861+
Annotation[][] paramAnnotations = ReflectionUtils.getParameterAnnotations(method);
862862
for (int i = 0; i < genericParameterTypes.length; i++) {
863863
final Type type = TypeFactory.defaultInstance().constructType(genericParameterTypes[i], cls);
864864
List<Parameter> parameters = getParameters(type, Arrays.asList(paramAnnotations[i]));

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)