fix: prevents incorrect RMG020 warning
Fix: prevents incorrect RMG020 warning
Description
Fixes issue where RMG020 "The member .. on the mapping source type ... is not mapped to any member on the mapping target type ." warnings were incorrectly reported when user mappings with MapperIgnoreSource attributes were used in collection element mapping scenarios.
For more details, see issue #1793.
Solution:
Modified EnumerableMappingBuilder to detect when user mappings exist for non-nullable element types and use FindOrBuildLooseNullableMapping instead of FindOrBuildMapping. This preserves user mapping configurations (like MapperIgnoreSource attributes) and prevents false positive diagnostics.
Fixes #1793
Checklist
- [x] The existing code style is followed
- [x] The commit message follows our guidelines
- [x] Performed a self-review of my code
- [x] Hard-to-understand areas of my code are commented
- [ ] The documentation is updated (as applicable)
- [x] Unit tests are added/updated
- [ ] Integration tests are added/updated (as applicable, especially if feature/bug depends on roslyn or framework version in use)
@latonz I tried applying the suggestion in #1793 (comment) — specifically ignoring IUserMapping in MappingBuilderContext.BuildDelegatedMapping. Unless I misunderstood something, the suggested fix didn’t resolve the issue. Please let me know if you'd like me to explore a different approach, make any changes, or if the current solution looks good to you.
Thank you for your contribution! Why didn't the idea (ignoring IUserMapping in MappingBuilderContext.BuildDelegatedMapping) work?
@latonz I'm not sure, but I tried something like this:
var existingMapping = FindMapping(source, target);
if (existingMapping is IUserMapping)
{
return null;
}
The early return happens when I'm debugging the test that reproduces the error, but the warning still appears.
The easiest way to identify the source of a diagnostic is to set a breakpoint in DiagnosticCollection.ReportDiagnostic. To narrow it down, you can add a condition to the breakpoint that matches the specific RMG number you want to trace.