Skip to content

Commit

Permalink
GH-798 - Guard against multiple non-unique module base packages.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Sep 4, 2024
1 parent 86eae79 commit b95b0c0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,14 @@ public Optional<String> getSystemName() {
@Override
public List<String> getBasePackages() {

var result = new ArrayList<>(List.of(basePackage));
result.addAll(List.of(annotation.additionalPackages()));
var result = new ArrayList<String>();
result.add(basePackage);

for (var candidate : annotation.additionalPackages()) {
if (!result.contains(candidate)) {
result.add(candidate);
}
}

return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,14 @@ protected ApplicationModules(ModulithMetadata metadata, Collection<String> 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();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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", //
Expand All @@ -82,4 +90,7 @@ static class ModuliticAnnotated {}
static class SpringBootApplicationAnnotated {}

static class Unannotated {}

@Modulithic(additionalPackages = "org.springframework.modulith.core")
static class AdditionalPackagesShadowing {}
}

0 comments on commit b95b0c0

Please sign in to comment.