Fix/collection element field transformer null regression#654
Merged
fborriello merged 4 commits intomasterfrom Mar 31, 2026
Merged
Conversation
…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().
Test Coverage Report
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 togetSourceFieldName(), 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 == sourceObjso both paths were equivalent; at nested level the breadcrumb is always non-null so the guard has no effect.How Has This Been Tested?
Checklist:
feature: Feature I'm adding or expandingbug: Bugfix or experimentwip: Work in progress; stuff I know won't be finished soon