Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to map struct from object in another object #615

Open
jankoci91 opened this issue Jul 31, 2023 · 10 comments
Open

Unable to map struct from object in another object #615

jankoci91 opened this issue Jul 31, 2023 · 10 comments
Assignees

Comments

@jankoci91
Copy link

jankoci91 commented Jul 31, 2023

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");
        }
    }
}
@codelovercc
Copy link

Any exception stack trace?

@jankoci91
Copy link
Author

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

@andrerav andrerav self-assigned this Aug 9, 2023
@andrerav
Copy link
Contributor

andrerav commented Aug 9, 2023

@jankoci91 Which NuGet package version are you using?

@jankoci91
Copy link
Author

7.3.0 Sorry for missing info.

@andrerav
Copy link
Contributor

andrerav commented Aug 9, 2023

@jankoci91 Can you try the latest prerelease package please?

@jankoci91
Copy link
Author

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

@DocSvartz
Copy link

DocSvartz commented Sep 23, 2023

Hello, This is another type of bug #537 . Struct is detected as Record in

public static bool IsRecordType(this Type type)

@andrerav
Copy link
Contributor

Thank you @jankoci91 and @DocSvartz. I ran a git bisect session using the test posted by @jankoci91 above, and ended up with 773f64a 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
Copy link
Contributor

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

@DocSvartz
Copy link

DocSvartz commented Sep 25, 2023

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants