Skip to content

Allow a single element to override an array attribute in a meta-annotation [SPR-13972] #18544

Closed

Description

@spring-projects-issues

Juergen Hoeller opened SPR-13972 and commented

Overview

In 4.2, aliased annotation attribute types must match exactly. However, it is rather common in Java for single element values to be specified for an array attribute: Reflecting this in custom annotation design, it would be nice for a custom annotation to be able to declare a single element only, aliasing a meta-annotation attribute which was originally declared as an array, e.g. in a custom @PropertySource variant with a single String value.

Deliverables

  1. Allow for a single element overriding an array attribute in a meta-annotation by convention.
    • See findMergedAnnotationAttributesWithSingleElementOverridingAnArrayViaConvention() in AnnotatedElementUtilsTests.
  2. Allow for a single element overriding an array attribute in a meta-annotation using @AliasFor.
    • See findMergedAnnotationAttributesWithSingleElementOverridingAnArrayViaAliasFor() in AnnotatedElementUtilsTests.
  3. Ensure that the aforementioned overrides are supported in AnnotationAttributes (which is assumed to already be the case but should be verified).
    • See singleElementToSingleElementArrayConversionSupport() in AnnotationAttributesTests.
  4. Ensure that the aforementioned overrides are supported in synthesized annotations.
    • See various getMergedAnnotation*WithSingleElementOverridingAnArrayVia*() and findMergedAnnotationWithSingleElementOverridingAnArrayVia*() methods in AnnotatedElementUtilsTests.

Affects: 4.2.4

Issue Links:

Activity

  1. spring-projects-issues commented on Feb 26, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Juergen Hoeller, the committed change only works for reading such attributes via the AnnotationAttributes API -- actually, that (little known) feature has been in place for several years in AnnotationAttributes - however, this does not work for synthesized annotations.

    When attempting to use this feature with synthesized annotations (e.g., a merged @RequestMapping), we see errors similar to the following (thrown from MapAnnotationAttributeExtractor.enrichAndValidateAttributes()):

    java.lang.IllegalArgumentException: Attributes map {name=, value=/get, path=/get, params=[Ljava.lang.String;@6325a3ee, headers=[Ljava.lang.String;@1d16f93d, consumes=[Ljava.lang.String;@67b92f0a, produces=[Ljava.lang.String;@2b9627bc, method=[Lorg.springframework.web.bind.annotation.RequestMethod;@65e2dbf3} returned a value of type [java.lang.String] for attribute 'value', but a value of type [[Ljava.lang.String;] is required as defined by annotation type [org.springframework.web.bind.annotation.RequestMapping].
    

    I have been planning on adding such support for synthesized annotations. So, do you want me to reopen/repurpose this issue or create a separate one to address this?

    Cheers,

    Sam

  2. spring-projects-issues commented on Feb 27, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Juergen Hoeller commented

    Let's reopen this one for the wider purpose in the 4.3 timeframe. I only really sneaked it in since I ran into it when coding a recent unit test :-)

    Juergen

  3. spring-projects-issues commented on Feb 27, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    OK. I'm reopening this issue and assigning it to me in order to address annotations synthesized from maps.

  4. spring-projects-issues commented on Mar 6, 2016

    @spring-projects-issues
    CollaboratorAuthor
  5. spring-projects-issues commented on Mar 7, 2016

    @spring-projects-issues
    CollaboratorAuthor

    Sam Brannen commented

    Pushed to master in the following commit:

    eb654dc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions