Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Mapping property with underscore to record with constructor

Open RobThree opened this issue 2 years ago • 0 comments

First of all: thanks for this great library!

I think I have stumbled upon a bug; if not then I apologize but I don't think this is by design?

using Mapster;

TypeAdapterConfig.GlobalSettings.Default.NameMatchingStrategy(NameMatchingStrategy.Exact);
var e = new MyEntity
{
    Name = "Foo",
    Some_Value = "Bar"
};
var works = e.Adapt<MyDTO>();
var fails = e.Adapt<MyDTOWithConstructor>();

public record MyEntity
{
    public string Name { get; init; } = null!;
    public string Some_Value { get; init; } = null!;
}

public record MyDTO
{
    public string Name { get; init; }
    public string Some_Value { get; init; }
};


public record MyDTOWithConstructor
(
    string Name,
    string Some_Value
);

Mapping MyEntity to MyDTO works fine, as expected. However, when mapping to MyDTOWithConstructor the Some_Value property is not mapped. I have tried some other NameMatchingStrategy values but none seem to help. When I remove the underscore from the entity and DTO accordingly then everything works fine. I could also use explicit mappings. However, I have a LOT of entities and DTO's and I was hoping to keep it as simple as possible. I'm not sure if this behavior is by design but, at least, in my opinion, it doesn't seem very consistent?

Oh, using Mapster 7.2.0.

RobThree avatar Nov 10 '21 11:11 RobThree