diff --git a/core/src/main/java/org/springframework/security/core/parameters/AnnotationParameterNameDiscoverer.java b/core/src/main/java/org/springframework/security/core/parameters/AnnotationParameterNameDiscoverer.java index ba081e6d7a..544ec99d1d 100644 --- a/core/src/main/java/org/springframework/security/core/parameters/AnnotationParameterNameDiscoverer.java +++ b/core/src/main/java/org/springframework/security/core/parameters/AnnotationParameterNameDiscoverer.java @@ -63,8 +63,9 @@ import org.springframework.util.ReflectionUtils; *

* *

- * It is important that all the parameter names have a supported annotation on - * them. Otherwise, the result will be null. For example, consider the + * It is important to note that if a single parameter name has a supported + * annotation on it then all methods are resolved using + * {@link AnnotationParameterNameDiscoverer}. For example, consider the * following: *

* @@ -76,9 +77,9 @@ import org.springframework.util.ReflectionUtils; * * *

- * The result of finding parameters on the previous sample will be a null - * String[] since only a single parameter contains an annotation. This is mostly - * due to the fact that the fallbacks for + * The result of finding parameters on the previous sample will be + * new String[] { "to", null} since only a single parameter + * contains an annotation. This is mostly due to the fact that the fallbacks for * {@link PrioritizedParameterNameDiscoverer} are an all or nothing operation. *

* @@ -149,16 +150,16 @@ public class AnnotationParameterNameDiscoverer implements ParameterNameFactory parameterNameFactory, T t) { int parameterCount = parameterNameFactory.getParamCount(t); String[] paramNames = new String[parameterCount]; + boolean found = false; for (int i = 0; i < parameterCount; i++) { Annotation[] annotations = parameterNameFactory.findAnnotationsAt(t, i); String parameterName = findParameterName(annotations); - if (parameterName == null) { - return null; - } else { + if (parameterName != null) { + found = true; paramNames[i] = parameterName; } } - return paramNames; + return found ? paramNames : null; } /** diff --git a/core/src/test/java/org/springframework/security/core/parameters/AnnotationParameterNameDiscovererTests.java b/core/src/test/java/org/springframework/security/core/parameters/AnnotationParameterNameDiscovererTests.java index f618ac2702..9660509646 100644 --- a/core/src/test/java/org/springframework/security/core/parameters/AnnotationParameterNameDiscovererTests.java +++ b/core/src/test/java/org/springframework/security/core/parameters/AnnotationParameterNameDiscovererTests.java @@ -22,7 +22,7 @@ public class AnnotationParameterNameDiscovererTests { @Test public void getParameterNamesInterfaceSingleParamAnnotatedWithMultiParams() { - assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(Dao.class, "findMessageByToAndFrom", String.class, String.class))).isNull(); + assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(Dao.class, "findMessageByToAndFrom", String.class, String.class))).isEqualTo(new String [] { "to", null}); } @Test @@ -37,7 +37,7 @@ public class AnnotationParameterNameDiscovererTests { @Test public void getParameterNamesClassSingleParamAnnotatedWithMultiParams() { - assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(Dao.class, "findMessageByToAndFrom", String.class, String.class))).isNull(); + assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(Dao.class, "findMessageByToAndFrom", String.class, String.class))).isEqualTo(new String [] { "to", null}); } @Test @@ -64,7 +64,7 @@ public class AnnotationParameterNameDiscovererTests { @Test public void getParameterNamesClassAnnotationOnImpl() throws Exception { - assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(Dao.class, "findMessageByToAndFrom", String.class, String.class))).isNull(); + assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(Dao.class, "findMessageByToAndFrom", String.class, String.class))).isEqualTo(new String [] { "to", null}); assertThat(discoverer.getParameterNames(ReflectionUtils.findMethod(DaoImpl.class, "findMessageByToAndFrom", String.class, String.class))).isEqualTo(new String[] {"to", "from"}); }