MapsterMapper / MapsterMapper/Mapster
Mapping fails when source type is nullable and source selector contains ternary conditional since 10.0.7
A pull request for this has already been merged.
- #986 by @DocSvartz — merged
- Dominant language
- C#
- Stars
- 5.2k
- Forks
- 410
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
The following sample unit test passes in v10.0.6, but fails in 10.0.7 where `result1` is 0.
```c#
public class MapsterSample
{
[Fact]
public void MapsterSampleTest()
{
var config = TypeAdapterConfig.GlobalSettings.Fork(config =>
{
config.ForType()
.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(config);
var result2 = source2.Adapt(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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the inline MapsterSampleTest with the provided nullable SourceClass properties and ternary source selector on 10.0.7, then compare behavior with 10.0.6. Trace the mapping path used by .Map and verify that both assertions return 100 and 30 without changing the reported scenario.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 35/100