MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.32k stars 329 forks source link

Adapting derived types does not function as expected #644

Open fytpet opened 11 months ago

fytpet commented 11 months ago

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

DocSvartz commented 11 months ago

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()