MapsterMapper / MapsterMapper/Mapster
Unable to map nullable primitive types (string? to bool?) using custom MapWith delegate
A pull request for this has already been merged.
- Dominant language
- C#
- Stars
- 5.2k
- Forks
- 410
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 6
Description
What am I missing?
```csharp
using Mapster;
using MapsterMapper;
TypeAdapterConfig config = new TypeAdapterConfig();
config.NewConfig().MapWith(src => Helper.ParseToNullableBool(src));
config.Compile();
Mapper mapper = new Mapper(config);
Source source = new Source { Prop1 = "yes" };
Destination destination = mapper.Map(source);
Console.ReadLine();
static class Helper
{
public static bool? ParseToNullableBool(string? value)
{
if (string.IsNullOrWhiteSpace(value))
return null;
return value.Trim().ToLower() switch
{
"true" or "t" or "yes" or "1" => true,
"false" or "f" or "no" or "0" => false,
_ => null
};
}
}
class Source
{
public string? Prop1 { get; set; }
}
class Destination
{
public bool? Prop1 { get; set; }
}
```
converting non-nullable string to non-nullable bool works fine
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
Reproduce the issue using the nullable string-to-bool MapWith configuration and the Source and Destination classes shown in the report. Start by tracing how MapWith delegates are compiled for nullable primitive properties; done means the custom delegate maps Prop1 correctly, including null and invalid values, while the existing non-nullable case remains working.
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
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 50/100