RicoSuter / NSwag

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

CS0649 due to _baseUrl instead of BaseUrl from 14.0.2 => 14.0.[3-7] #4854

Closed martindamgaardlorensen closed 2 months ago

martindamgaardlorensen commented 2 months ago

I have a working setup with 14.0.2 with the following settings:

"codeGenerators": {
    "openApiToCSharpClient": {
      "clientBaseClass": "ApiClientBase",
      "configurationClass": "IApiClientConfiguration",
      "useBaseUrl": true,
      "generateBaseUrlProperty": false,
      ...
      }
   }

The ApiClientBase class contains a protected string BaseUrl { get; } property which is set in the constructor with a value obtained from the IApiClientConfiguration passed in the constructor.

However, when upgrading to 14.0.3 (or any thing else up to latest 14.0.7), the generated code now contains a private readonly string _baseUrl field (which is used instead of the BaseUrl property in the base class) which is never initialized so I get a CS0649 error when building it - and if I disabled that somehow, the code would still not work as the base url would not be set.

I see no documentation about this breaking change and have been unable to find reasonable way to mitigate this.

I could probably add an abstract method in ApiClientBase that I would invoke from the constructor in the same class (if I disable warnings about virtual methods called from constructor is unsafe) and implement that method to set _baseUrl field from BaseUrl property in a partial hand-coded class alongside the generated code. But that sounds wrong in multiple ways.

What is the intensions with this changed behavior? Or is this a bug?

martindamgaardlorensen commented 2 months ago

Just noticed that this is probably a duplicate of #4764.

olegd-superoffice commented 2 months ago

@martindamgaardlorensen it is a duplicate indeed. Maybe close this and add a comment on that bug instead?

martindamgaardlorensen commented 2 months ago

Duplicate of #4764.