Mapster
Mapster copied to clipboard
Adapting derived types does not function as expected
Hello,
The TypeAdapterSetter.Include method as demonstrated here to adapt derived types only works when the TypeAdapter.Adapt method is provided both TSource and TDestination type parameters.
Given this configuration:
TypeAdapterConfig<Vehicle, VehicleDto>.NewConfig()
.Include<Car, CarDto>();
When the method TypeAdapter.Adapt is provided both TSource and TDestination type parameters, the behavior is as expected:
Vehicule car = new Car() { Make = "Toyota" };
VehiculeDto actual = car.Adapt<Vehicule, VehiculeDto>();
Assert.True(actual is CarDto);
However, when the method TypeAdapter.Adapt is only provided the TDestination type parameter, the derived type is not adapted:
Vehicule car = new Car() { Make = "Toyota" };
VehiculeDto actual = car.Adapt<VehiculeDto>();
Assert.True(actual is CarDto); // Assert.True() Failure, Expected: True, Actual: False
Is there any way to fix this?
Thanks
Hello, I think, these are different types of adapters.
car.Adapt<VehiculeDto>(); This is a directive adapter. you have to get exactly what was requested - VehiculeDto