spring-modulith icon indicating copy to clipboard operation
spring-modulith copied to clipboard

Prevent module misconfiguration from additional packages that enclose the root package or another additional package

Open nikolamicic21 opened this issue 1 month ago • 0 comments

#1247 filters an additionalPackages entry that is nested in the root package. Two similar entries are still registered as root packages, and both give unexpected modules:

  1. an entry that encloses the root package, for example "com" for an application in com.example.app;
  2. an entry that is nested in another additional package, for example "com.other.billing" next to "com.other".

Reproducer

A minimal Maven project, attached as spring-modulith-additional-packages-repro.zip. Run mvn test.

  • The application is in com.example.app, with the module com.example.app.order.
  • A second root has com.other.billing, com.other.billing.invoice and com.other.billing.payment. The invoice package has @ApplicationModule without an id. The payment package has no annotation.
  • Five @Modulithic types in com.example.app declare the configurations: the intended one, the two cases, and the two cases with useFullyQualifiedModuleNames = true.
  • AdditionalPackagesReproTest prints ApplicationModules.of(type) for each one.
  • AdditionalPackagesVerifyTest calls ApplicationModules.of(type).verify() for each one.

Actual result

The intended configuration, additionalPackages = "com.other":

billing -> com.other.billing
billing.invoice -> com.other.billing.invoice
order -> com.example.app.order

1. additionalPackages = "com": 11 modules

apple -> com.apple
example -> com.example
fasterxml -> com.fasterxml
jayway -> com.jayway
jetbrains -> com.jetbrains
order -> com.example.app.order
other -> com.other
other.billing.invoice -> com.other.billing.invoice
structurizr -> com.structurizr
sun -> com.sun
tngtech -> com.tngtech
  • The packages of libraries and of the JDK become application modules: com.fasterxml, com.tngtech, com.sun, com.apple and others. The default ImportOption excludes only the tests, so importPackages("com") imports the classes of every JAR on the classpath.
  • com.example becomes the module example. It holds the application package com.example.app and every type there that is not in another module.

verify() fails with 146 violations between two libraries, which the user cannot fix. They name 6 distinct types:

org.springframework.modulith.core.Violations:
- Module 'structurizr' depends on non-exposed type com.fasterxml.jackson.annotation.JsonGetter within module 'fasterxml'!
- Module 'structurizr' depends on non-exposed type com.fasterxml.jackson.annotation.JsonIgnore within module 'fasterxml'!
- Module 'structurizr' depends on non-exposed type com.fasterxml.jackson.annotation.JsonInclude within module 'fasterxml'!
- Module 'structurizr' depends on non-exposed type com.fasterxml.jackson.annotation.JsonInclude$Include within module 'fasterxml'!
- Module 'structurizr' depends on non-exposed type com.fasterxml.jackson.annotation.JsonSetter within module 'fasterxml'!
- Module 'structurizr' depends on non-exposed type com.fasterxml.jackson.annotation.Nulls within module 'fasterxml'!

With "com", each test takes 7 to 11 seconds, because it imports all classes under com on the classpath. With the other configurations, each test takes less than a second.

2. additionalPackages = { "com.other", "com.other.billing" }: 5 modules

billing -> com.other.billing
billing.invoice -> com.other.billing.invoice
invoice -> com.other.billing.invoice
order -> com.example.app.order
payment -> com.other.billing.payment
  • Two modules on one package: com.other.billing.invoice is found from both roots, with an identifier relative to each root. ApplicationModuleSource.equals compares the identifier and the package, so both sources stay. The types of invoice then belong to two modules. This is the situation from #1247, where getModuleForPackage picked one of them by iteration order.
  • A split module: com.other.billing.payment becomes its own module. With the intended configuration, it is a part of billing.

verify() passes, so nothing tells the user about either effect.

useFullyQualifiedModuleNames = true helps only with the duplicate

With "com", the result has the same 11 modules, with fully qualified identifiers such as com.fasterxml. verify() fails with the same 146 violations.

With { "com.other", "com.other.billing" }, the result has 4 modules:

com.example.app.order -> com.example.app.order
com.other.billing -> com.other.billing
com.other.billing.invoice -> com.other.billing.invoice
com.other.billing.payment -> com.other.billing.payment

Both roots now give invoice the same identifier, so the two sources are equal and only one module stays. payment is still a module of its own.

Expected result

Spring Modulith rejects or ignores both entries, as #1247 does for an entry in the root package:

  1. An entry that encloses the root package fails at startup with a clear message. If an enclosing entry has a valid use, Spring could ignore it and log a warning instead.
  2. An entry that is nested in another additional package is ignored, because the enclosing entry already scans it.

Related

  • #1247: the same filter, for an entry that is nested in the root package.
  • #1964: duplicate module identifiers from two roots. A broad entry such as "com" makes this more likely.
  • #613: modules from other JARs through additionalPackages. This is why the import of JARs is correct, and only a too broad entry is the problem.

spring-modulith-additional-packages-repro.zip

nikolamicic21 avatar Oct 30 '26 10:10 nikolamicic21