Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Unable to reverse map from Destination.NestedObject.StringProperty to Source.StringProperty

Open jalwn opened this issue 3 years ago • 1 comments

I have a similar map config as below and I am trying to do a reverse map. However I get an Invalid Cast Exception when I add the map for string property on source to string property on destination's nested object. A different nested object has an integer property but this value can be mapped and reverse mapped to the destination's string property without issues

TypeAdapterConfig<Source, Destination>
    .NewConfig()
    .TwoWays()
    .Map(dest => dest.StringProperty, src => src.NestedObject.IntProperty) // this works, but why?
    .Map(dest => dest.StringProperty, src => src.DifferentNestedObject.StringProperty) // Invalid cast when reverse mapping
    .PreserveReference(true);

// reverse mapping
Source? sourceObject = destObject.Adapt<Destination, Source>();

I get the following error

'Invalid cast from 'System.String' to 'ModelMap.Models.NestedClass'

jalwn avatar Aug 09 '22 13:08 jalwn

@andrerav this issue still exists

mehrdades avatar Jun 22 '23 16:06 mehrdades