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

When using TypeAdapterConfig.GlobalSettings.Default.IgnoreNullValues(true), it causes the Mapper function to be called twice. #737

Open
jiuzhou2019 opened this issue Nov 25, 2024 · 1 comment

Comments

@jiuzhou2019
Copy link

There is example code following this. When Adapt() is called, it can be seen from the console that SummaryDetail runs twice. I would like to ask what is the reason for this and how to avoid it. I have also uploaded the test code for the entire project in the attachment. Thank you all for helping me answer this.

Please attach the test code file, and I will take a look to provide further assistance.

TestMapster.zip

Program.cs:

TypeAdapterConfig.GlobalSettings.Default.IgnoreNullValues(true);
TypeAdapterConfig.GlobalSettings.Scan(Assembly.Load("TestMapster"));

TestInput.cs:

public class TestInput
{
    public string Summary { get; set; }
}

public class TestView
{
    public string Summary { get; set; }
}

MapperConfig.cs

public class MapperConfig : IRegister
{
    public void Register(TypeAdapterConfig config)
    {
        config.ForType<TestInput, TestView>()
       .Map(dest => dest.Summary, src => SummaryDetail(src.Summary))
       ;
    }

    private static string SummaryDetail(string s)
    {
        Console.WriteLine($"================{s}================");
        return s + " plus";
    }
}

Controller.cs

    [HttpGet]
    public TestView Get()
    {
        var input = new TestInput { Summary = "Hello World" };
        return input.Adapt<TestView>();
    }
@jiuzhou2019
Copy link
Author

The attachment did not upload successfully, I am re-uploading it here.
TestMapster.zip

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

No branches or pull requests

1 participant