Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.swagger.util;

import com.fasterxml.jackson.databind.type.TypeFactory;

import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -209,7 +211,34 @@ public static <A extends Annotation> A getAnnotation(Class<?> cls, Class<A> anno
}
return annotation;
}

public static Annotation[][] getParameterAnnotations(Method method) {
Annotation[][] methodAnnotations = method.getParameterAnnotations();
Method overriddenmethod = getOverriddenMethod(method);

if (overriddenmethod != null) {
Annotation[][] overriddenAnnotations = overriddenmethod
.getParameterAnnotations();

for (int i = 0; i < methodAnnotations.length; i++) {
List<Type> types = new ArrayList<Type>();
for (int j = 0; j < methodAnnotations[i].length; j++) {
types.add(methodAnnotations[i][j].annotationType());
}
for (int j = 0; j < overriddenAnnotations[i].length; j++) {
if (!types.contains(overriddenAnnotations[i][j]
.annotationType())) {
methodAnnotations[i] = ArrayUtils.add(
methodAnnotations[i],
overriddenAnnotations[i][j]);
}
}

}
}
return methodAnnotations;
}

/**
* Checks if the type is void.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private Swagger read(Class<?> cls, String parentPath, String parentMethod, boole
String[] produces = new String[0];
final Set<Scheme> globalSchemes = EnumSet.noneOf(Scheme.class);

Api api = (Api) cls.getAnnotation(Api.class);
Api api = ReflectionUtils.getAnnotation(cls, Api.class);

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

private void readImplicitParameters(Method method, Operation operation) {
ApiImplicitParams implicitParams = method.getAnnotation(ApiImplicitParams.class);
ApiImplicitParams implicitParams = ReflectionUtils.getAnnotation(method, ApiImplicitParams.class);
if (implicitParams != null && implicitParams.value().length > 0) {
for (ApiImplicitParam param : implicitParams.value()) {
Parameter p = readImplicitParam(param);
Expand Down Expand Up @@ -858,7 +858,7 @@ private Operation parseMethod(Class<?> cls, Method method, List<Parameter> globa
}

Type[] genericParameterTypes = method.getGenericParameterTypes();
Annotation[][] paramAnnotations = method.getParameterAnnotations();
Annotation[][] paramAnnotations = ReflectionUtils.getParameterAnnotations(method);
for (int i = 0; i < genericParameterTypes.length; i++) {
final Type type = TypeFactory.defaultInstance().constructType(genericParameterTypes[i], cls);
List<Parameter> parameters = getParameters(type, Arrays.asList(paramAnnotations[i]));
Expand Down
22 changes: 22 additions & 0 deletions modules/swagger-jaxrs/src/test/java/io/swagger/ReaderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import io.swagger.jaxrs.Reader;
import io.swagger.models.Operation;
import io.swagger.models.Swagger;
import io.swagger.models.Tag;
import io.swagger.models.parameters.*;
import io.swagger.resources.*;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -316,6 +317,27 @@ public void scanDeclaredExceptionsAndCombineWithMethodResponsesClassLevel() {

}

@Test(description = "scan resource (impl) which has the Api annotations only declared in its interface")
public void scanApiAnnotationWhichAreOnlyPresentInInterfaceAndNotInImplementation() {
Swagger swagger = getSwagger(ResourceWithAnnotationsOnlyInInterfaceImpl.class);
assertNotNull(swagger);

final List<Tag> tags = swagger.getTags();
assertEquals(tags.size(), 1);
assertEquals(tags.get(0).getName(), "someTag");
}

@Test(description = "scan resource (impl) which has the ApiParam annotations only declared in its interface")
public void scanApiImplicitParamAnnotationWhichAreOnlyPresentInInterfaceAndNotInImplementation() {
Swagger swagger = getSwagger(ResourceWithAnnotationsOnlyInInterfaceImpl.class);
assertNotNull(swagger);

List<Parameter> parameters = getGet(swagger, "/pet/randomPet").getParameters();
assertNotNull(parameters);
assertEquals(parameters.size(), 1);
assertEquals(parameters.get(0).getName(), "petImplicitIdParam");
}

private Swagger getSwagger(Class<?> cls) {
return new Reader(new Swagger()).read(cls);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.swagger.resources;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

@Path("/pet")
@Api(tags = "someTag")
public interface ResourceWithAnnotationsOnlyInInterface {

@GET
@Path("/randomPet")
@ApiOperation(value = "getRandomPet")
@ApiImplicitParams({ @ApiImplicitParam(name = "petImplicitIdParam", paramType = "query", dataType = "string") })
String getRandomPet();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.swagger.resources;

public class ResourceWithAnnotationsOnlyInInterfaceImpl implements ResourceWithAnnotationsOnlyInInterface {

@Override
public String getRandomPet() {
return "No pet today..";
}
}