MapsterMapper / MapsterMapper/Mapster

Mapster.Tool - PropertySettingBuilder.Map(member, mapFunc, targetPropertyName) method is not working as expected!

Open
#1,017 6 comments 0 reactions 0 assignees View on GitHub

A pull request for this has already been merged.

  • #1018 by @DocSvartz — closed without merging
  • #1019 by @DocSvartz — merged
Dominant language
C#
Stars
5.2k
Forks
410
Avg merge
2d 12h
Merged PRs (30d)
6

Description

I'm using -
Mapster 10.0.12
Mapster.Tool 10.0.12
.NET 10.0.400

I have the following entity model -

```C#
public class User
{
public int Id { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
```
and I wanted the following DTO model -

```C#
public partial class UserDto
{
public int Id { get; set; }
public string Email { get; set; }
public string FullName { get; set; }
public int Age { get; set; }
}
```
and a mapper extension method to be generated which would map `User.FirstName + " " + User.LastName` to `UserDto.FullName`.

I used the following code -

```C#
public class UserCodeGenConfig : ICodeGenerationRegister
{
public void Register(CodeGenerationConfig config)
{
config.AdaptTo("[name]Dto", MapType.Map)
.ForType(p =>
{
p.Ignore(s => s.FirstName);
p.Map(s => s.LastName, s => $"{s.FirstName} {s.LastName}", "FullName");
});

config.GenerateMapper("[name]Mapper")
.ForType();
}
}
```
and used the model and extension generating commands with Mapster.Tool. Following are the generated codes -

```C#
public partial class UserDto
{
public int Id { get; set; }
public string Email { get; set; }
public string FullName { get; set; }
public int Age { get; set; }
}
```
```C#
public static partial class UserMapper
{
public static UserDto AdaptToDto(this User p1)
{
return p1 == null ? null : new UserDto()
{
Id = p1.Id,
Email = p1.Email,
FullName = p1.LastName,
Age = p1.Age
};
}
}
```
As you can see, the DTO model is generated as expected, but the mapping code is exactly what it would generate if I had used -

`p.Map(s => s.LastName, "FullName");`

instead of -

`p.Map(s => s.LastName, s => $"{s.FirstName} {s.LastName}", "FullName");`

Following is the source code documentation for the `Map` method in question -

```C#
///
/// Map a specific property of the source type to a target property using a custom mapping function.
///
/// Type of source property.
/// Type of target property type.
/// A lambda expression that identifies the source property to be mapped.
/// A lambda expression that defines the custom mapping function.
/// The name of the target property to which the source property should be mapped.
///
public PropertySettingBuilder Map(Expression> member, Expression> mapFunc, string? targetPropertyName = null);
```

So, it seems the tool is not respecting the 2nd parameter (`Expression> mapFunc`) of this `Map` method.

Is there anything I failed to understand or is this a bug? Is there any other approach to achieve the expected result?

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

Start at PropertySettingBuilder.Map and reproduce the UserCodeGenConfig example with Mapster.Tool 10.0.12. Trace how the mapFunc argument reaches the generated UserMapper.AdaptToDto output; done means FullName is generated from both FirstName and LastName rather than only LastName.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
68/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.