Skip to content

Commit e7f2966

Browse files
committed
GH-669 - Fix named interface detection in case of nested packages.
If named interfaces are declared in nested packages, the name detection might accidentally pick up the names declared in child packages. We now deliberately only inspect the base package.
1 parent c82e0ff commit e7f2966

File tree

6 files changed

+87
-2
lines changed

6 files changed

+87
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,13 @@ private NamedInterface(String name, Classes classes) {
6969
*/
7070
static List<NamedInterface> of(JavaPackage javaPackage) {
7171

72-
var names = javaPackage.getAnnotation(org.springframework.modulith.NamedInterface.class) //
72+
var basePackage = javaPackage.toSingle();
73+
var names = basePackage.getAnnotation(org.springframework.modulith.NamedInterface.class) //
7374
.map(it -> getDefaultedNames(it, javaPackage.getName())) //
7475
.orElseThrow(() -> new IllegalArgumentException(
7576
String.format("Couldn't find NamedInterface annotation on package %s!", javaPackage)));
7677

77-
var classes = javaPackage.toSingle().getExposedClasses();
78+
var classes = basePackage.getExposedClasses();
7879

7980
return names.stream()
8081
.<NamedInterface> map(it -> new NamedInterface(it, classes)) //
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2024 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 reproducers.gh650;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.springframework.modulith.core.NamedInterface;
22+
import org.springframework.modulith.core.TestUtils;
23+
24+
/**
25+
* @author Oliver Drotbohm
26+
*/
27+
public class Gh650Tests {
28+
29+
@Test // GH-650
30+
void usesBasePackageNamedInterface() {
31+
32+
var module = TestUtils.of("reproducers.gh650").getModuleByName("first").orElseThrow();
33+
34+
assertThat(module.getNamedInterfaces())
35+
.extracting(NamedInterface::getName)
36+
.containsExactlyInAnyOrder("<<UNNAMED>>", "persistence", "persistence.dto");
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2024 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 reproducers.gh650.first.persistence;
17+
18+
/**
19+
* @author Oliver Drotbohm
20+
*/
21+
public interface SomeRepository {}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright 2024 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 reproducers.gh650.first.persistence.dto;
17+
18+
/**
19+
* @author Oliver Drotbohm
20+
*/
21+
public class SomeDto {}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@org.springframework.modulith.NamedInterface("persistence.dto")
2+
package reproducers.gh650.first.persistence.dto;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@org.springframework.modulith.NamedInterface
2+
package reproducers.gh650.first.persistence;

0 commit comments

Comments
 (0)