Skip to content

Commit cb16a84

Browse files
committed
GH-432 - Include JSR 303 ConstraintValidator implementations in bootstrap dependency analysis.
Spring Boot configures Hibernate Validator in a way that the latter looks up the components it needs to instantiate via the Spring container. That in turn then creates prototype instances of the types requested. This means, implementations of e.g. ConstraintValidator do not need to be explicitly marked as Spring beans, but could still declare dependencies to other Spring beans. If such a dependency crosses a module boundary, we currently fail to detect that implicitly established module dependency. This is now fixed by considering ConstraintValidator implementations Spring beans implicitly in out bootstrap dependency analysis.
1 parent f7e5086 commit cb16a84

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModule.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,10 +567,12 @@ private static Classes filterSpringBeans(JavaPackage source) {
567567
Classes repositories = source.that(isSpringDataRepository());
568568
Classes coreComponents = source.that(not(INTERFACES).and(isComponent()));
569569
Classes configurationProperties = source.that(isConfigurationProperties());
570+
Classes jsr303Validator = source.that(isJsr303Validator());
570571

571572
return coreComponents //
572573
.and(repositories) //
573574
.and(configurationProperties) //
575+
.and(jsr303Validator) //
574576
.and(collect.getOrDefault(true, Collections.emptyList())) //
575577
.and(collect.getOrDefault(false, Collections.emptyList()));
576578
}

spring-modulith-core/src/main/java/org/springframework/modulith/core/Types.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ static DescribedPredicate<? super JavaClass> isConfigurationProperties() {
119119
return isAnnotatedWith(AT_CONFIGURATION_PROPERTIES);
120120
}
121121

122+
static DescribedPredicate<? super JavaClass> isJsr303Validator() {
123+
return implement("jakarta.validation.ConstraintValidator");
124+
}
125+
122126
static boolean isAtBeanMethod(JavaMethod method) {
123127
return isAnnotatedWith(SpringTypes.AT_BEAN).test(method);
124128
}

spring-modulith-integration-test/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@
5252
<artifactId>spring-tx</artifactId>
5353
</dependency>
5454

55+
<dependency>
56+
<groupId>jakarta.validation</groupId>
57+
<artifactId>jakarta.validation-api</artifactId>
58+
</dependency>
59+
5560
<!-- Test -->
5661

5762
<dependency>
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.acme.myproject.validator;
17+
18+
import jakarta.validation.ConstraintValidator;
19+
import jakarta.validation.ConstraintValidatorContext;
20+
21+
import com.acme.myproject.moduleA.ServiceComponentA;
22+
23+
public class SampleValidator implements ConstraintValidator<Override, Object> {
24+
25+
public SampleValidator(ServiceComponentA dependency) {}
26+
27+
@Override
28+
public boolean isValid(Object value, ConstraintValidatorContext context) {
29+
return false;
30+
}
31+
}

spring-modulith-integration-test/src/test/java/org/springframework/modulith/core/ApplicationModulesIntegrationTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import com.acme.myproject.moduleA.ServiceComponentA;
3838
import com.acme.myproject.moduleA.SomeConfigurationA.SomeAtBeanComponentA;
3939
import com.acme.myproject.moduleB.ServiceComponentB;
40+
import com.acme.myproject.validator.SampleValidator;
4041

4142
/**
4243
* @author Oliver Drotbohm
@@ -196,6 +197,20 @@ void excludesSpringAOTGeneratedTypes() {
196197
});
197198
}
198199

200+
@Test // GH-418
201+
void detectsDependencyInducedByValidatorImplementation() {
202+
203+
assertThat(modules.getModuleByName("validator")).hasValueSatisfying(it -> {
204+
205+
assertThat(it.getSpringBeans())
206+
.extracting(SpringBean::getFullyQualifiedTypeName)
207+
.containsExactly(SampleValidator.class.getName());
208+
209+
assertThat(it.getBootstrapDependencies(modules).map(ApplicationModule::getName))
210+
.containsExactly("moduleA");
211+
});
212+
}
213+
199214
private static void verifyNamedInterfaces(NamedInterfaces interfaces, String name, Class<?>... types) {
200215

201216
Stream.of(types).forEach(type -> {

0 commit comments

Comments
 (0)