Skip to content

Mapping class to record result in empty properties #338

Closed

Description

@mcamorchia

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.

Activity

  1. chaowlert commented on May 17, 2021

    @chaowlert
    Collaborator

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

  2. mcamorchia commented on May 17, 2021

    @mcamorchia
    Author

    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
    
    );
    
  3. chaowlert commented on May 17, 2021

    @chaowlert
    Collaborator

    Oh, this could be bug

  4. jods4 commented on Jul 29, 2021

    @jods4

    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.

  5. hechengyuhui commented on Oct 4, 2021

    @hechengyuhui

    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);

  6. self-assigned this
    on Feb 19, 2022
  7. andrerav commented on Feb 19, 2022

    @andrerav
    Member

    Possibly related to #388.

  8. wahidrezgui commented on Mar 21, 2023

    @wahidrezgui

    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
    )

  9. andrerav commented on May 29, 2023

    @andrerav
    Member

    Fixed in #590

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions