Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Mapping class to record result in empty properties

Open mcamorchia opened this issue 4 years ago • 6 comments

Hi, If you try to map from class object to record type, the result is a new record with empty properties. For example :

source class :

public class DAO
{
    public string name { get; set; }
    public string code { get; set; }
}

destination record :

public record DTO
{
    string name;
    string code;

    public DTO(string _name, string _code)
    {
        name = _name;
        code = _code;
    }
}

mapping :

var dao = new DAO() { name = "test", code = "test" };
var dto = dao.Adapt<DTO>();

dto object contains all properties empty.

mcamorchia avatar May 17 '21 07:05 mcamorchia

When mapping to record, Mapster will map to constructor parameter. Your parameter name has _ prefix. Either config naming convention or rename your parameter.

chaowlert avatar May 17 '21 08:05 chaowlert

When mapping to record, Mapster will map to constructor parameter. Your parameter name has _ prefix. Either config naming convention or rename your parameter.

Hi, Yes, you're right. looking better, i found the condition

this classes mapped correctly :

public class DAO
{
    public string? Name { get; set; }

    public string? Code{ get; set; }
}

public record DTO
(
    string? Name,
    string? Code

);

If I change the name of the property 'Code' in 'CodeEU', this property isn't mapped.

public class DAO
{
    public string? Name { get; set; }

    public string? CodeEU{ get; set; }
}

public record DTO
(
    string? Name,
    string? CodeEU

);

mcamorchia avatar May 17 '21 13:05 mcamorchia

Oh, this could be bug

chaowlert avatar May 17 '21 13:05 chaowlert

I believe I'm seeing the same issue here.

Mapping to this class works:

public class Target
{
  public string Id { get; set; }
  public string NAME { get; set; }
}

But if I try to shorten the code and map to a C# 9.0 record, it doesn't work anymore:

public class record Target(string Id, string NAME);

When I map to the record, Id is correctly mapped, but NAME is ignored. It stays null and is completely absent from the ProjectTo expression.

jods4 avatar Jul 29 '21 14:07 jods4

I'm also seeing the same issue.

Maybe the consecutive uppercase letters caused this bug.

When I map to the record like this: public record DTO(string PlatformName, string URL); PlatformName is correctly mapped while URL stays null.

But this record can be mapped correctly: public record DTO(string PlatformName, string Url);

hechengyuhui avatar Oct 04 '21 12:10 hechengyuhui

Possibly related to #388.

andrerav avatar Feb 19 '22 13:02 andrerav

I faced the same issue: this record public record CreateItemCommand ( int userId, int sourceId, string note ) BUT When I' capitalized all properties it's working perfectly public record CreateItemCommand ( int UserId, int SourceId, string Note )

wahidrezgui avatar Mar 21 '23 07:03 wahidrezgui

Fixed in #590

andrerav avatar May 29 '23 21:05 andrerav