Stack constructor reverses order during deep cloning
Please do the checklist before filing an issue:
- [X] I have read the documentation, including the FAQ
- [X] I can reproduce the bug using the latest prerelease version
- [X] I have searched existing discussion and issue to avoid duplicates
Describe the bug Hello! Thank you everyone for this wonderful package! I have been using over AutoMapper! This is something that I can workaround with a UserDefined mapping, but it seems to be a bug. I hope I submitted this properly.
When Mapperly uses deep cloning, it generates new Stack<T>(source) which reverses the stack order due to the constructor's behavior.
This is also an issue when it is part of another object like Dictionary<string, Stack<string>>, or in a nested mapping.
Microsoft decided not to fix this same issue for deserialization. As far as I can tell it is because it would be a breaking change.
Declaration code
[Mapper(UseDeepCloning = true)]
public static partial class TestMapper
{
public static partial Stack<string> MapStack(Stack<string> source);
}
Actual relevant generated code
public static partial class TestMapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.1.0")]
public static partial global::System.Collections.Generic.Stack<string> MapStack(global::System.Collections.Generic.Stack<string> source)
{
return new global::System.Collections.Generic.Stack<string>(source);
}
}
Expected relevant generated code
public static partial class TestMapper
{
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.1.0")]
public static partial global::System.Collections.Generic.Stack<string> MapStack(global::System.Collections.Generic.Stack<string> source)
{
return new global::System.Collections.Generic.Stack<string>(source.Reverse());
}
}
Environment (please complete the following information):
- Mapperly Version:
4.3.0-next.3 - Nullable reference types:
enabled - .NET Version: .NET
8.0 - Target Framework:
net8.0 - Compiler Version:
4.11.0-3.24554.2 (bc1c3011) - C# Language Version:
12.0 - IDE:
JetBrains Rider - OS:
Windows
If this is the default type behavior (which I agree with you that is pretty bad) I don’t think Mapperly should be responsible for magically fixing it (for the same reason why MS didn't want to fix in the framework).
I didn't use deep cloning yet so sorry if the question will sound stupid but can't you just define your user impl method and handle it how you want?
This is indeed a behavioral bug. We plan to fix this in the next breaking change, updating the deep cloning logic to maintain the original stack order rather than reverse it.
For now you should be able to use a user implemented method.
Thanks for catching this!