BuildAdapter().AdaptToType<T>() does not include all expected properties when source is using inheritance
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Idoneità per principianti
- 45/100
Direzione di ricerca
Inizia riproducendo la differenza tra Adapt() e BuildAdapter().AdaptToType() usando l’esempio da console, in particolare AdaptToTypeWithInterface con IMyInterface e MyImplementation. Segui il punto di ingresso BuildAdapter().AdaptToType() e confronta la gestione del tipo di origine con Adapt(); il lavoro è completo quando le proprietà dell’implementazione ereditata vengono copiate sia nel percorso sincrono sia in quello asincrono.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
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
- Lingua principale
- C#
- Stelle
- 5.2k
- Fork
- 411
- Merge medio
- 1g 8h
- PR unite (30g)
- 4
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Nessun modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di MapsterMapper/Mapster
-
.NET MatrixAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
MapsterMapper/Mapster#1003 ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 58/100
MapsterMapper/Mapster#1024 · 1 commento ·
-
[Mapster Tool] Add support for global:: to disambiguate type names in generated mappersForse già presa Una pull request collegata a questa issue è aperta o già unita. Apertaimprovement
Difficoltà 3/5 1-2 giorni Idoneità per principianti 65/100
MapsterMapper/Mapster#1020 · 5 commenti ·
-
Mapster.Tool - PropertySettingBuilder.Map(member, mapFunc, targetPropertyName) method is not working as expected!Forse già presa Una pull request collegata a questa issue è aperta o già unita. Aperta
Difficoltà 3/5 1-2 giorni Idoneità per principianti 68/100
MapsterMapper/Mapster#1017 · 6 commenti ·
-
Difficoltà 3/5 1-2 giorni Idoneità per principianti 57/100
MapsterMapper/Mapster#1007 · 5 commenti ·
Tutte le issue di MapsterMapper/Mapster
Issue simili
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 70/100
lugassawan/swe-workbench#823 ·
I maintainer di solito rispondono entro 1 giorno
-
devtools
Difficoltà 2/5 1-3 ore Idoneità per principianti 82/100
I maintainer di solito rispondono entro 1 giorno
-
clawsweeper:fix-shape-clear clawsweeper:needs-maintainer-review clawsweeper:no-new-fix-pr clawsweeper:not-repro-on-main impact:ux-release-blocker issue-rating: 🦪 silver shellfish P0
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
openclaw/openclaw-windows-node#1843 · 1 commento · 1 reazione ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 66/100
-
kind:ux size:S type:bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
elsa-workflows/elsa-foundation#3256 ·
I maintainer di solito rispondono entro 1 giorno