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
Expand Up @@ -64,20 +64,19 @@ public static boolean isOverriddenMethod(Method methodToFind, Class<?> cls) {
public static Method getOverriddenMethod(Method method) {
Class<?> declaringClass = method.getDeclaringClass();
Class<?> superClass = declaringClass.getSuperclass();
Method result = null;
if (superClass != null && !(superClass.equals(Object.class))) {
Method result = findMethod(method, superClass);
if (result == null) {
for (Class<?> anInterface : declaringClass.getInterfaces()) {
result = findMethod(method, anInterface);
if (result != null) {
return result;
}
result = findMethod(method, superClass);
}
if (result == null) {
for (Class<?> anInterface : declaringClass.getInterfaces()) {
result = findMethod(method, anInterface);
if (result != null) {
return result;
}
} else {
return result;
}
}
return null;
return result;
}

/**
Expand Down Expand Up @@ -182,6 +181,25 @@ public static <A extends Annotation> A getAnnotation(Method method, Class<A> ann
return annotation;
}

public static <A extends Annotation> A getAnnotation(Class<?> cls, Class<A> annotationClass) {
A annotation = cls.getAnnotation(annotationClass);
if (annotation == null) {
Class<?> superClass = cls.getSuperclass();
if (superClass != null && !(superClass.equals(Object.class))) {
annotation = getAnnotation(superClass, annotationClass);
}
}
if (annotation == null) {
for (Class<?> anInterface : cls.getInterfaces()) {
annotation = getAnnotation(anInterface, annotationClass);
if (annotation != null) {
return annotation;
}
}
}
return annotation;
}

/**
* Checks if the type is void.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.lang.reflect.Type;
import java.util.Arrays;

import javax.ws.rs.Path;

public class ReflectionUtilsTest {

@Test
Expand Down Expand Up @@ -105,4 +107,11 @@ public void isVoidTest() {
Assert.assertTrue(ReflectionUtils.isVoid(Void.TYPE));
Assert.assertFalse(ReflectionUtils.isVoid(String.class));
}

@Test
public void testDerivedAnnotation() {
final Path annotation = ReflectionUtils.getAnnotation(Child.class, javax.ws.rs.Path.class);
Assert.assertNotNull(annotation);
Assert.assertEquals(annotation.value(), "parentInterfacePath");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package io.swagger.reflection;

import javax.ws.rs.Path;

@Path("parentInterfacePath")
public interface IParent<T extends Number> {

public String parametrizedMethod2(T arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ private Swagger read(Class<?> cls, String parentPath, String parentMethod, boole
globalParameters.addAll(ReaderUtils.collectFieldParameters(cls, swagger));

// parse the method
final javax.ws.rs.Path apiPath = cls.getAnnotation(javax.ws.rs.Path.class);
final javax.ws.rs.Path apiPath = ReflectionUtils.getAnnotation(cls, javax.ws.rs.Path.class);
Method methods[] = cls.getMethods();
for (Method method : methods) {
if (ReflectionUtils.isOverriddenMethod(method, cls)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.swagger.models.parameters.Parameter;
import io.swagger.models.parameters.PathParameter;
import io.swagger.models.parameters.QueryParameter;
import io.swagger.resources.AnnotatedInterfaceImpl;
import io.swagger.resources.ApiConsumesProducesResource;
import io.swagger.resources.BookResource;
import io.swagger.resources.BothConsumesProducesResource;
Expand All @@ -20,6 +21,7 @@
import io.swagger.resources.ResourceWithKnownInjections;
import io.swagger.resources.RsConsumesProducesResource;
import io.swagger.resources.SimpleMethods;

import org.testng.annotations.Test;

import javax.ws.rs.DELETE;
Expand Down Expand Up @@ -161,6 +163,13 @@ public void scanOverriddenMethod() {
assertNotNull(methodFromInterface);
}

@Test(description = "scan annotation from interface, issue#1427")
public void scanInterfaceTest() {
final Swagger swagger = new Reader(new Swagger()).read(AnnotatedInterfaceImpl.class);
assertNotNull(swagger);
assertNotNull(swagger.getPath("/v1/users/{id}").getGet());
}

@Test(description = "scan implicit params")
public void scanImplicitParam() {
Swagger swagger = getSwagger(ResourceWithImplicitParams.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.swagger.resources;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/v1/users")
public interface AnnotatedInterface {

@GET
@Path("/{id}")
String findById(@PathParam("id") String id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.swagger.resources;

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

@Api(value = "/v1/users", tags = "annotatedInterface")
public class AnnotatedInterfaceImpl implements AnnotatedInterface {

@Override
@ApiOperation(value = "Load by userId")
public String findById(String id) {
return "";
}
}