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

New instance created when mapping via Adapt existing "child" object with public ctor with parameters #772

Open
gurustron opened this issue Feb 5, 2025 · 5 comments

Comments

@gurustron
Copy link

// Source
public class EditModelCommand
{
    public string Name { get; set; }
    public EditModelChild Child { get; set; }
}

public class EditModelChild
{
    public int Property1 { get; set; }
}

// Destination 
public class Model
{
    public string Name { get; set; }
    public Child Child { get; set; }
}

public class Child
{
    public int Property1 { get; set; }
    public int Property2 { get; set; }

    private Child()
    {
        Debug.WriteLine("new Child");
    }

    public Child(int property1, int property2)
    {
        Debug.WriteLine($"new Child({property1}, {property2})");
        Property1 = property1;
        Property2 = property2;
    }
}

Mapping test:

[Fact]
public void Test()
{
    Model model = new()
    {
        Name = "Model",
        Child = new Child(10, 20)
    };
    EditModelCommand command1 = new()
    {
        Name = "Edited Model",
        Child = new EditModelChild()
        {
            Property1 = 99,
        }
    };
    EditModelCommand command = command1;

    var modelChild = model.Child;

    // ACT
    command.Adapt(model);

    // ASSERT
    Assert.Equal("Edited Model", model.Name);
    Assert.True(object.ReferenceEquals(modelChild, model.Child));
    Assert.Equal(99, model.Child.Property1);
    Assert.Equal(20, model.Child.Property2);
}

Which fails on the assertion.

Removing public Child(int property1, int property2) or changing it's visibility to private (or making parameterless one private Child() public) makes the test pass

@DocSvartz
Copy link

DocSvartz commented Feb 5, 2025

@gurustron Please indicate your Mapster version
Check on latest pre-release?

linked #537 should be resolved in the latest pre-release (record detection)

@gurustron
Copy link
Author

gurustron commented Feb 5, 2025

@DocSvartz

Thank you for the reply!

I was running tests with 7.4.1-pre01. It seems that 7.4.2-pre02 does not have the problem

@daaa57150
Copy link

Confirmed that 7.4.2-pre02 does not have the problem. But the "downside" is the need of .Net 8

@DocSvartz
Copy link

DocSvartz commented Feb 5, 2025

@daaa57150 What is the minimum Net target you need?

@andrerav What do you think about this?

Confirmed that 7.4.2-pre02 does not have the problem. But the "downside" is the need of .Net 8

@daaa57150
Copy link

We are on a somewhat old project now, still using .net 6.0.
I know we should update but it's not that easy for us (small team & the project contains stuff tightly coupled with .net 6.0)

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

3 participants