Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Generated mappers and required attributes

Open EniacMlezi opened this issue 2 years ago • 12 comments

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

EniacMlezi avatar Feb 27 '23 16:02 EniacMlezi

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

stormaref avatar Mar 04 '23 21:03 stormaref

@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.

andrerav avatar Mar 05 '23 18:03 andrerav

@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

stormaref avatar Mar 06 '23 09:03 stormaref

@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 avatar Mar 06 '23 13:03 EniacMlezi

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

EniacMlezi avatar Mar 06 '23 14:03 EniacMlezi