Skip to content

Commit

Permalink
Make @ConditionalOn[Boolean]Property @Repeatable
Browse files Browse the repository at this point in the history
Update `ConditionalOnProperty`, `ConditionalOnBooleanProperty`
and `OnPropertyCondition` to support `@Repeatable`.

Closes gh-2541
  • Loading branch information
philwebb committed Jan 29, 2025
1 parent f32b29e commit a13fe0b
Show file tree
Hide file tree
Showing 7 changed files with 190 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.condition;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Conditional;

/**
* Container annotation that aggregates several
* {@link ConditionalOnProperty @ConditionalOnProperty} annotations.
*
* @author Phillip Webb
* @since 3.5.0
* @see ConditionalOnBooleanProperty
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnBooleanProperties {

/**
* Return the contained
* {@link ConditionalOnBooleanProperty @ConditionalOnBooleanProperty} annotations.
* @return the contained annotations
*/
ConditionalOnBooleanProperty[] value();

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand All @@ -43,6 +44,7 @@
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
@Repeatable(ConditionalOnBooleanProperties.class)
public @interface ConditionalOnBooleanProperty {

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.boot.autoconfigure.condition;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.springframework.context.annotation.Conditional;

/**
* Container annotation that aggregates several
* {@link ConditionalOnProperty @ConditionalOnProperty} annotations.
*
* @author Phillip Webb
* @since 3.5.0
* @see ConditionalOnProperty
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
public @interface ConditionalOnProperties {

/**
* Return the contained {@link ConditionalOnProperty @ConditionalOnProperty}
* annotations.
* @return the contained annotations
*/
ConditionalOnProperty[] value();

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
Expand Down Expand Up @@ -94,6 +95,7 @@
@Target({ ElementType.TYPE, ElementType.METHOD })
@Documented
@Conditional(OnPropertyCondition.class)
@Repeatable(ConditionalOnProperties.class)
public @interface ConditionalOnProperty {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

Expand All @@ -28,6 +29,7 @@
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotationPredicates;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.annotation.Order;
import org.springframework.core.env.PropertyResolver;
import org.springframework.core.type.AnnotatedTypeMetadata;
Expand All @@ -50,11 +52,8 @@ class OnPropertyCondition extends SpringBootCondition {

@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
List<MergedAnnotation<Annotation>> annotations = Stream
.concat(metadata.getAnnotations().stream(ConditionalOnProperty.class.getName()),
metadata.getAnnotations().stream(ConditionalOnBooleanProperty.class.getName()))
.filter(MergedAnnotationPredicates.unique(MergedAnnotation::getMetaTypes))
.toList();
MergedAnnotations mergedAnnotations = metadata.getAnnotations();
List<MergedAnnotation<Annotation>> annotations = stream(mergedAnnotations).toList();
List<ConditionMessage> noMatch = new ArrayList<>();
List<ConditionMessage> match = new ArrayList<>();
for (MergedAnnotation<Annotation> annotation : annotations) {
Expand All @@ -67,6 +66,34 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
return ConditionOutcome.match(ConditionMessage.of(match));
}

private Stream<MergedAnnotation<Annotation>> stream(MergedAnnotations mergedAnnotations) {
return Stream.concat(stream(mergedAnnotations, ConditionalOnProperty.class, ConditionalOnProperties.class),
stream(mergedAnnotations, ConditionalOnBooleanProperty.class, ConditionalOnBooleanProperties.class));
}

private Stream<MergedAnnotation<Annotation>> stream(MergedAnnotations mergedAnnotations,
Class<? extends Annotation> type, Class<? extends Annotation> containerType) {
return Stream.concat(stream(mergedAnnotations, type), streamRepeated(mergedAnnotations, type, containerType));
}

private Stream<MergedAnnotation<Annotation>> streamRepeated(MergedAnnotations mergedAnnotations,
Class<? extends Annotation> type, Class<? extends Annotation> containerType) {
return stream(mergedAnnotations, containerType).flatMap((container) -> streamRepeated(container, type));
}

@SuppressWarnings("unchecked")
private Stream<MergedAnnotation<Annotation>> streamRepeated(MergedAnnotation<Annotation> container,
Class<? extends Annotation> type) {
MergedAnnotation<? extends Annotation>[] repeated = container.getAnnotationArray(MergedAnnotation.VALUE, type);
return Arrays.stream((MergedAnnotation<Annotation>[]) repeated);
}

private Stream<MergedAnnotation<Annotation>> stream(MergedAnnotations annotations,
Class<? extends Annotation> containerType) {
return annotations.stream(containerType.getName())
.filter(MergedAnnotationPredicates.unique(MergedAnnotation::getMetaTypes));
}

private ConditionOutcome determineOutcome(MergedAnnotation<Annotation> annotation, PropertyResolver resolver) {
Class<Annotation> annotationType = annotation.getType();
Spec spec = new Spec(annotationType, annotation.asAnnotationAttributes());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,22 @@ void conditionReportWhenDoesNotMatch() {
.contains("@ConditionalOnBooleanProperty (test=true) found different value in property 'test'");
}

@Test
void repeatablePropertiesConditionReportWhenMatched() {
load(RepeatablePropertiesRequiredConfiguration.class, "property1=true", "property2=true");
assertThat(this.context.containsBean("foo")).isTrue();
String report = getConditionEvaluationReport();
assertThat(report).contains("@ConditionalOnBooleanProperty (property1=true) matched");
assertThat(report).contains("@ConditionalOnBooleanProperty (property2=true) matched");
}

@Test
void repeatablePropertiesConditionReportWhenDoesNotMatch() {
load(RepeatablePropertiesRequiredConfiguration.class, "property1=true");
assertThat(getConditionEvaluationReport())
.contains("@ConditionalOnBooleanProperty (property2=true) did not find property 'property2'");
}

private <T extends Exception> Consumer<T> causeMessageContaining(String message) {
return (ex) -> assertThat(ex.getCause()).hasMessageContaining(message);
}
Expand Down Expand Up @@ -266,4 +282,16 @@ String foo() {

}

@Configuration(proxyBeanMethods = false)
@ConditionalOnBooleanProperty("property1")
@ConditionalOnBooleanProperty("property2")
static class RepeatablePropertiesRequiredConfiguration {

@Bean
String foo() {
return "foo";
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -274,19 +274,35 @@ void metaAndDirectAnnotationWithAliasConditionMatchesWhenBothPropertiesAreSet()
}

@Test
void conditionReportWhenMatched() {
void multiplePropertiesConditionReportWhenMatched() {
load(MultiplePropertiesRequiredConfiguration.class, "property1=value1", "property2=value2");
assertThat(this.context.containsBean("foo")).isTrue();
assertThat(getConditionEvaluationReport()).contains("@ConditionalOnProperty ([property1,property2]) matched");
}

@Test
void conditionReportWhenDoesNotMatch() {
void multiplePropertiesConditionReportWhenDoesNotMatch() {
load(MultiplePropertiesRequiredConfiguration.class, "property1=value1");
assertThat(getConditionEvaluationReport())
.contains("@ConditionalOnProperty ([property1,property2]) did not find property 'property2'");
}

@Test
void repeatablePropertiesConditionReportWhenMatched() {
load(RepeatablePropertiesRequiredConfiguration.class, "property1=value1", "property2=value2");
assertThat(this.context.containsBean("foo")).isTrue();
String report = getConditionEvaluationReport();
assertThat(report).contains("@ConditionalOnProperty (property1) matched");
assertThat(report).contains("@ConditionalOnProperty (property2) matched");
}

@Test
void repeatablePropertiesConditionReportWhenDoesNotMatch() {
load(RepeatablePropertiesRequiredConfiguration.class, "property1=value1");
assertThat(getConditionEvaluationReport())
.contains("@ConditionalOnProperty (property2) did not find property 'property2'");
}

private void load(Class<?> config, String... environment) {
TestPropertyValues.of(environment).applyTo(this.environment);
this.context = new SpringApplicationBuilder(config).environment(this.environment)
Expand Down Expand Up @@ -315,6 +331,18 @@ String foo() {

}

@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty("property1")
@ConditionalOnProperty("property2")
static class RepeatablePropertiesRequiredConfiguration {

@Bean
String foo() {
return "foo";
}

}

@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = "spring.", name = "the-relaxed-property")
static class RelaxedPropertiesRequiredConfiguration {
Expand Down

0 comments on commit a13fe0b

Please sign in to comment.