Skip to content

Commit 063a5df

Browse files
j-wrenschfrantuma
authored andcommitted
fix: check all parents for further parameter annotations
see the issue for details Refs #4002
1 parent 6ccd2f8 commit 063a5df

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ public static Annotation[][] getParameterAnnotations(Method method) {
385385
Annotation[][] methodAnnotations = method.getParameterAnnotations();
386386
Method overriddenmethod = getOverriddenMethod(method);
387387

388-
if (overriddenmethod != null) {
388+
while (overriddenmethod != null) {
389389
Annotation[][] overriddenAnnotations = overriddenmethod
390390
.getParameterAnnotations();
391391

@@ -404,6 +404,8 @@ public static Annotation[][] getParameterAnnotations(Method method) {
404404
}
405405

406406
}
407+
408+
overriddenmethod = getOverriddenMethod(overriddenmethod);
407409
}
408410
return methodAnnotations;
409411
}

modules/swagger-core/src/test/java/io/swagger/v3/core/util/reflection/ReflectionUtilsTest.java

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
import org.testng.annotations.Test;
1313

1414
import javax.ws.rs.Path;
15+
16+
import java.lang.annotation.Annotation;
17+
import java.lang.annotation.Retention;
18+
import java.lang.annotation.RetentionPolicy;
19+
import java.lang.annotation.Target;
1520
import java.lang.reflect.Field;
1621
import java.lang.reflect.Method;
1722
import java.lang.reflect.Type;
@@ -22,6 +27,8 @@
2227

2328
import static org.testng.Assert.assertNull;
2429

30+
import static java.lang.annotation.ElementType.PARAMETER;
31+
2532
public class ReflectionUtilsTest {
2633

2734
@Test
@@ -161,8 +168,54 @@ public void getRepeatableAnnotationsArrayTest() {
161168
Assert.assertEquals("inherited tag", annotations[0].name());
162169
}
163170

171+
@Test
172+
public void getParameterAnnotationsTest() throws NoSuchMethodException {
173+
Method method = SecondLevelSubClass.class.getMethod("method", String.class);
174+
Annotation[][] parameterAnnotations = ReflectionUtils.getParameterAnnotations(method);
175+
Assert.assertEquals(1, parameterAnnotations.length);
176+
Assert.assertEquals(1, parameterAnnotations[0].length);
177+
Assert.assertTrue(parameterAnnotations[0][0] instanceof AnnotationInterface);
178+
Assert.assertEquals("level1", ((AnnotationInterface)parameterAnnotations[0][0]).value());
179+
}
180+
181+
@Test
182+
public void getParameterAnnotationsForOverriddenAnnotationTest() throws NoSuchMethodException {
183+
Method method = ThirdLevelSubClass.class.getMethod("method", String.class);
184+
Annotation[][] parameterAnnotations = ReflectionUtils.getParameterAnnotations(method);
185+
Assert.assertEquals(1, parameterAnnotations.length);
186+
Assert.assertEquals(1, parameterAnnotations[0].length);
187+
Assert.assertTrue(parameterAnnotations[0][0] instanceof AnnotationInterface);
188+
Assert.assertEquals("level4", ((AnnotationInterface)parameterAnnotations[0][0]).value());
189+
}
190+
164191
@Tag(name = "inherited tag")
165192
private interface AnnotatedInterface {}
166193

167194
private class InheritingClass implements AnnotatedInterface {}
195+
196+
@Target({PARAMETER})
197+
@Retention(RetentionPolicy.RUNTIME)
198+
private @interface AnnotationInterface {
199+
String value();
200+
}
201+
202+
private static class BaseClass {
203+
public void method(@AnnotationInterface("level1") String example) {}
204+
}
205+
206+
private static class FirstLevelSubClass extends BaseClass {
207+
@Override
208+
public void method(String example){}
209+
}
210+
211+
private static class SecondLevelSubClass extends FirstLevelSubClass {
212+
@Override
213+
public void method(String example){}
214+
}
215+
216+
private static class ThirdLevelSubClass extends SecondLevelSubClass {
217+
@Override
218+
public void method(@AnnotationInterface("level4") String example){}
219+
}
220+
168221
}

0 commit comments

Comments
 (0)