Hacktoberfest 2026: los issues que los mantenedores marcaron para octubre, abiertos y aptos para principiantes. Explorar issues de Hacktoberfest

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

Abierto
#1,017 6 comentarios 0 reacciones 0 asignados Ver en GitHub

Ya se ha fusionado un pull request relacionado.

  • #1018 de @DocSvartz — cerrado sin fusionar
  • #1019 de @DocSvartz — fusionado

Evaluación

Dificultad
3/5
Tiempo estimado
1-2 días
Aptitud para principiantes
68/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
csharp
Área
tooling

Línea de trabajo

Empieza en PropertySettingBuilder.Map y reproduce el ejemplo de UserCodeGenConfig con Mapster.Tool 10.0.12. Sigue cómo el argumento mapFunc llega a la salida generada de UserMapper.AdaptToDto; se considera terminado cuando FullName se genera a partir de FirstName y LastName, en lugar de solo LastName.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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?

Lenguaje dominante
C#
Estrellas
5.2k
Forks
409
Merge medio
3 d 4 h
PR fusionados (30 d)
3

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de MapsterMapper/Mapster

Todos los issues de MapsterMapper/Mapster

Issues similares

Más issues de C#

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.