BuildAdapter().AdaptToType<T>() does not include all expected properties when source is using inheritance
还没有人认领这个 Issue。
评估
调研方向
首先使用控制台示例重现 Adapt() 和 BuildAdapter().AdaptToType() 之间的差异,尤其是使用 IMyInterface 和 MyImplementation 的 AdaptToTypeWithInterface。跟踪 BuildAdapter().AdaptToType() 入口点,并将其对源类型的处理与 Adapt() 进行比较;当继承的实现属性在同步和异步路径中都被复制时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
While rewriting some code to use the async implementation of adapt using package Mapster.Async v2.0.1, I stubled upon an issue where not all expected source properties were copied to the result class.
However, the code was working as expected when I was using .Adapt<T>().
I've tracked it down to the source class being inherited from a base class and using a method where the parameter is of this base class type.
The following code can be used (e.g. in a console application) to reproduce the issue and show the difference in behavior between .Adapt<T>() and AdaptToType<T>():
using Mapster;
using Newtonsoft.Json;
namespace MapsterBug
{
internal class Program
{
static void Main(string[] args)
{
// sample source object
var myImplementation = new MyImplementation
{
InterfaceProperty = 123,
FirstImplementationProperty = 789,
SecondImplementationProperty = "test"
};
// adapt using .Adapt<T>(): the result is as expected
var resultAdapt = myImplementation.Adapt<MyDto>();
Console.WriteLine("Result of myImplementation.Adapt<MyDto>():");
Console.WriteLine(JsonConvert.SerializeObject(resultAdapt, Formatting.Indented));
Console.WriteLine("\r\n-----------------------------------------\r\n");
// adapt using .AdaptToType<T>(): the result is as expected
var resultAdaptToType = myImplementation.BuildAdapter().AdaptToType<MyDto>();
Console.WriteLine("Result of myImplementation.BuildAdapter().AdaptToType<MyDto>():");
Console.WriteLine(JsonConvert.SerializeObject(resultAdaptToType, Formatting.Indented));
Console.WriteLine("\r\n-----------------------------------------\r\n");
// adapt using .Adapt<T>() inside a method where the parameter is of the interface type: the result is as expected
var resultAdaptWithInterface = AdaptWithInterface(myImplementation);
Console.WriteLine("Result of myImplementation.Adapt<MyDto>() in method with interface:");
Console.WriteLine(JsonConvert.SerializeObject(resultAdaptWithInterface, Formatting.Indented));
Console.WriteLine("\r\n-----------------------------------------\r\n");
// adapt using .AdaptToType<T>() inside a method where the parameter is of the interface type:
// the result only includes the interface property, but it does not include the properties of the implementation
var resultAdaptToTypeWithInterface = AdaptToTypeWithInterface(myImplementation);
Console.WriteLine("Result of myInterface.BuildAdapter().AdaptToType<MyDto>() in method with interface:");
Console.WriteLine(JsonConvert.SerializeObject(resultAdaptToTypeWithInterface, Formatting.Indented));
}
static MyDto AdaptWithInterface(IMyInterface myInterface) => myInterface.Adapt<MyDto>();
static MyDto AdaptToTypeWithInterface(IMyInterface myInterface) => myInterface.BuildAdapter().AdaptToType<MyDto>();
}
public interface IMyInterface
{
public int InterfaceProperty { get; set; }
}
public class MyImplementation : IMyInterface
{
public int InterfaceProperty { get; set; }
public int FirstImplementationProperty { get; set; }
public string SecondImplementationProperty { get; set; }
}
public class MyDto
{
public int InterfaceProperty { get; set; }
public int FirstImplementationProperty { get; set; }
public string SecondImplementationProperty { get; set; }
}
}
This code is using AdaptToType<T>() to not complicate things by using an additional package (Mapster.Async), but the AdaptToTypeAsync<T>() method has the same behavior.
When running the application, the output is as follows:
Result of myImplementation.Adapt<MyDto>():
{
"InterfaceProperty": 123,
"FirstImplementationProperty": 789,
"SecondImplementationProperty": "test"
}
-----------------------------------------
Result of myImplementation.BuildAdapter().AdaptToType<MyDto>():
{
"InterfaceProperty": 123,
"FirstImplementationProperty": 789,
"SecondImplementationProperty": "test"
}
-----------------------------------------
Result of myImplementation.Adapt<MyDto>() in method with interface:
{
"InterfaceProperty": 123,
"FirstImplementationProperty": 789,
"SecondImplementationProperty": "test"
}
-----------------------------------------
Result of myInterface.BuildAdapter().AdaptToType<MyDto>() in method with interface:
{
"InterfaceProperty": 123,
**"FirstImplementationProperty": 0,**
**"SecondImplementationProperty": null**
}
Results 1, 2 and 3 are as expected, but the last result only includes the IMyInterface property (InterfaceProperty) and not the additional MyImplementation properties (FirstImplementationProperty and SecondImplementationProperty) marked in bold.
Versions used in code sample:
- .NET 8
- Mapster 7.4.0
- Newtonsoft.Json 13.0.3
- 主要语言
- C#
- 星标
- 5.2k
- 派生
- 411
- 平均合并
- 1 天 8 小时
- 30 天内合并 PR
- 4
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
MapsterMapper/Mapster 的其他 Issue
-
.NET Matrix未关闭
难度 2/5 1-3 小时 新手友好度 72/100
MapsterMapper/Mapster#1003 ·
-
难度 3/5 1-2 天 新手友好度 58/100
MapsterMapper/Mapster#1024 · 1 条评论 ·
-
[Mapster Tool] Add support for global:: to disambiguate type names in generated mappers可能已有人在做 @DocSvartz 于 3 天前认领。 未关闭improvement
难度 3/5 1-2 天 新手友好度 65/100
MapsterMapper/Mapster#1020 · 5 条评论 ·
-
Mapster.Tool - PropertySettingBuilder.Map(member, mapFunc, targetPropertyName) method is not working as expected!可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭
难度 3/5 1-2 天 新手友好度 68/100
MapsterMapper/Mapster#1017 · 6 条评论 ·
-
难度 3/5 1-2 天 新手友好度 57/100
MapsterMapper/Mapster#1007 · 5 条评论 ·
查看 MapsterMapper/Mapster 的全部 Issue
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 66/100
-
[Bug]: Revit selector reads process ids 2000-2099 as years (flaky AgentClientTests.AYearOrAProcessIdSelectsTheHost)可能已有人在做 @Wurschdhaud 今天认领。 未关闭
难度 2/5 1-3 小时 新手友好度 83/100
pyrevitlabs/pyRevit#3817 ·
维护者通常 1 天内回复
-
bug effort:S P2
难度 2/5 1-3 小时 新手友好度 76/100
nightscout/nocturne#2449 ·
维护者通常 1 天内回复
-
Python: Rejected system messages leave AgentChat permanently active可能已有人在做 @Michael-Duck 今天认领。 未关闭python triage
难度 2/5 1-3 小时 新手友好度 82/100
microsoft/semantic-kernel#14638 ·
维护者通常 3 天内回复
-
audit:1 bug
难度 2/5 1-3 小时 新手友好度 76/100
NethermindEth/dotnet-libp2p#395 ·
维护者通常 1 天内回复