Skip to content

Fix/collection element field transformer null regression#654

Merged
fborriello merged 4 commits intomasterfrom
fix/collection-element-field-transformer-null-regression
Mar 31, 2026
Merged

Fix/collection element field transformer null regression#654
fborriello merged 4 commits intomasterfrom
fix/collection-element-field-transformer-null-regression

Conversation

@fborriello
Copy link
Copy Markdown
Member

@fborriello fborriello commented Mar 31, 2026

Description

Guard the breadcrumb-based mapping lookup with isNotEmpty(breadcrumb). When breadcrumb is null (collection element transforms and root-level transforms), the method falls through to getSourceFieldName(), which already performs the same mapping lookup correctly using the element as the source.

This has no impact on root-level or nested-object transforms: at root level rootSourceObj == sourceObj so both paths were equivalent; at nested level the breadcrumb is always non-null so the guard has no effect.

How Has This Been Tested?

  • Added two regression tests in MixedObjectTransformationTest covering the exact failure scenario with multi-element lists
  • Fixed test state leakage in AbstractBeanTransformerTest.beforeMethod() by resetting field mappings alongside existing field transformer resets
  • All 663 existing tests pass

Checklist:

  • The branch follows the best practices naming convention:
    • Use grouping tokens (words) at the beginning of your branch names.
      • feature: Feature I'm adding or expanding
      • bug: Bugfix or experiment
      • wip: Work in progress; stuff I know won't be finished soon
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented on my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • Any dependent changes have been merged and published in downstream modules
  • My changes have no bad impacts on performances
  • My changes have been tested through Unit Tests and the overall coverage has not decreased. Check the coverall report for your branch: here
  • Any implemented change has been added in the CHANGELOG.md file
  • Any implemented change has been added in the CHANGELOG-JDK11.md file
  • My changes have been applied on a separate branch too with compatibility with Java 11. Follow this instructions for help.
  • The maven version has been increased.

…ents with flat field name mapping

When a CollectionPopulator transforms list elements (e.g. List<PersonSvc> -> List<Person>),
it calls transformer.transform(elem, targetClass) without a breadcrumb. In resolveEffectiveSource,
evalBreadcrumb("fieldName", null) returns just "fieldName", which could match a simple flat
field mapping (e.g. "name" -> "fullName"). This caused the lookup to incorrectly redirect to
the root source object instead of the current element, and getSourceFieldValue silently returned
null (swallowing the MissingFieldException because a transformer was defined), ultimately
passing null into the user's transformer function and triggering a NullPointerException.

Fix: guard the breadcrumb-based mapping lookup with isNotEmpty(breadcrumb) so root-source
redirection only occurs when there is an actual breadcrumb prefix in the path.
Add tests verifying that field transformers using flat field name transformation
correctly use the collection element as the source, not the root object.

New sample classes:
- FromFooWithFullName: source item with a fullName field
- FromFooContainingList: root source with id + List<FromFooWithFullName>
- ImmutableToFooWithSplitName: target item with name + surname fields
- ImmutableToFooContainingList: root target with id + List<ImmutableToFooWithSplitName>

New tests in MixedObjectTransformationTest:
- testFieldTransformerOnCollectionElementsUsesElementNotRootAsSource: verifies
  that withFieldMapping + withFieldTransformer on collection elements reads from
  the element, not the root (would have NPEd before the fix)
- testAllCollectionElementsAreIndependentlyTransformedFromTheirOwnSource: verifies
  each element is transformed from its own source independently

Also adds resetFieldsMapping() to AbstractBeanTransformerTest.beforeMethod() so
field name mappings are always cleared between tests, preventing state leakage
when a test throws before calling underTest.reset().
@github-actions
Copy link
Copy Markdown

github-actions bot commented Mar 31, 2026

Test Coverage Report

Coverage Baseline Build Diff
Lines 100.00% 1224/1224 100.00% 1225/1225 +0.00% 💙
Branches 94.79% 455/480 94.81% 457/482 +0.02% 💚
Instructions 99.79% 6067/6080 99.79% 6070/6083 +0.00% 💙
Complexity 96.92% 756/780 96.93% 757/781 +0.01% 💚
Methods 100.00% 540/540 100.00% 540/540 +0.00% 💙
Classes 100.00% 50/50 100.00% 50/50 +0.00% 💙
Normalised Score 98.58% 98.59% +0.00% 💙

@fborriello fborriello marked this pull request as ready for review March 31, 2026 08:46
@fborriello fborriello requested a review from a team as a code owner March 31, 2026 08:46
@fborriello fborriello merged commit 6519502 into master Mar 31, 2026
10 checks passed
@fborriello fborriello deleted the fix/collection-element-field-transformer-null-regression branch March 31, 2026 09:03
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.

1 participant