Skip to content

Commit 7bcdb7d

Browse files
quaffphilwebb
authored andcommitted
Enforce @ConfigurationProperties don't use only prefix
See gh-43917 Signed-off-by: Yanming Zhou <[email protected]>
1 parent f8ae1ff commit 7bcdb7d

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureCheck.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import com.tngtech.archunit.core.domain.JavaParameter;
4040
import com.tngtech.archunit.core.domain.JavaType;
4141
import com.tngtech.archunit.core.domain.properties.CanBeAnnotated;
42+
import com.tngtech.archunit.core.domain.properties.HasAnnotations;
4243
import com.tngtech.archunit.core.domain.properties.HasName;
4344
import com.tngtech.archunit.core.domain.properties.HasOwner.Predicates.With;
4445
import com.tngtech.archunit.core.domain.properties.HasParameterTypes;
@@ -97,7 +98,9 @@ public ArchitectureCheck() {
9798
noClassesShouldLoadResourcesUsingResourceUtils(), noClassesShouldCallStringToUpperCaseWithoutLocale(),
9899
noClassesShouldCallStringToLowerCaseWithoutLocale(),
99100
conditionalOnMissingBeanShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodReturnType(),
100-
enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType());
101+
enumSourceShouldNotSpecifyOnlyATypeThatIsTheSameAsMethodParameterType(),
102+
classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute(),
103+
methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute());
101104
getRules().addAll(getProhibitObjectsRequireNonNull()
102105
.map((prohibit) -> prohibit ? noClassesShouldCallObjectsRequireNonNull() : Collections.emptyList()));
103106
getRuleDescriptions().set(getRules().map((rules) -> rules.stream().map(ArchRule::getDescription).toList()));
@@ -344,6 +347,39 @@ public void check(JavaMethod item, ConditionEvents events) {
344347
};
345348
}
346349

350+
private ArchRule classLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute() {
351+
return ArchRuleDefinition.classes()
352+
.that()
353+
.areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationProperties")
354+
.should(notSpecifyOnlyPrefixAttributeOfConfigurationProperties())
355+
.allowEmptyShould(true);
356+
}
357+
358+
private ArchRule methodLevelConfigurationPropertiesShouldNotSpecifyOnlyPrefixAttribute() {
359+
return ArchRuleDefinition.methods()
360+
.that()
361+
.areAnnotatedWith("org.springframework.boot.context.properties.ConfigurationProperties")
362+
.should(notSpecifyOnlyPrefixAttributeOfConfigurationProperties())
363+
.allowEmptyShould(true);
364+
}
365+
366+
private ArchCondition<? super HasAnnotations<?>> notSpecifyOnlyPrefixAttributeOfConfigurationProperties() {
367+
return new ArchCondition<>("not specify only prefix attribute of @ConfigurationProperties") {
368+
369+
@Override
370+
public void check(HasAnnotations<?> item, ConditionEvents events) {
371+
JavaAnnotation<?> configurationProperties = item
372+
.getAnnotationOfType("org.springframework.boot.context.properties.ConfigurationProperties");
373+
Map<String, Object> properties = configurationProperties.getProperties();
374+
if (properties.size() == 1 && properties.containsKey("prefix")) {
375+
events.add(SimpleConditionEvent.violated(item, configurationProperties.getDescription()
376+
+ " should specify implicit 'value' attribute other than explicit 'prefix' attribute"));
377+
}
378+
}
379+
380+
};
381+
}
382+
347383
public void setClasses(FileCollection classes) {
348384
this.classes = classes;
349385
}

0 commit comments

Comments
 (0)