fix: guard nullable paths in existing target mappings#2373
Open
AmirTahan80 wants to merge 1 commit into
Open
Conversation
Use null-conditional source access when building existing-target null guards so nullable intermediate members are not dereferenced. Add regression coverage for nullable-aware and disabled-nullability contexts.
Author
|
Implementation note: the generated null guard and mapping body intentionally use different source expressions.
The regression tests cover the original disabled-nullability reproduction from #2303 and an explicit nullable intermediate-member variant. Full local validation completed with 1,727 tests passed, 2 skipped, and 0 failed. |
AmirTahan80
marked this pull request as ready for review
July 18, 2026 12:15
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.
Fixes #2303.
Summary
Existing-target mappings for nested collections could dereference a nullable intermediate source member while evaluating the generated null guard. For example, Mapperly generated
source.Nested.Items != null, which throws whensource.Nestedis null.Root cause
MemberExistingTargetMappingpassed the same direct member-access expression to both the null guard and the mapping body. The mapping body should use direct access after the guard has succeeded, but the guard itself needs null-conditional access across nullable intermediate members.Changes
NullDelegateExistingTargetMappingto receive a separate source expression for its null check.MemberExistingTargetMapping.Impact
Generated mappings now safely check paths such as
source.Nested?.Itemsbefore populating an existing readonly target collection. When an intermediate source object is null, the collection mapping is skipped instead of throwingNullReferenceException.Validation
dotnet build Riok.Mapperly.slnx /p:TreatWarningsAsErrors=true --no-restoredotnet test Riok.Mapperly.slnx --no-build --no-restore