Skip to content

Commit d66259e

Browse files
committed
GH-662 - 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 8d86615 commit d66259e

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
@@ -72,12 +72,13 @@ private NamedInterface(String name, Classes classes) {
7272
*/
7373
static List<NamedInterface> of(JavaPackage javaPackage) {
7474

75-
var names = javaPackage.findAnnotation(org.springframework.modulith.NamedInterface.class) //
75+
var basePackage = javaPackage.toSingle();
76+
var names = basePackage.findAnnotation(org.springframework.modulith.NamedInterface.class) //
7677
.map(it -> getDefaultedNames(it, javaPackage.getName())) //
7778
.orElseThrow(() -> new IllegalArgumentException(
7879
String.format("Couldn't find NamedInterface annotation on package %s!", javaPackage)));
7980

80-
var classes = javaPackage.toSingle().getExposedClasses();
81+
var classes = basePackage.getExposedClasses();
8182

8283
return names.stream()
8384
.<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)