Search code examples
spring-bootopenapispring-modulith

Where to generate OpenAPI types in a Spring Modulith application?


We're using the openapi-generator-maven-plugin to generate the interface layer and DTOs for our Spring Boot application.

Additionally we use Spring Modulith and domain driven application modules to structure our application.

  • we have our application modules within src/main/java/my.app/...
  • we have our generated code within target/generated-sources/my/app/restinterfaces/..

(Example package structure below)

We have the generated API-interfaces and models in a package restinterfaces and we want to allow access to this package from our domainA and domainB packages.

Normally, we would do this via @org.springframework.modulith.NamedInterface in the package-info.java - files. How would we solve this in our case where a package only exists in a generated package-structure.

image showing example package structure


Solution

  • From an architectural perspective, the ideal way to tweak this is to make sure that OpenAPI generates the API and DTO classes into the modules they belong, as a web API logically belongs to the module.

    A (rather ugly) workaround would be to declare a package-info.java in my.app.restinterfaces within src/main/java and annotate that with @ApplicationModule(type = Type.OPEN). This would cause any code inside that package to be accessibly to other application modules.

    That said, this kind of violates the idea that a module is self-contained, i.e., any kind of technology adapters would reside within modules, and not outside of it in a global bucket.