GH-267 - Explicitly declared empty allowed dependencies now forbids any dependency.
The default for @ApplicationModule(allowedDependencies) is now a single element list with a dedicated token we recognize as "all dependencies allowed". This allows users to declare an empty array explicitly to disallow any outgoing dependencies for an application module. Previously, such a declaration would have allowed any dependency.
This commit is contained in:
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2023 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 example.declared.first;
|
||||
|
||||
import example.declared.second.Second;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@Component
|
||||
public class First {
|
||||
|
||||
First(Second second) {}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
// No dependencies allowed
|
||||
@org.springframework.modulith.ApplicationModule(allowedDependencies = {})
|
||||
package example.declared.first;
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* Copyright 2023 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 example.declared.fourth;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@Component
|
||||
public class Fourth {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2023 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 example.declared.second;
|
||||
|
||||
import example.declared.third.Third;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@Component
|
||||
public class Second {
|
||||
Second(Third third) {}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
// No explicit allowed dependencies -> all allowed
|
||||
@org.springframework.modulith.ApplicationModule(displayName = "Second")
|
||||
package example.declared.second;
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2023 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 example.declared.third;
|
||||
|
||||
import example.declared.fourth.Fourth;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* @author Oliver Drotbohm
|
||||
*/
|
||||
@Component
|
||||
public class Third {
|
||||
Third(Fourth fourth) {}
|
||||
}
|
||||
@@ -17,6 +17,11 @@ package org.springframework.modulith.core;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import example.declared.first.First;
|
||||
import example.declared.fourth.Fourth;
|
||||
import example.declared.second.Second;
|
||||
import example.declared.third.Third;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
@@ -164,6 +169,22 @@ class ApplicationModulesIntegrationTest {
|
||||
assertThat(source).containsExactly(ServiceComponentA.class, ServiceComponentB.class, String.class);
|
||||
}
|
||||
|
||||
@Test // GH-267
|
||||
void explicitEmptyAllowedModulesResultsInAllDependenciesRejected() {
|
||||
|
||||
var modules = ApplicationModules.of("example.declared");
|
||||
var first = modules.getModuleByType(First.class).orElseThrow();
|
||||
var second = modules.getModuleByType(Second.class).orElseThrow();
|
||||
var third = modules.getModuleByType(Third.class).orElseThrow();
|
||||
|
||||
// Disallowed due to allowedDependencies = {}
|
||||
assertThat(first.getDeclaredDependencies(modules).isAllowedDependency(Second.class)).isFalse();
|
||||
|
||||
// Allowed as allowedDependencies not set
|
||||
assertThat(second.getDeclaredDependencies(modules).isAllowedDependency(Third.class)).isTrue();
|
||||
assertThat(third.getDeclaredDependencies(modules).isAllowedDependency(Fourth.class)).isTrue();
|
||||
}
|
||||
|
||||
private static void verifyNamedInterfaces(NamedInterfaces interfaces, String name, Class<?>... types) {
|
||||
|
||||
Stream.of(types).forEach(type -> {
|
||||
|
||||
Reference in New Issue
Block a user