MapsterMapper / Mapster

A fast, fun and stimulating object to object Mapper
MIT License
4.31k stars 328 forks source link

ID property is ignored, but Id is mapped #494

Closed ThaStealth closed 1 year ago

ThaStealth commented 1 year ago

This one caused me a few headaches to figure out:

Source type

public class ExampleInput
{
    public long ID { get; set; }
    public string Name { get; set; }
    public int Order { get; set; }
}

Dest type

public record ExampleOutput
(
    long ID,
    string Name,
    int Order
);

Mapping code:

ExampleInput input = new ExampleInput() { ID = 5, Name = "Sample", Order = 1 };
string output = input.BuildAdapter() .CreateMapExpression<ExampleOutput>().ToScript();

(I didn't configure any other settings or created mappingconfigs)

Results in the following expression:

public ExampleOutput Main(ExampleInput p1)
{
    return p1 == null ? null : new ExampleOutput(0l, p1.Name, p1.Order);
}

But when changing both ID fields in the input/output types to Id we get the following result:

public ExampleOutput Main(ExampleInput p1)
{
    return p1 == null ? null : new ExampleOutput(p1.Id, p1.Name, p1.Order);
}

Not sure why this is happening, I couldn't find anything in the documentation that ID is a reserved keyword which states the field should be ignored.

andrerav commented 1 year ago

Duplicate of #388. Really need to get that fixed soon! :)

andrerav commented 1 year ago

Fixed in #590