-
Notifications
You must be signed in to change notification settings - Fork 300
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing dependency in a LayeredArchitecture #1012
Comments
Thanks for providing the demo project, that's very helpful! 💚 ArchUnit currently does not analyze local variables (#768), therefore public String dataDetails(@PathVariable("id") String id, Model model) {
Optional<EntityData> data = service.findById(id);
if (data.isEmpty()) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "data " + id + " not available");
}
else {
// *** comment in the following line to see that than the dependency violation is indeed found and shown
// int counter = data.get().counter;
model.addAttribute("dataDetails", data.get());
return "dataDetails"; // direct to Thymeleaf template
}
} (In a way, the current code without the explicit access to In contrast, public EntityData findById(@PathVariable String id) {
Optional<EntityData> entity = service.findById(id);
if (entity.isEmpty()) {
throw new IllegalStateException("nope");
}
else {
return entity.get(); // transformed to JSON automatically
}
} |
Thanks very much. Well, that's what I guessed: Local variable usage is not considered as a dependency. I am a bit confused by the fact that commenting in the line "int counter = data.get().counter;" does help. Still this is only a local variable usage but the test does then report the incorrect dependency. Anyway. Thanks for the hint to #768 , I had missed this one. You might want to tag this issue here as a duplicate of 768, I guess?! |
ArchUnit does analyze each code unit's method calls, so a dependency is recorded once you actually use the |
Ah, I see. Thanks. |
For what it's worth, I think you only can catch these sorts of things reliably if you write something like a test for the surface of the public API of your service layer 🤔 I.e. prevent that the service may even return a persistence type. Because, even if ArchUnit would detect the local variable type, you could simply replace it by
since no properties of the payload are directly used. But then again, the service could also offer a |
I would close this issue, since I don't think this particular problem can really be fully solved with ArchUnit (at least the part that is concerned what really happens at runtime, i.e. at runtime no persistence types are passed). |
Absolutely no objections. If #768 could be adressed, this would be great and much appreciated. Thanks and keep up the good work! |
A few days ago I stumbled over an ArchUnit test for LayeredArchitecture (checking for unwanted dependencies in an Onion-like architecture).
This is a Spring Boot application. I want to check for unwanted direct dependencies to JPA entities from external interfaces - from both a REST interface and also a Web frontend. While this architecture dependency check works perfectly fine for the REST interface, it does not work for the Web frontend.
The main difference between the REST and the Web controller is that
I set up a demo Github project https://github.com/mrtnlhmnn/archunit-dependency-problem. See its Readme.md:
Why is the 'OnionDependenciesTest' not reporting the unwanted dependency from 'WebControllerIncorrectlyUsingPersistenceData' - not before the explicit access (in line 36) is commented in? Note that it is reporting the unwanted dependency from 'RestControllerIncorrectlyUsingPersistenceData' correctly.
Any help very much appreciated!
The text was updated successfully, but these errors were encountered: