Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NSwag 14.0.1 still issues warning CS8618 #4704

Closed
vvdb-architecture opened this issue Jan 12, 2024 · 9 comments · Fixed by #4739
Closed

NSwag 14.0.1 still issues warning CS8618 #4704

vvdb-architecture opened this issue Jan 12, 2024 · 9 comments · Fixed by #4739

Comments

@vvdb-architecture
Copy link

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 #4467 are still there.

@AlbertWeinert-RWEST
Copy link

AlbertWeinert-RWEST commented Jan 17, 2024

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

@powercode
Copy link
Contributor

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
Copy link

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
Copy link

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
Copy link
Contributor

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

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

@adamhathcock
Copy link

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

@AlbertWeinert-RWEST
Copy link

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

Or simply do this.

@nifklas
Copy link

nifklas commented Feb 14, 2024

We're also getting the error again in 14.0.3.

@mchilicki
Copy link

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?)	

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants