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.
This commit is contained in:
Oliver Drotbohm
2024-06-18 13:57:31 +02:00
parent c82e0ffc3b
commit e7f2966617
6 changed files with 87 additions and 2 deletions

View File

@@ -69,12 +69,13 @@ public class NamedInterface implements Iterable<JavaClass> {
*/
static List<NamedInterface> of(JavaPackage javaPackage) {
var names = javaPackage.getAnnotation(org.springframework.modulith.NamedInterface.class) //
var basePackage = javaPackage.toSingle();
var names = basePackage.getAnnotation(org.springframework.modulith.NamedInterface.class) //
.map(it -> getDefaultedNames(it, javaPackage.getName())) //
.orElseThrow(() -> new IllegalArgumentException(
String.format("Couldn't find NamedInterface annotation on package %s!", javaPackage)));
var classes = javaPackage.toSingle().getExposedClasses();
var classes = basePackage.getExposedClasses();
return names.stream()
.<NamedInterface> map(it -> new NamedInterface(it, classes)) //

View File

@@ -0,0 +1,38 @@
/*
* Copyright 2024 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 reproducers.gh650;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
import org.springframework.modulith.core.NamedInterface;
import org.springframework.modulith.core.TestUtils;
/**
* @author Oliver Drotbohm
*/
public class Gh650Tests {
@Test // GH-650
void usesBasePackageNamedInterface() {
var module = TestUtils.of("reproducers.gh650").getModuleByName("first").orElseThrow();
assertThat(module.getNamedInterfaces())
.extracting(NamedInterface::getName)
.containsExactlyInAnyOrder("<<UNNAMED>>", "persistence", "persistence.dto");
}
}

View File

@@ -0,0 +1,21 @@
/*
* Copyright 2024 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 reproducers.gh650.first.persistence;
/**
* @author Oliver Drotbohm
*/
public interface SomeRepository {}

View File

@@ -0,0 +1,21 @@
/*
* Copyright 2024 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 reproducers.gh650.first.persistence.dto;
/**
* @author Oliver Drotbohm
*/
public class SomeDto {}

View File

@@ -0,0 +1,2 @@
@org.springframework.modulith.NamedInterface("persistence.dto")
package reproducers.gh650.first.persistence.dto;

View File

@@ -0,0 +1,2 @@
@org.springframework.modulith.NamedInterface
package reproducers.gh650.first.persistence;