From b95b0c0d7dbb2115c18b2509f2e6204dbf1c0fda Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Wed, 4 Sep 2024 09:51:48 +0200 Subject: [PATCH] GH-798 - Guard against multiple non-unique module base packages. --- .../modulith/core/AnnotationModulithMetadata.java | 10 ++++++++-- .../modulith/core/ApplicationModules.java | 2 ++ .../modulith/core/ModulithMetadataUnitTest.java | 11 +++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/core/AnnotationModulithMetadata.java b/spring-modulith-core/src/main/java/org/springframework/modulith/core/AnnotationModulithMetadata.java index 2853f2269..d1bc0dec6 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/core/AnnotationModulithMetadata.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/core/AnnotationModulithMetadata.java @@ -134,8 +134,14 @@ public Optional getSystemName() { @Override public List getBasePackages() { - var result = new ArrayList<>(List.of(basePackage)); - result.addAll(List.of(annotation.additionalPackages())); + var result = new ArrayList(); + result.add(basePackage); + + for (var candidate : annotation.additionalPackages()) { + if (!result.contains(candidate)) { + result.add(candidate); + } + } return result; } diff --git a/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java b/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java index 02ae5831b..fa2d5809b 100644 --- a/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java +++ b/spring-modulith-core/src/main/java/org/springframework/modulith/core/ApplicationModules.java @@ -139,12 +139,14 @@ protected ApplicationModules(ModulithMetadata metadata, Collection packa var strategy = ApplicationModuleDetectionStrategyLookup.getStrategy(); this.modules = packages.stream() // + .distinct() .map(it -> JavaPackage.of(classes, it)) .flatMap(strategy::getModuleBasePackages) // .map(it -> new ApplicationModule(it, useFullyQualifiedModuleNames)) // .collect(toMap(ApplicationModule::getName, Function.identity())); this.rootPackages = packages.stream() // + .distinct() .map(it -> JavaPackage.of(classes, it).toSingle()) // .toList(); diff --git a/spring-modulith-core/src/test/java/org/springframework/modulith/core/ModulithMetadataUnitTest.java b/spring-modulith-core/src/test/java/org/springframework/modulith/core/ModulithMetadataUnitTest.java index 6c12242cb..6bafceede 100644 --- a/spring-modulith-core/src/test/java/org/springframework/modulith/core/ModulithMetadataUnitTest.java +++ b/spring-modulith-core/src/test/java/org/springframework/modulith/core/ModulithMetadataUnitTest.java @@ -66,6 +66,14 @@ public void rejectsTypeNotAnnotatedWithEitherModulithAnnotationOrSpringBootAppli .withMessageContaining(SpringBootApplication.class.getSimpleName()); } + @Test // GH-798 + void returnsUniqueBasePackages() { + + var metadata = ModulithMetadata.of(AdditionalPackagesShadowing.class); + + assertThat(metadata.getBasePackages()).containsExactly(getClass().getPackageName()); + } + @Modulith(additionalPackages = "com.acme.foo", // sharedModules = "shared.module", // systemName = "systemName", // @@ -82,4 +90,7 @@ static class ModuliticAnnotated {} static class SpringBootApplicationAnnotated {} static class Unannotated {} + + @Modulithic(additionalPackages = "org.springframework.modulith.core") + static class AdditionalPackagesShadowing {} }