From 063a5dfdbd85c3787a49dfeb8cda1d05fb6dd18e Mon Sep 17 00:00:00 2001 From: Jeremias Wrensch Date: Thu, 19 Aug 2021 13:27:29 +0200 Subject: [PATCH] fix: check all parents for further parameter annotations see the issue for details Refs #4002 --- .../swagger/v3/core/util/ReflectionUtils.java | 4 +- .../util/reflection/ReflectionUtilsTest.java | 53 +++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ReflectionUtils.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ReflectionUtils.java index ba1f73dae1..de7b73ff5d 100644 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ReflectionUtils.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/util/ReflectionUtils.java @@ -385,7 +385,7 @@ public static Annotation[][] getParameterAnnotations(Method method) { Annotation[][] methodAnnotations = method.getParameterAnnotations(); Method overriddenmethod = getOverriddenMethod(method); - if (overriddenmethod != null) { + while (overriddenmethod != null) { Annotation[][] overriddenAnnotations = overriddenmethod .getParameterAnnotations(); @@ -404,6 +404,8 @@ public static Annotation[][] getParameterAnnotations(Method method) { } } + + overriddenmethod = getOverriddenMethod(overriddenmethod); } return methodAnnotations; } diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/util/reflection/ReflectionUtilsTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/util/reflection/ReflectionUtilsTest.java index a583765a13..b45f58a99f 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/util/reflection/ReflectionUtilsTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/util/reflection/ReflectionUtilsTest.java @@ -12,6 +12,11 @@ import org.testng.annotations.Test; import javax.ws.rs.Path; + +import java.lang.annotation.Annotation; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Type; @@ -22,6 +27,8 @@ import static org.testng.Assert.assertNull; +import static java.lang.annotation.ElementType.PARAMETER; + public class ReflectionUtilsTest { @Test @@ -161,8 +168,54 @@ public void getRepeatableAnnotationsArrayTest() { Assert.assertEquals("inherited tag", annotations[0].name()); } + @Test + public void getParameterAnnotationsTest() throws NoSuchMethodException { + Method method = SecondLevelSubClass.class.getMethod("method", String.class); + Annotation[][] parameterAnnotations = ReflectionUtils.getParameterAnnotations(method); + Assert.assertEquals(1, parameterAnnotations.length); + Assert.assertEquals(1, parameterAnnotations[0].length); + Assert.assertTrue(parameterAnnotations[0][0] instanceof AnnotationInterface); + Assert.assertEquals("level1", ((AnnotationInterface)parameterAnnotations[0][0]).value()); + } + + @Test + public void getParameterAnnotationsForOverriddenAnnotationTest() throws NoSuchMethodException { + Method method = ThirdLevelSubClass.class.getMethod("method", String.class); + Annotation[][] parameterAnnotations = ReflectionUtils.getParameterAnnotations(method); + Assert.assertEquals(1, parameterAnnotations.length); + Assert.assertEquals(1, parameterAnnotations[0].length); + Assert.assertTrue(parameterAnnotations[0][0] instanceof AnnotationInterface); + Assert.assertEquals("level4", ((AnnotationInterface)parameterAnnotations[0][0]).value()); + } + @Tag(name = "inherited tag") private interface AnnotatedInterface {} private class InheritingClass implements AnnotatedInterface {} + + @Target({PARAMETER}) + @Retention(RetentionPolicy.RUNTIME) + private @interface AnnotationInterface { + String value(); + } + + private static class BaseClass { + public void method(@AnnotationInterface("level1") String example) {} + } + + private static class FirstLevelSubClass extends BaseClass { + @Override + public void method(String example){} + } + + private static class SecondLevelSubClass extends FirstLevelSubClass { + @Override + public void method(String example){} + } + + private static class ThirdLevelSubClass extends SecondLevelSubClass { + @Override + public void method(@AnnotationInterface("level4") String example){} + } + }