Skip to content

Fix single-value adaptation for primitive array types in TypeMappedAnnotation - #37349

Merged
sbrannen merged 1 commit into
spring-projects:7.0.xfrom
guanchengang:fix/single-value-primitive-array-adaptation
Sep 30, 2026
Merged

sbrannen merged 1 commit into
spring-projects:7.0.xfrom
guanchengang:fix/single-value-primitive-array-adaptation

Conversation

@guanchengang

Copy link
Copy Markdown
Contributor

This branch appears to handle the single-value to array case when using TypeMappedAnnotation.of(classLoader, source, annotationType,attributes) with a Map. However, due to primitive boxing/unboxing, it only works for object array types such as String, Class, enum, and annotation arrays.

For primitive array types, value.getClass() returns the boxed type, so Array.newInstance(value.getClass(), 1) produces a boxed array rather than a primitive array. The subsequent compatibility check then fails and throws an IllegalStateException.

Checking whether the value can be unboxed to the component type of the primitive array would let this branch handle all primitive array types on equal footing.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 30, 2026
@sbrannen sbrannen added in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Sep 30, 2026
@sbrannen sbrannen added this to the 7.0.10 milestone Sep 30, 2026
…notation

Prior to this commit, `adaptForAttribute(Method, Object)` created
the wrapping array from `value.getClass()` when a single non-array
value was provided for an array attribute. This worked for object
array types but failed for primitive array types: wrapping a boxed
value produced a boxed array, which then failed the compatibility
check and threw an `IllegalStateException`.

This commit derives the component type from the declared attribute
type when it is assignable from the value type, falling back to
`value.getClass()` otherwise. The existing adaptation path for
object array types is therefore preserved, and all primitive array
types now accept a single value.

The accompanying test covers single-value wrapping for every array
type declared by ArrayTypes.

Closes spring-projectsgh-37349

Signed-off-by: Chengang Guan <guanchengang@qq.com>
@sbrannen
sbrannen force-pushed the fix/single-value-primitive-array-adaptation branch from b8e2b64 to 6774270 Compare September 30, 2026 11:35
@sbrannen
sbrannen changed the base branch from main to 7.0.x September 30, 2026 11:35
@sbrannen
sbrannen merged commit 6afb65b into spring-projects:7.0.x Sep 30, 2026
2 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

Fixed via 6afb65b

sbrannen added a commit to sbrannen/spring-framework that referenced this pull request Sep 30, 2026
This commit adds a test to MergedAnnotationsTests that verifies a
single primitive attribute in a composed annotation can be aliased via
@⁠AliasFor to a primitive array attribute in a meta-annotation.

See spring-projectsgh-37349
@sbrannen

Copy link
Copy Markdown
Member

This has been merged into 7.0.x and main in 6afb65b, and I added a concrete test for @AliasFor support in 043442a.

Thanks

@guanchengang
guanchengang deleted the fix/single-value-primitive-array-adaptation branch October 4, 2026 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in: core Issues in core modules (aop, beans, core, context, expression) type: bug A general bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants