Skip to content

#4029 Fall back from ADDER_PREFERRED to setter when adder path not applicable - #4108

Open
arimu1 wants to merge 1 commit into
mapstruct:mainfrom
arimu1:fix/4029-adder-fallback-setter
Open

#4029 Fall back from ADDER_PREFERRED to setter when adder path not applicable#4108
arimu1 wants to merge 1 commit into
mapstruct:mainfrom
arimu1:fix/4029-adder-fallback-setter

Conversation

@arimu1

@arimu1 arimu1 commented Aug 8, 2026

Copy link
Copy Markdown

Summary

CollectionMappingStrategy.ADDER_PREFERRED is a preference, not a hard requirement. When the adder path is not applicable — for example mapping a Map source property onto a collection that has both an adder and a setter, with a direct whole-collection mapping method (List map(Map)) — MapStruct previously failed while trying to map into the adder parameter type (e.g. Map → String).

This change falls back to setter-preferred behavior when:

  1. The source is not suitable for adder element matching (notably Map, which is not treated as element-iterable for matching), and a setter exists for the property; or
  2. The adder path still cannot produce an assignment, and a setter is available.

List/iterable/array sources that can use the adder continue to prefer the adder.

Fixes #4029

Changes

  • PropertyMapping: fall back to the property setter when ADDER is selected but not applicable; do not forge doomed Map → adder-element mappings
  • Processor tests in org.mapstruct.ap.test.bugs._4029 covering Map→List (setter fallback) and List→List (adder still preferred)

Test plan

  • Issue4029MapperTest (4 runs: eclipse + javac) — green
  • Regression: AdderTest, Issue3165MapperTest, bugs._1170.AdderTest — green
export JAVA_HOME=…/jdk-21
./mvnw -pl processor -am test -Dtest=Issue4029MapperTest,AdderTest,Issue3165MapperTest -Dsurefire.failIfNoSpecifiedTests=false

…th not applicable

When CollectionMappingStrategy.ADDER_PREFERRED selects an adder but the
source cannot supply elements for adder matching (e.g. Map with a direct
Map→List mapping method), fall back to SETTER_PREFERRED behavior instead
of failing while trying to map into the adder parameter type.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ADDED_PREFFERED should fallback to SETTER_PREFERRED when direct list mapping is given

1 participant