Skip to content

AnnotationReadingVisitorUtils.getMergedAnnotationAttributes() overrides values it should not [SPR-11649] #16272

Closed

Description

@spring-projects-issues

Ilian Iliev opened SPR-11649 and commented

Status Quo

I have the following class with 2 annotations:

@VsoFinder(name="some_name", ...)
@VsoObject(name="another_name", ...)
public class SomeClass { /* ... */ }

In my app, there is the following call:

Map<String, Object> attributes = annotMetadata.getAnnotationAttributes(VsoObject.class.getName());

I'd expect this call to return the attributes/values defined in the @VsoObject annotation, and it worked fine in Spring versions up to 4.0.2. However, due to the changes introduced in conjunction with #16198 in Spring 4.0.3 the returned values are those coming from the @VsoFinder annotation.

A quick look at AnnotationReadingVisitorUtils.getMergedAnnotationAttributes() confirms that indeed the values of attributes with the same name are overridden with the values from annotations of any type, not only from the requested annotation type.

Deliverables

  1. Ensure that AnnotationReadingVisitorUtils.getMergedAnnotationAttributes() only overrides attributes in the target annotation with attributes from annotations that are annotated with the target annotation.

Affects: 4.0.3

Issue Links:

Activity

  1. spring-projects-issues commented on Apr 2, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Is this by design, or a bug in getMergedAnnotationAttributes() code?

    This sounds like it might in fact be a bug.

    We will look into it!

    Thanks,

    Sam

  2. spring-projects-issues commented on Apr 2, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    I've just pushed a fix to a #16272 branch in my personal GitHub repository (direct link to commit).

    A few notes on the status quo as well as my changes:

    • AnnotationMetadataReadingVisitor currently tracks the meta-annotations present on the underlying class in a LinkedMultiValueMap.
    • This data structure unfortunately does not accurately represent the tree nature of the annotation hierarchy, but we need this tree-like structural information to determine which annotations are allowed to override the target annotation.
    • To overcome this, we have two options:
      1. Overhaul AnnotationMetadataReadingVisitor and associated classes so that the actual tree nature of the metadata is preserved. This would allow us to query the tree structure to infer which annotations are meta-annotated with other annotations.
      2. Look up the meta-annotation metadata for each annotation as we encounter it in the AnnotationReadingVisitorUtils.getMergedAnnotationAttributes() algorithm.

    In my branch I implemented the latter option since it required fewer changes to the code base; however, I am not totally happy with it for the following reasons:

    1. It uses ASM to revisit meta-annotations that have already been visited in the original creation of the metadata for the underlying class, but this revisiting is necessary since the tree structure is not preserved. See AnnotationReadingVisitorUtils.isMetaAnnotated().
    2. isMetaAnnotated() swallows IOExceptions silently.
    3. getMergedAnnotationAttributes always instantiates a new SimpleMetadataReaderFactory with zero caching.

    Juergen Hoeller, could you please review this change before I push it to the official repository?

    Thanks!

    Sam

  3. spring-projects-issues commented on Apr 30, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    I've addressed the immediate problem here through passing the AnnotationMetadataReadingVisitor's metaAnnotationMap into the getMergedAnnotationAttributes algorithm, checking for a corresponding entry there before actually letting another annotation's attributes override. This makes all test cases from Sam's branch pass.

    If any corner problems remain, let's deal with them for 4.0.5.

    Juergen

  4. spring-projects-issues commented on May 9, 2014

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Juergen Hoeller,

    That's definitely a more elegant solution. ;)

    Thanks!

  5. added
    type: bug
    in: core
    on Jan 11, 2019
  6. added this to the 4.0.4 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions