RicoSuter / NSwag

The Swagger/OpenAPI toolchain for .NET, ASP.NET Core and TypeScript.
http://NSwag.org
MIT License
6.69k stars 1.24k forks source link

NSwag 14.0.1 still issues warning CS8618 #4704

Closed vvdb-architecture closed 7 months ago

vvdb-architecture commented 8 months ago

When compiling with 14.0.1, the generated C# sdk now has the following warning:

Warning CS8618  Non-nullable field '_baseUrl' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.

That's because even though the constructor assignes BaseUrl:

            BaseUrl = "https://";

... the compiler is unable to detect that the _baseUrl field is in fact initialized through this property.

The way to get rid of the warning is to assign the field directly:

            _baseUrl = "https://";

Also, the warnings reported in issue https://github.com/RicoSuter/NSwag/issues/4467 are still there.

AlbertWeinert-RWEST commented 7 months ago

Yes, the pragma restore of 8616 must happens after the ctor. Not after the _baseUrl field

powercode commented 7 months ago

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.

kvansaders commented 7 months ago

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.

rwb196884 commented 7 months ago

this is completely fucked in 14.0.3

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

You should be able to reference the Nullable package as a workaround until this is fixed.

See https://github.com/manuelroemer/Nullable

adamhathcock commented 7 months ago

14.0.3 results in the same error as @rwb196884 reducing to 14.0.2 fixes

AlbertWeinert-RWEST commented 7 months ago

Yes, the pragma restore of 8616 must happens after the ctor. Not after the _baseUrl field

Or simply do this.

nifklas commented 7 months ago

We're also getting the error again in 14.0.3.

mchilicki commented 5 months ago

I also get this error in 14.0.3, on 14.0.2 it works as expected.

this is completely fucked in 14.0.3

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