You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I can reproduce the bug using the latest prerelease version
I have searched existing discussion and issue to avoid duplicates
Describe the bug
When defining MapProperty rules on a method that maps from a parent type to sub-type the generated code is a downcast and the MapProperty instruction is ignored. A real-world example where this was picked up is a class that encrypts items in its base class. When you need to convert it back to the base class the encrypted values need to be copied back into the base class properties unencrypted. MapProperty is used to instruct it to do this but the generated code does not include this instruction.
publicstaticpartialglobal::MapperlyInheritanceMapPropertyBug.ComplexTypeWorkingMapper(thisglobal::MapperlyInheritanceMapPropertyBug.AnotherComplexTypesource){vartarget=(global::MapperlyInheritanceMapPropertyBug.ComplexType)source;// Use MapProperty instructions.target.Strings=source.SameStrings;returntarget;}ORpublicstaticpartialglobal::MapperlyInheritanceMapPropertyBug.ComplexTypeWorkingMapper(thisglobal::MapperlyInheritanceMapPropertyBug.AnotherComplexTypesource){vartarget=newglobal::MapperlyInheritanceMapPropertyBug.ComplexType();target.Strings=global::System.Linq.Enumerable.ToList(source.SameStrings);returntarget;}
Reported relevant diagnostics
None
Environment (please complete the following information):
Mapperly Version: 4.1.1
Nullable reference types: Enabled
.NET Version: 9.0.0
Target Framework: net9.0
Compiler Version: 4.12.0-3.24572.7 (dfa7fc6b)
C# Language Version: 13.0
IDE: Visual Studio v17.12.3
OS: Windows 11
Additional context
Using UseDeepCloning works correctly producing the following generated code:
However, this applies to the entire Mapper class not just specific mapper methods and in complex mappers with dozens of types you would sacrifice reference copying performance across the board.
The text was updated successfully, but these errors were encountered:
Thanks for reporting. Currently this is the expected behaviour, as Mapperly considers a cast as a "complete" mapping and doesn't apply any further mapping rules. I can see that there are use-cases for which additional configuration rules make sense, I changed it from a bug to a feature request 😊
The more I think about it and go over this with colleagues of mine, we seem to agree this violates POLA. If you've supplied mapping rules the expectation is that they are honoured. I think it's certainly a surprise when attributes are ignored.
The code in question was a conversion from AutoMapper that was quickly picked up as not working despite the correct code conversion being put in place. And not to say AutoMapper made all the correct assumptions about things, but if you supplied a FromMember rule it would certainly not ignore it.
Describe the bug
When defining
MapProperty
rules on a method that maps from a parent type to sub-type the generated code is a downcast and theMapProperty
instruction is ignored. A real-world example where this was picked up is a class that encrypts items in its base class. When you need to convert it back to the base class the encrypted values need to be copied back into the base class properties unencrypted.MapProperty
is used to instruct it to do this but the generated code does not include this instruction.Declaration code
Actual relevant generated code
Expected relevant generated code
Reported relevant diagnostics
None
Environment (please complete the following information):
Additional context
Using
UseDeepCloning
works correctly producing the following generated code:However, this applies to the entire Mapper class not just specific mapper methods and in complex mappers with dozens of types you would sacrifice reference copying performance across the board.
The text was updated successfully, but these errors were encountered: