New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve exception message for mixed explicit and implicit aliases with @AliasFor #24168
Comments
Thanks for raising the issue. We'll take a look. |
I've been able to reproduce this on class AnnotationAttributeOverrideTests {
@Test
void test() throws Exception {
Method method = getClass().getDeclaredMethod("handlerMethod");
assertThat(AnnotatedElementUtils.findMergedAnnotation(method, PostApi.class)).isNotNull();
}
@PostApi
void handlerMethod() {
}
@Retention(RetentionPolicy.RUNTIME)
@RequestMapping
@interface PostApi {
@AliasFor("value")
String[] path() default {};
@AliasFor(annotation = RequestMapping.class, attribute = "path")
String[] value() default {};
}
} |
In the interim, the following works. @Retention(RetentionPolicy.RUNTIME)
@RequestMapping
@interface PostApi {
@AliasFor(annotation = RequestMapping.class, attribute = "value")
String[] path() default {};
@AliasFor(annotation = RequestMapping.class, attribute = "path")
String[] value() default {};
} |
After further investigation, I have determined that the provided example was only permitted due to a bug that existed prior to Spring Framework 5.2. In other words, the example provided should never have worked since it combines "explicit" and "implicit" aliasing, which is not supported with From the Javadoc for Implicit aliases within an annotation:
Thus, the proper implementation of the @Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface PostApi {
@AliasFor(annotation = ResponseStatus.class, attribute = "code")
HttpStatus status() default HttpStatus.OK;
@AliasFor(annotation = RequestMapping.class)
String[] value() default {};
@AliasFor(annotation = RequestMapping.class)
String[] path() default {};
@AliasFor(annotation = RequestMapping.class)
String[] consumes() default MediaType.APPLICATION_JSON_VALUE;
} The above follows the same Please note as well that the following also results in an exception prior to Spring Framework 5.2. @Retention(RetentionPolicy.RUNTIME)
@RequestMapping
@interface PostApi {
@AliasFor("value")
String[] path() default {};
@AliasFor(annotation = RequestMapping.class, attribute = "value")
String[] value() default {};
} In other words, you simply got lucky that Spring did not throw an exception for your use case, since it was a configuration error. However, the exception message is indeed a bit confusing. In light of the above, I am changing the focus of this issue to improved wording of the exception message for 5.2.x; however, we will not make any changes in this regard for previous versions of the Spring Framework. |
I just upgraded Spring Boot version to 2.2.2 from 2.1.8 and noticed a weird exception:
Whole code for my custom annotation.
Any ideas what is going on?
The text was updated successfully, but these errors were encountered: