Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Adapting derived types does not function as expected

Open fytpet opened this issue 2 years ago • 1 comments

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

fytpet avatar Oct 17 '23 20:10 fytpet

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

car.Adapt<Vehicule, VehiculeDto>()

car.Adapt<VehiculeDto>()

DocSvartz avatar Oct 18 '23 12:10 DocSvartz