Skip to content

@AliasFor potentially overrides attribute in wrong meta-annotation [SPR-13325] #17910

@spring-projects-issues

Description

@spring-projects-issues

Sam Brannen opened SPR-13325 and commented

Status Quo

Spring Framework 4.2 introduced support for explicit attribute overrides in meta-annotations via the @AliasFor annotation.

For example, the following @MyTestConfig annotation uses @AliasFor to explicitly override the locations attribute in @ContextConfiguration, effectively renaming it.

@ContextConfiguration
public @interface MyTestConfig {

    @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
    String[] xmlFiles();
 }

@MyTestConfig works fine as long as there is not more than one meta-annotation with an attribute named locations.

In contrast, the following currently does not work.

@ContextConfiguration
@TestPropertySource(locations = "test.properties")
public @interface MyCombinedTestConfig {

    @AliasFor(annotation = ContextConfiguration.class, attribute = "locations")
    String[] xmlFiles();
}
@MyCombinedTestConfig(xmlFiles = "test.xml")
public class MyTests {
    // ...
}

The reason is that the getAliasedAttributeName(Method attribute, Class<? extends Annotation> targetAnnotationType) method in AnnotationUtils does not verify that the supplied attribute actually declares an alias for the targetAnnotationType. Consequently, a lookup for the locations attribute in @TestPropertySource on MyTests will incorrectly return "test.xml" instead of "test.properties".

Deliverables

  1. Ensure that an explicit annotation attribute override configured via @AliasFor is only applied to the configured target meta-annotation.

Affects: 4.2 GA

Issue Links:

Referenced from: commits e20b47c, c8d604b

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions