NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

Warning fix using MemberNullAttribute causes accessibility level error.

Open kvansaders opened this issue 1 year ago • 5 comments

          > I think a better solution is to generate
 public string BaseUrl
 {
     get { return _baseUrl; }
     [System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))]
     set
     {
         _baseUrl = value;
         if (!string.IsNullOrEmpty(_baseUrl) && !_baseUrl.EndsWith("/"))
             _baseUrl += '/';
     }
 }

To let the compiler null analysis know that the property setter sets the field _baseUrl.

In my netstandard2.0 project, this gives: 'MemberNotNullAttribute' is inaccessible due to its protection level.

Originally posted by @kvansaders in https://github.com/RicoSuter/NSwag/issues/4704#issuecomment-1927882073

kvansaders avatar Feb 05 '24 19:02 kvansaders

I'm seeing the same issue as the other commenter on that issue, where the build completely breaks with the following error message:

Error	CS0234	The type or namespace name 'MemberNotNullAttribute' does not exist in the namespace 'System.Diagnostics.CodeAnalysis' (are you missing an assembly reference?)	

as it is, 14.0.3 breaks netstandard compat from what I can tell

Vogel612 avatar Feb 06 '24 12:02 Vogel612

I'm seeing the same issue as well, trying to use the generated C# client in a net472 project. Downgrading to 14.0.2 removed the attribute from the generated class. That works as a workaround for me at the moment.

Wholteza avatar Feb 06 '24 15:02 Wholteza

The same issue on my side, MemberNotNull shouldn't be emitted when targeting netstandard2.0

ImoutoChan avatar Feb 06 '24 16:02 ImoutoChan

The same issue for targeting WinX86 runtime in a .Net framework 4.5.2 project

nessuarez avatar Feb 09 '24 05:02 nessuarez

The same issue on my side as well. Caused by NSwag toolchain v14.0.3.0.

jstun avatar Feb 12 '24 13:02 jstun

The same issue with NSwag v14.0.3.0 for a .Net 4.8 project.

melucas avatar Feb 20 '24 16:02 melucas

The feature System.Diagnostics.CodeAnalysis.MemberNotNull introduced with .NET 5 as MS documentation declares at https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.codeanalysis.membernotnullattribute?view=net-8.0

So, the code line

            [System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))]

should be as follows to solve the compilation error

#if NET5_0_OR_GREATER
            [System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))]
#endif

jochenwezel avatar Mar 19 '24 16:03 jochenwezel

I need to multi-target net40 and this makes 14 unusable, have to use 13.

rwb196884 avatar Mar 20 '24 16:03 rwb196884

If you render the code as provided into the generated document, you don't need to multi-target your solution itself. Just the generated output has to provide the 2 additional lines for compiler directives

jochenwezel avatar Mar 20 '24 16:03 jochenwezel

Forward duplicated by #4833, fixed since 14.0.6

Vogel612 avatar Mar 27 '24 08:03 Vogel612