Skip to content

#4094 Prefer uses methods when nested target options are only ignores - #4109

Open
arimu1 wants to merge 2 commits into
mapstruct:mainfrom
arimu1:fix/4094-nested-ignore-ignoreByDefault
Open

#4094 Prefer uses methods when nested target options are only ignores#4109
arimu1 wants to merge 2 commits into
mapstruct:mainfrom
arimu1:fix/4094-nested-ignore-ignoreByDefault

Conversation

@arimu1

@arimu1 arimu1 commented Aug 8, 2026

Copy link
Copy Markdown

Summary

With @BeanMapping(ignoreByDefault = true), combining a whole-property mapping and a nested path ignore:

@Mapping(target = "relatedPerson", source = "relatedPerson")
@Mapping(target = "relatedPerson.relatedInformation", ignore = true)
@BeanMapping(ignoreByDefault = true)
PersonInformationDto toDto(PersonInformation source);

caused MapStruct to always forge a nested personToPersonDto method instead of using a suitable method from Mapper#uses. Because the forged method inherited ignoreByDefault and only carried the nested ignore, it mapped nothing — silent data loss. Removing the redundant nested ignore restored correct delegation.

Fix

  1. PropertyMapping: when nested forge options are ignore-only, try existing mapping method selection first (same path as a plain property mapping), and only forge if none is found.
  2. BeanMappingMethod: for forged methods that only carry ignore references, do not let parent ignoreByDefault suppress name-based mapping (so same-named properties still map when no uses method exists). Positive nested mappings under ignoreByDefault (e.g. ToolBox / Ignore mappings by default #1392) are unchanged.
  3. MappingReferences#containsOnlyIgnoreMappings helper.

Fixes #4094

Changes

  • PropertyMapping, BeanMappingMethod, MappingReferences
  • Processor tests in org.mapstruct.ap.test.bugs._4094 (uses mapper + without uses)
  • NEXT_RELEASE_CHANGELOG.md

Test plan

  • Issue4094Test (6 runs: eclipse + javac) — green
  • Regression: IgnorePropertyTest (ignore / expand / inherit), Issue2278Test — green
export JAVA_HOME=…/jdk-21
./mvnw -pl processor -am test \
  -Dtest=Issue4094Test,IgnorePropertyTest,Issue2278Test \
  -Dsurefire.failIfNoSpecifiedTests=false

arimu1 added 2 commits August 8, 2026 13:04
…y ignores

Under BeanMapping(ignoreByDefault = true), a whole-property mapping combined
with a nested path ignore was forcing an empty forged method and silently
skipping Mapper#uses methods. Prefer existing mapping methods when nested
options are ignore-only, and allow name-based mapping for those forged methods.
…-assign

Only retain Mapper#uses mapping-method assignments for ignore-only nested
options; forge for direct/type-conversion so same-type nested ignores apply.
Add same-type regression test.
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.

Silent default mapping on conflicting ignore properties flags

1 participant