MapsterMapper / Mapster

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

Mapping doesn't work for combination source=IReadOnlyDictionary[Int32,String] destination=Dictionary[String,String] #534

Closed YaroslavMudryk closed 1 year ago

YaroslavMudryk commented 1 year ago

Hello, i have:

class Model1
{
      public Dictionary<string, string>? Items1 { get; init; }
}

class Model2
{
      public IReadOnlyDictionary<int, string> Items1 { get; init; } = new Dictionary<int, string>().AsReadOnly();
}
var config = TypeAdapterConfig.GlobalSettings;

var registers = config.Scan(Assembly.GetExecutingAssembly());

config.Apply(registers);

config.NewConfig<Model2, Model1>()
       .Map(destination => destination.Items1, source => source.Items1.ToDictionary(s => s.Key.ToString(), s => s.Value));
model1 = model2.Adapt(model1);

And retrieve next exception: Mapster.CompileException : Error while compiling source=System.Collections.Generic.IReadOnlyDictionary2[System.Int32,System.String] destination=System.Collections.Generic.Dictionary2[System.String,System.String] type=MapToTarget ---- System.ArgumentException : Expression of type 'System.Int32' cannot be used for parameter of type 'System.String' of method 'System.String GetValueOrDefault[String,String](System.Collections.Generic.IDictionary`2[System.String,System.String], System.String)' (Parameter 'arg1')

Mapster v 7.3

Thanks for response!