MapsterMapper / MapsterMapper/Mapster

Unable to map nullable primitive types (string? to bool?) using custom MapWith delegate

Open
#992 9 comments 0 reactions 0 assignees View on GitHub

A pull request for this has already been merged.

  • #993 by @DocSvartz — merged
  • #999 by @DocSvartz — merged
bug
Dominant language
C#
Stars
5.2k
Forks
410
Avg merge
2d 12h
Merged PRs (30d)
6

Description

What am I missing?

Image

```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

Image

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.