RicoSuter / NSwag

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

Annotate BaseUrl set with MemberNotNull to prevent CS8618. #4739

Closed powercode closed 6 months ago

powercode commented 6 months ago

Fixes #4704 Generate BaseUrl like this

 public string BaseUrl
 {
     get { return _baseUrl; }
     [System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))]
     set
     {
         _baseUrl = value;
         if (!string.IsNullOrEmpty(_baseUrl) && !_baseUrl.EndsWith("/"))
             _baseUrl += '/';
     }
 }

This prevents the warning

 warning CS8618: Non-nullable field '_baseUrl' must contain a non-null value when exiting constructor. Consider declaring the field as nullable.
nakrad commented 5 months ago

This change seems to break .NET Standard 2.0 support.