MapsterMapper / Mapster

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

`MapWith()` and `MapToTargetWith()` are ignored when `ShallowCopyForSameType` is set #628

Closed rafalka closed 1 year ago

rafalka commented 1 year ago

When ShallowCopyForSameType then custom conversion defined by MapWith() or MapToTargetWith() is ignored

Sample code (based on existing UT with added ShallowCopyForSameType config)

      [TestMethod]
      public void MapToTargetWith_Should_Work_With_Adapt_To_Target()
      {
          var a = new Foo { A = 1 };
          var b = new Bar { A = 2 };

          //This will not work as expected => b.A will be 1, ignoring the mapping defined
          var config = new TypeAdapterConfig();
          config.Default.ShallowCopyForSameType(true);

          config.NewConfig<double, double>().MapToTargetWith((x, y) => 5);
          a.Adapt(b, config);
          b.A.ShouldBe(5);
      }

      internal class Foo
      {
          public double A { get; set; }
      }

      internal class Bar
      {
          public double A { get; set; }
      }