Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Potential regression from minor versions 7.20 --> 7.30

Open agause opened this issue 3 years ago • 3 comments

Using the static Adapt<> method, I adapt from a concrete type to an interface (all properties defined w/ get & set).

All is well and the expected property values are transported to the generated type.

Upon adapting from a 2nd concrete type to the newly minted instance (generated from the interface), the 2nd adaptation nullifies a member of the interface instance -- presumably b/c the 2nd concrete type defines no matching member.

7.20 leaves the populated interface property in peace 7.30 nullifies the previously populated interface property

If this isn't intended behavior, it's a pretty dangerous regression. If it is intended behavior, it's

  1. a breaking change, so more appropriate in a major version revision and
  2. a change that needs to be called out in notes, given past [expected] behavior.

I'm a big fan and rely heavily on Mapster and, fortunately, found the issue during testing and before releasing to production.

Please educate me on the expected behavior in v7.30 when adapting from Type2 to Type1, where Type2 does not define a property that's present [and populated] in Type1?

Thanks much and apologies if this duplicates an existing issue.

agause avatar Oct 12 '22 08:10 agause

@agause I will make an attempt to reproduce this. Meanwhile, will you perhaps consider trying the latest prerelease version and check if the issue is still present there?

andrerav avatar Jan 08 '23 00:01 andrerav

@agause I tried to reproduce the problem, and wrote this test case:

    [TestClass]
    public class WhenMappingWithSecondSourceObject
    {
        public interface ISomeType
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
        }
        public class ConcreteType1 : ISomeType
        {
            public int Id { get; set; }
            public string Name { get; set; }

            public string Address { get; set; }
        }

        public class ConcreteType2
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }

        [TestMethod]
        public void TestMapFromSecondSourceObject()
        {
            var c1 = new ConcreteType1
            {
                Id = 1,
                Name = "Name 1",
                Address = "Address 1"
            };

            var c2 = new ConcreteType2
            {
                Id = 2,
                Name = "Name 2"
            };

            var generatedType = c1.Adapt<ISomeType>();

            generatedType.Id.ShouldBe(1);
            generatedType.Name.ShouldBe("Name 1");
            generatedType.Address.ShouldBe("Address 1");

            generatedType = c2.Adapt(generatedType);

            generatedType.Id.ShouldBe(2);
            generatedType.Name.ShouldBe("Name 2");
            generatedType.Address.ShouldBe("Address 1");
        }
    }

However, this test passes. Can you take a look and see if I made an error in the logic? You mention that you are using the static Adapt<> method -- can you perhaps some me some code samples?

andrerav avatar Jan 08 '23 00:01 andrerav

@agause Does the second Adapt() call create a new object instance?

andrerav avatar Jan 29 '23 21:01 andrerav