Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

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

オープン
#1,017 コメント 6 件 リアクション 0 件 担当者 0 名 GitHub で見る

関連するプルリクエストがすでにマージされています。

  • #1018 @DocSvartz による — マージされずにクローズ
  • #1019 @DocSvartz による — マージ済み

評価

難易度
3/5
見積もり時間
1〜2日
初心者へのやさしさ
68/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
csharp
領域
tooling

調査の方向性

PropertySettingBuilder.Map から開始し、Mapster.Tool 10.0.12 で UserCodeGenConfig の例を再現します。mapFunc 引数が生成された UserMapper.AdaptToDto の出力にどのように到達するかを追跡します。完了条件は、FullName が LastName だけではなく FirstName と LastName の両方から生成されることです。

索引モデルが issue の本文から書いたものです。

説明

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

I have the following entity model -

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 -

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 -

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

        config.GenerateMapper("[name]Mapper")
            .ForType<User>();
    }
}

and used the model and extension generating commands with Mapster.Tool. Following are the generated codes -

public partial class UserDto
{
    public int Id { get; set; }
    public string Email { get; set; }
    public string FullName { get; set; }
    public int Age { get; set; }
}
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 -

/// <summary>
/// Map a specific property of the source type to a target property using a custom mapping function.
/// </summary>
/// <typeparam name="TReturn">Type of source property.</typeparam>
/// <typeparam name="TReturn2">Type of target property type.</typeparam>
/// <param name="member">A lambda expression that identifies the source property to be mapped.</param>
/// <param name="mapFunc">A lambda expression that defines the custom mapping function.</param>
/// <param name="targetPropertyName">The name of the target property to which the source property should be mapped.</param>
/// <returns></returns>
public PropertySettingBuilder<T> Map<TReturn, TReturn2>(Expression<Func<T, TReturn>> member, Expression<Func<T, TReturn2>> mapFunc, string? targetPropertyName = null);

So, it seems the tool is not respecting the 2nd parameter (Expression<Func<T, TReturn2>> 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?

主要言語
C#
スター
5.2k
フォーク
411
平均マージ
1日 8時間
マージ済み PR(30日)
4

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

MapsterMapper/Mapster のほかの issue

MapsterMapper/Mapster の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。