Skip to content

Commit dc57ad5

Browse files
terminuxwilkinsona
authored andcommitted
Fix ConditionalOnProperty when used in an aliased composed annotation
See gh-30505
1 parent 09ec409 commit dc57ad5

File tree

2 files changed

+65
-29
lines changed

2 files changed

+65
-29
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/OnPropertyCondition.java

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,20 +17,22 @@
1717
package org.springframework.boot.autoconfigure.condition;
1818

1919
import java.util.ArrayList;
20-
import java.util.HashMap;
2120
import java.util.List;
2221
import java.util.Map;
22+
import java.util.stream.Collectors;
2323

2424
import org.springframework.boot.autoconfigure.condition.ConditionMessage.Style;
2525
import org.springframework.context.annotation.Condition;
2626
import org.springframework.context.annotation.ConditionContext;
2727
import org.springframework.core.Ordered;
2828
import org.springframework.core.annotation.AnnotationAttributes;
29+
import org.springframework.core.annotation.MergedAnnotation;
30+
import org.springframework.core.annotation.MergedAnnotation.Adapt;
31+
import org.springframework.core.annotation.MergedAnnotationPredicates;
2932
import org.springframework.core.annotation.Order;
3033
import org.springframework.core.env.PropertyResolver;
3134
import org.springframework.core.type.AnnotatedTypeMetadata;
3235
import org.springframework.util.Assert;
33-
import org.springframework.util.MultiValueMap;
3436
import org.springframework.util.StringUtils;
3537

3638
/**
@@ -47,8 +49,12 @@ class OnPropertyCondition extends SpringBootCondition {
4749

4850
@Override
4951
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
50-
List<AnnotationAttributes> allAnnotationAttributes = annotationAttributesFromMultiValueMap(
51-
metadata.getAllAnnotationAttributes(ConditionalOnProperty.class.getName()));
52+
Adapt[] adaptations = Adapt.values(false, true);
53+
List<AnnotationAttributes> allAnnotationAttributes = metadata.getAnnotations()
54+
.stream(ConditionalOnProperty.class.getName())
55+
.filter(MergedAnnotationPredicates.unique(MergedAnnotation::getMetaTypes))
56+
.map((annotation) -> annotation.asAnnotationAttributes(adaptations)).collect(Collectors.toList());
57+
5258
List<ConditionMessage> noMatch = new ArrayList<>();
5359
List<ConditionMessage> match = new ArrayList<>();
5460
for (AnnotationAttributes annotationAttributes : allAnnotationAttributes) {
@@ -61,29 +67,6 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
6167
return ConditionOutcome.match(ConditionMessage.of(match));
6268
}
6369

64-
private List<AnnotationAttributes> annotationAttributesFromMultiValueMap(
65-
MultiValueMap<String, Object> multiValueMap) {
66-
List<Map<String, Object>> maps = new ArrayList<>();
67-
multiValueMap.forEach((key, value) -> {
68-
for (int i = 0; i < value.size(); i++) {
69-
Map<String, Object> map;
70-
if (i < maps.size()) {
71-
map = maps.get(i);
72-
}
73-
else {
74-
map = new HashMap<>();
75-
maps.add(map);
76-
}
77-
map.put(key, value.get(i));
78-
}
79-
});
80-
List<AnnotationAttributes> annotationAttributes = new ArrayList<>(maps.size());
81-
for (Map<String, Object> map : maps) {
82-
annotationAttributes.add(AnnotationAttributes.fromMap(map));
83-
}
84-
return annotationAttributes;
85-
}
86-
8770
private ConditionOutcome determineOutcome(AnnotationAttributes annotationAttributes, PropertyResolver resolver) {
8871
Spec spec = new Spec(annotationAttributes);
8972
List<String> missingProperties = new ArrayList<>();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/condition/ConditionalOnPropertyTests.java

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -31,6 +31,7 @@
3131
import org.springframework.context.ConfigurableApplicationContext;
3232
import org.springframework.context.annotation.Bean;
3333
import org.springframework.context.annotation.Configuration;
34+
import org.springframework.core.annotation.AliasFor;
3435
import org.springframework.core.env.ConfigurableEnvironment;
3536
import org.springframework.core.env.StandardEnvironment;
3637

@@ -246,6 +247,25 @@ void metaAndDirectAnnotationConditionMatchesWhenBothPropertiesAreSet() {
246247
assertThat(this.context.containsBean("foo")).isTrue();
247248
}
248249

250+
@Test
251+
void metaAnnotationWithAliasConditionMatchesWhenPropertyIsSet() {
252+
load(MetaAnnotationWithAlias.class, "my.feature.enabled=true");
253+
assertThat(this.context.containsBean("foo")).isTrue();
254+
}
255+
256+
@Test
257+
void metaAndDirectAnnotationWithAliasConditionDoesNotMatchWhenOnlyMetaPropertyIsSet() {
258+
load(MetaAnnotationAndDirectAnnotationWithAlias.class, "my.feature.enabled=true");
259+
assertThat(this.context.containsBean("foo")).isFalse();
260+
}
261+
262+
@Test
263+
void metaAndDirectAnnotationWithAliasConditionMatchesWhenBothPropertiesAreSet() {
264+
load(MetaAnnotationAndDirectAnnotationWithAlias.class, "my.feature.enabled=true",
265+
"my.other.feature.enabled=true");
266+
assertThat(this.context.containsBean("foo")).isTrue();
267+
}
268+
249269
private void load(Class<?> config, String... environment) {
250270
TestPropertyValues.of(environment).applyTo(this.environment);
251271
this.context = new SpringApplicationBuilder(config).environment(this.environment).web(WebApplicationType.NONE)
@@ -416,4 +436,37 @@ String foo() {
416436

417437
}
418438

439+
@Configuration(proxyBeanMethods = false)
440+
@ConditionalOnMyFeatureWithAlias("my.feature")
441+
static class MetaAnnotationWithAlias {
442+
443+
@Bean
444+
String foo() {
445+
return "foo";
446+
}
447+
448+
}
449+
450+
@Configuration(proxyBeanMethods = false)
451+
@ConditionalOnMyFeatureWithAlias("my.feature")
452+
@ConditionalOnProperty(prefix = "my.other.feature", name = "enabled", havingValue = "true")
453+
static class MetaAnnotationAndDirectAnnotationWithAlias {
454+
455+
@Bean
456+
String foo() {
457+
return "foo";
458+
}
459+
460+
}
461+
462+
@Retention(RetentionPolicy.RUNTIME)
463+
@Target({ ElementType.TYPE, ElementType.METHOD })
464+
@ConditionalOnProperty(name = "enabled", havingValue = "true")
465+
@interface ConditionalOnMyFeatureWithAlias {
466+
467+
@AliasFor(annotation = ConditionalOnProperty.class, attribute = "prefix")
468+
String value();
469+
470+
}
471+
419472
}

0 commit comments

Comments
 (0)