From b9f3fc2263f476e691e82d06f8868e2d87e22abe Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Thu, 21 Dec 2023 18:09:29 +0100 Subject: [PATCH] GH-418 - 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. --- .../modulith/core/ApplicationModule.java | 2 ++ .../springframework/modulith/core/Types.java | 4 +++ spring-modulith-integration-test/pom.xml | 5 +++ .../myproject/validator/SampleValidator.java | 31 +++++++++++++++++++ .../ApplicationModulesIntegrationTest.java | 15 +++++++++ 5 files changed, 57 insertions(+) create mode 100644 spring-modulith-integration-test/src/main/java/com/acme/myproject/validator/SampleValidator.java diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModule.java b/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModule.java index 0c62e7f3..42832974 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModule.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModule.java @@ -589,10 +589,12 @@ public class ApplicationModule { Classes repositories = source.that(isSpringDataRepository()); Classes coreComponents = source.that(not(INTERFACES).and(isComponent())); Classes configurationProperties = source.that(isConfigurationProperties()); + Classes jsr303Validator = source.that(isJsr303Validator()); return coreComponents // .and(repositories) // .and(configurationProperties) // + .and(jsr303Validator) // .and(collect.getOrDefault(true, Collections.emptyList())) // .and(collect.getOrDefault(false, Collections.emptyList())); } diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/core/Types.java b/spring-modulith-core/src/main/java/org/springframework/modulith/core/Types.java index f6b18c93..2b76e94d 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/core/Types.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/core/Types.java @@ -119,6 +119,10 @@ class Types { return isAnnotatedWith(AT_CONFIGURATION_PROPERTIES); } + static DescribedPredicate isJsr303Validator() { + return implement("jakarta.validation.ConstraintValidator"); + } + static boolean isAtBeanMethod(JavaMethod method) { return isAnnotatedWith(SpringTypes.AT_BEAN).test(method); } diff --git a/spring-modulith-integration-test/pom.xml b/spring-modulith-integration-test/pom.xml index 0d4ba40c..635a7fea 100644 --- a/spring-modulith-integration-test/pom.xml +++ b/spring-modulith-integration-test/pom.xml @@ -52,6 +52,11 @@ spring-tx + + jakarta.validation + jakarta.validation-api + + diff --git a/spring-modulith-integration-test/src/main/java/com/acme/myproject/validator/SampleValidator.java b/spring-modulith-integration-test/src/main/java/com/acme/myproject/validator/SampleValidator.java new file mode 100644 index 00000000..5eab68fe --- /dev/null +++ b/spring-modulith-integration-test/src/main/java/com/acme/myproject/validator/SampleValidator.java @@ -0,0 +1,31 @@ +/* + * Copyright 2023 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 com.acme.myproject.validator; + +import jakarta.validation.ConstraintValidator; +import jakarta.validation.ConstraintValidatorContext; + +import com.acme.myproject.moduleA.ServiceComponentA; + +public class SampleValidator implements ConstraintValidator { + + public SampleValidator(ServiceComponentA dependency) {} + + @Override + public boolean isValid(Object value, ConstraintValidatorContext context) { + return false; + } +} diff --git a/spring-modulith-integration-test/src/test/java/org/springframework/modulith/core/ApplicationModulesIntegrationTest.java b/spring-modulith-integration-test/src/test/java/org/springframework/modulith/core/ApplicationModulesIntegrationTest.java index e0765dd1..e6ca36ed 100644 --- a/spring-modulith-integration-test/src/test/java/org/springframework/modulith/core/ApplicationModulesIntegrationTest.java +++ b/spring-modulith-integration-test/src/test/java/org/springframework/modulith/core/ApplicationModulesIntegrationTest.java @@ -37,6 +37,7 @@ import com.acme.myproject.complex.internal.SecondTypeBasePort; import com.acme.myproject.moduleA.ServiceComponentA; import com.acme.myproject.moduleA.SomeConfigurationA.SomeAtBeanComponentA; import com.acme.myproject.moduleB.ServiceComponentB; +import com.acme.myproject.validator.SampleValidator; /** * @author Oliver Drotbohm @@ -196,6 +197,20 @@ class ApplicationModulesIntegrationTest { }); } + @Test // GH-418 + void detectsDependencyInducedByValidatorImplementation() { + + assertThat(modules.getModuleByName("validator")).hasValueSatisfying(it -> { + + assertThat(it.getSpringBeans()) + .extracting(SpringBean::getFullyQualifiedTypeName) + .containsExactly(SampleValidator.class.getName()); + + assertThat(it.getBootstrapDependencies(modules).map(ApplicationModule::getName)) + .containsExactly("moduleA"); + }); + } + private static void verifyNamedInterfaces(NamedInterfaces interfaces, String name, Class... types) { Stream.of(types).forEach(type -> {