Mapster icon indicating copy to clipboard operation
Mapster copied to clipboard

Include #nullable enable to generated files

Open scottcollins opened this issue 4 years ago • 9 comments

I have some models being generated in a project that uses nullable reference types

The types generate fine, but I am getting CS8669 warnings because the generated file does not have #nullable enable included

Ex: public string? City { get; set; } gets generated but is marked as a warning

Adding this directive to the generated file would eliminate these warnings

scottcollins avatar May 14 '21 19:05 scottcollins

Attempt to avoid this warning by changing the type from "nullable"(string?) to "non nullable"(string) also has not effect. Looks like Mapster does not see the difference between string? and string.

About changing a type: https://github.com/MapsterMapper/Mapster/wiki/Fluent-API-Code-generation#change-a-property-name-type

SML-DmitrySokolov avatar May 20 '21 10:05 SML-DmitrySokolov

@scottcollins Is it possible for you to solve this by adding <Nullable>enable</Nullable> in the csproj that contains the generated files?

andrerav avatar Feb 19 '22 13:02 andrerav

The warning says PersonDto.g.cs(12, 22): [CS8669] The annotation for nullable reference types should only be used in code within a '#nullable' annotations context. Auto-generated code requires an explicit '#nullable' directive in source.

Apparently it still needs #nullable enable in the file even with <Nullable>enable</Nullable> in the project.

cyril265 avatar Mar 05 '22 20:03 cyril265

Having the same problem. Any update on this? It's a blocker for us

Elgirhath avatar Apr 26 '22 21:04 Elgirhath

As noted in Microsoft's documentation here in an Important section - generated files need to include explicit #nullable enable line.

Elgirhath avatar Apr 26 '22 21:04 Elgirhath

I have experienced the same. Some more info can be found here: https://stackoverflow.com/a/59460631

As @cyril265 says, <Nullable>enable</Nullable> in csproj is ignored - Roslyn requires an explicit nullable directive in generated code files (*.g.cs).

inmhoel avatar Jun 16 '22 06:06 inmhoel

+1 Getting this warning. And with <WarningAsErrors>nullable</WarningAsErrors>, the tool breaks the build.

kcadduk avatar Jul 06 '22 14:07 kcadduk

I would like for this issue to get some attention too, as it's a pain to have to manually add it back to each generated DTO as soon as I rebuild.

oscarjaergren avatar Sep 01 '22 20:09 oscarjaergren

@andrerav I'm just going to mention you here, not sure if you are subscribed to this issue.

cyril265 avatar Sep 16 '22 10:09 cyril265