Skip to content

cis-api mapper constructor injection

Scope

All 127 MapStruct mappers in cis-api explicitly select InjectionStrategy.CONSTRUCTOR. The existing field mappings, uses relationships, expression imports, inverse configurations and warning policies were retained. This change does not resolve the repository lookups documented in the persistence audit.

Thirty abstract MapStruct decorators are now concrete Spring decorators. Each has @Component, @Primary, final dependencies and one explicit constructor. The generated mapper is the qualified delegate, for example:

public TemplateMapperDecorator(
        @Qualifier("templateMapperImpl") TemplateMapper delegate,
        @Qualifier("userRepository") UserRepository userRepository) {
    this.delegate = delegate;
    this.userRepository = userRepository;
}

These decorators implement their mapper interfaces directly. Existing custom methods are retained; other interface methods explicitly forward to the generated delegate. Unqualified injection of the interface resolves the primary decorator. The constructor qualifier resolves the generated implementation and prevents the decorator from injecting itself. Do not restore @DecoratedWith on these interfaces: that would generate another decorator wrapper and reintroduce the constructor/subclass limitation.

Generated mapper bean names follow Spring's class-name convention (TemplateMapperImpl becomes templateMapperImpl). When renaming a mapper or overriding its implementation name, update its decorator qualifier. The complete mapper-context test detects broken qualifiers and dependency cycles.

The archive mapper's five existing @AfterMapping methods moved from its abstract generated superclass into HistoricalRecordsArchiveEnricher, a constructor-injected component referenced through uses. This avoids leaving repository field injection behind in an abstract mapper. The existing workflow-note enrichment hook was already constructor-injected and remains in place.

All touched Java files use organized explicit imports. No generated source was edited. The root build's defaults for other modules were left unchanged; each cis-api mapper's explicit strategy takes precedence.

Validation

  • A clean cis-api main/test compilation passed, with existing nonblocking compiler warnings.
  • The full mapper component graph starts in an isolated Spring context with external dependencies mocked. Every component's non-static fields are checked for finality; field/method injection is rejected; decorator interfaces resolve to their primary wrappers.
  • Six focused tests pass across MapperConstructorWiringTest, MapperInjectionTest, WorkflowNotesMappingTest and HistoricalArchiveMappingTest. They cover nested address mapping, decorator forwarding and username enrichment, archive enrichment/reverse IDs/null references, and workflow notes.
  • During migration, 350 generated mapping methods were compared with their pre-change versions, ignoring whitespace and the archive hook receiver. All matched. All 51 pre-existing decorator method bodies and five extracted archive hook bodies also matched. Dependency setters were intentionally removed from that comparison.
  • Import organization, no-inline-type checks and scoped whitespace checks passed.

This verifies mapper construction and the covered mapping behavior. It is not a full application-startup, HTTP contract, database or complete test-suite result. Request/response DTOs, controllers and repository queries were not changed.

Reproduce the focused checks from the repository root:

./gradlew.bat :cis-api:clean :cis-api:compileJava :cis-api:compileTestJava --offline --console=plain
./gradlew.bat :cis-api:test --tests '*MapperConstructorWiringTest' --tests '*MapperInjectionTest' --tests '*WorkflowNotesMappingTest' --tests '*HistoricalArchiveMappingTest' --offline --console=plain

The implementation uses constructor injection and ordinary Spring decorator composition because MapStruct documents restrictions for generated abstract-class/decorator injection. See the MapStruct injection strategy reference.