Skip to content

Mapping fails when source type is nullable and source selector contains ternary conditional since 10.0.7 #987

@joeclarkpodium

Description

@joeclarkpodium

The following sample unit test passes in v10.0.6, but fails in 10.0.7 where result1 is 0.

public class MapsterSample
{
    [Fact]
    public void MapsterSampleTest()
    {
        var config = TypeAdapterConfig.GlobalSettings.Fork(config =>
        {
            config.ForType<SourceClass, DestinationClass>()
                .Map(dest => dest.Value, src => src.UseSecondaryValue
                    ? src.SecondaryValue1 + src.SecondaryValue2
                    : src.PrimaryValue);
        });

        var source1 = new SourceClass
        {
            UseSecondaryValue = false,
            PrimaryValue = 100
        };
        var source2 = new SourceClass
        {
            UseSecondaryValue = true,
            SecondaryValue1 = 10,
            SecondaryValue2 = 20
        };
        
        var result1 = source1.Adapt<DestinationClass>(config);
        var result2 = source2.Adapt<DestinationClass>(config);
        
        Assert.Multiple(
            () => Assert.Equal(100, result1.Value),
            () => Assert.Equal(30, result2.Value)
        );
    }
}

public class SourceClass
{
    public bool UseSecondaryValue { get; set; }
    public int? PrimaryValue { get; set; }
    public int? SecondaryValue1 { get; set; }
    public int? SecondaryValue2 { get; set; }
}

public class DestinationClass
{
    public int Value { get; set; }
}

This seems to be something related to using a ternary conditional in the source selector in .Map and the properties on SourceClass being nullable.

Appreciate this is a strange example and that there are definitely other ways it can be achieved, but it definitely used to work and hopefully highlights the issue.

Using .NET 10.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions