Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Unable to map struct from object in another object

Open jankoci91 opened this issue 2 years ago • 10 comments

Hi! Am I right that failing assert in this test means bug?

In version 6.5.1 it works fine.

using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;

namespace Mapster.Tests
{
    [TestClass]
    public class WhenMappingStructInObject
    {
        class Destination
        {
            public TestStruct TestStruct { get; set; }
        }

        class SourceWithClass
        {
            public SourceWithStruct SourceWithStruct { get; set; }
        }

        class SourceWithStruct
        {
            public TestStruct TestStruct { get; set; }
        }

        struct TestStruct
        {
            public string Property { get; }

            public TestStruct(string property) : this()
            {
                Property = property;
            }
        }

        [TestMethod]
        public void TestMapping()
        {
            TypeAdapterConfig<SourceWithClass, Destination>
                .ForType()
                .Map(x => x.TestStruct, x => x.SourceWithStruct.TestStruct);

            var source = new SourceWithClass
            {
                SourceWithStruct = new SourceWithStruct
                {
                    TestStruct = new TestStruct("A")
                }
            };

            var destination = source.Adapt<Destination>();

            destination.TestStruct.Property.ShouldBe("A");
        }
    }
}

jankoci91 avatar Jul 31 '23 18:07 jankoci91

Any exception stack trace?

codelovercc avatar Aug 01 '23 06:08 codelovercc

There is no exception I'm aware of. Just null instead of "A" as result.

jankoci91 avatar Aug 01 '23 07:08 jankoci91

@jankoci91 Which NuGet package version are you using?

andrerav avatar Aug 09 '23 08:08 andrerav

7.3.0 Sorry for missing info.

jankoci91 avatar Aug 09 '23 08:08 jankoci91

@jankoci91 Can you try the latest prerelease package please?

andrerav avatar Aug 09 '23 09:08 andrerav

Done. Same result. Null instead of "A".

jankoci91 avatar Aug 09 '23 12:08 jankoci91

Hello, This is another type of bug #537 . Struct is detected as Record in https://github.com/MapsterMapper/Mapster/blob/04ac871b55828c3909b6cee4764e6fab40db3983/src/Mapster/Utils/ReflectionUtils.cs#L161

DocSvartz avatar Sep 23 '23 05:09 DocSvartz

Thank you @jankoci91 and @DocSvartz. I ran a git bisect session using the test posted by @jankoci91 above, and ended up with https://github.com/MapsterMapper/Mapster/commit/773f64adac3925878c966990ef26d49290c9ba2a as the supposed culprit. Still trying to piece this together, but now we know which commit that introduced the problem that causes this test to fail.

andrerav avatar Sep 24 '23 19:09 andrerav

@DocSvartz I think you're spot on. IsRecordType() is detecting types as record when it should not.

andrerav avatar Sep 24 '23 20:09 andrerav

@andrerav I managed to make a partial fix. It seems that problems with collections #430 and modification by an Object of type #524 have a different reason for bug. This bug hit Fork and Clone config. There, too, instances are not created transparently. But based on tests, this behavior is legitimate.

DocSvartz avatar Sep 25 '23 01:09 DocSvartz