AmbiguousMatchException on Mapster compile

579 Views Asked by At

When I try to compile the mapster configuration, it throws an AmbiguousMatchException in this specific mapping:


config.NewConfig<Example, ExampleVM>()
    .Map(dest => dest.Id, src => src.Id)
    .Map(dest => dest.Prop1, src => src.Prop1)
    .Map(dest => dest.Prop2, src => src.Prop2)
    .Map(dest => dest.Prop3, src => src.Prop3)
    .Map(dest => dest.Prop1, src => src.Prop1);


config.NewConfig<ExampleVM, Example>()
    .Map(dest => dest.Prop2, src => src.Prop2);



public class Example{
    public int Prop1 {get;set;}
    public DateTime? Prop2 {get;set;}
    public string Prop3 {get;set;}
    public AnotherClass Prop1 {get;set;}
}

public class AnotherClass {

}


public class ExampleVM{
    public int Prop1 {get;set;}
    public DateTime? Prop2 {get;set;}
    public string Prop3 {get;set;}
}


And the exception does not inform what is wrong.

Prior to updating to Mapster 5.0, everything was working normally.

1

There are 1 best solutions below

1
On

Actually what was wrong was that for some reason in this new Mapster Version it's not possible to map a proberty that has the same name, even when the type of the properties are different.

What I needed to do is to remove the duplicated mapping line and rename one of the properties:


config.NewConfig<Example, ExampleVM>()
    .Map(dest => dest.Id, src => src.Id)
    .Map(dest => dest.Prop1, src => src.Prop1)
    .Map(dest => dest.Prop2, src => src.Prop2)
    .Map(dest => dest.Prop3, src => src.Prop3);


config.NewConfig<ExampleVM, Example>()
    .Map(dest => dest.Prop2, src => src.Prop2);



public class Example{
    public int Prop1 {get;set;}
    public DateTime? Prop2 {get;set;}
    public string Prop3 {get;set;}
    public AnotherClass Prop1Data {get;set;}
}

public class AnotherClass {

}


public class ExampleVM{
    public int Prop1 {get;set;}
    public DateTime? Prop2 {get;set;}
    public string Prop3 {get;set;}
}