From 6f15f32be3c5dcface3a9f4f5c58b0abe98e5071 Mon Sep 17 00:00:00 2001 From: wanxiangming1994 Date: Sun, 1 Dec 2019 20:22:41 +0800 Subject: [PATCH] Honor default values for implicit aliases in composed annotations Spring Framework 5.2 introduced a regression for implicit aliases declared via @AliasFor. Specifically, Spring's merged annotation algorithms stopped honoring default values for implicit alias pairs if the composed annotation was used without specifying the aliased attributes. This commit fixes this regression. Closes gh-24110 --- .../core/annotation/AnnotationTypeMapping.java | 3 +++ .../core/annotation/AnnotationTypeMappingsTests.java | 6 +++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java index c865b0a222b..bab16eaeda6 100644 --- a/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java +++ b/spring-core/src/main/java/org/springframework/core/annotation/AnnotationTypeMapping.java @@ -646,6 +646,9 @@ final class AnnotationTypeMapping { boolean isDefaultValue = (value == null || isEquivalentToDefaultValue(attribute, value, valueExtractor)); if (isDefaultValue || ObjectUtils.nullSafeEquals(lastValue, value)) { + if (result == -1) { + result = this.indexes[i]; + } continue; } if (lastValue != null && !ObjectUtils.nullSafeEquals(lastValue, value)) { diff --git a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationTypeMappingsTests.java b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationTypeMappingsTests.java index fd3f12b7cc1..c90fd3012bf 100644 --- a/spring-core/src/test/java/org/springframework/core/annotation/AnnotationTypeMappingsTests.java +++ b/spring-core/src/test/java/org/springframework/core/annotation/AnnotationTypeMappingsTests.java @@ -368,13 +368,13 @@ class AnnotationTypeMappingsTests { } @Test - void resolveMirrorsWhenOnlyHasDefaultValuesResolvesNone() { + void resolveMirrorsWhenOnlyHasDefaultValuesUsesFirst() { AnnotationTypeMapping mapping = AnnotationTypeMappings.forAnnotationType( AliasPair.class).get(0); Method[] resolved = resolveMirrorSets(mapping, WithDefaultValueAliasPair.class, AliasPair.class); - assertThat(resolved[0]).isNull(); - assertThat(resolved[1]).isNull(); + assertThat(resolved[0].getName()).isEqualTo("a"); + assertThat(resolved[1].getName()).isEqualTo("a"); } @Test