MapsterMapper / Mapster

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

Generated mappers and required attributes #554

Open EniacMlezi opened 1 year ago

EniacMlezi commented 1 year ago

When using the c#11.0 required feature, the generated mappers fail. Editinh Sample.CodeGen for example:

public class Person
{
    public required int ID { get; set; }
    public string LastName { get; set; }
    public string FirstMidName { get; set; }
}

And looking at the generated StudentMapper: image

stormaref commented 1 year ago

@andrerav should we remove this part entirely and just generate the part with initialization values? @EniacMlezi can you mention the complete source code, please

andrerav commented 1 year ago

@stormaref That is tempting, but it will change a lot of code for a lot of people. I think a similar solution to the one in #545 is preferable.

stormaref commented 1 year ago

@andrerav that fix won't work with this problem because in that case, the problem was setting the init-only property, and this line was ok:

_UserDto result = p4 ?? new _UserDto();

but in this case, the problem occurs on this line:

Person result = p4 ?? new Person();

and that solution won't fix this problem

EniacMlezi commented 1 year ago

@EniacMlezi can you mention the complete source code, please

The complete source code is the codegen sample in this repository (Sample.CodeGen), but with Person.Id made required and the C# langver set to support required.

EniacMlezi commented 1 year ago

Check this: for code sample: https://github.com/EniacMlezi/Mapster/commit/38401039547e1e5d800b634d92d5765c5eb28ae7