NSwag icon indicating copy to clipboard operation
NSwag copied to clipboard

Csharp Client - breaking change, Base URL now required to have an ending slash

Open dd43054 opened this issue 2 years ago • 2 comments

I recently upgraded to 14.0.3 (I don't remember from which version) and the runtime from Net50 to Net60. After running the generator, I have all tests failing. After reviewing the generated code, I see that a slash would now be required in the configured base URL. I can modify all my config files, but this is a breaking change.

Older nswag generated code:

var urlBuilder_ = new System.Text.StringBuilder();
urlBuilder_.Append(BaseUrl != null ? BaseUrl.TrimEnd('/') : "").Append("/mypath");

Code generated with 14.0.3:

var urlBuilder_ = new System.Text.StringBuilder();
if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl);
// Operation Path: "mypath"
urlBuilder_.Append("mypath");

dd43054 avatar Feb 15 '24 00:02 dd43054

I think the problem is, that the BaseUrl property is not used. The logik to replace the "/" is now in the property but the methods use the _baseUrl and not the property. public string BaseUrl { get { return _baseUrl; } [System.Diagnostics.CodeAnalysis.MemberNotNull(nameof(_baseUrl))] set { _baseUrl = value; if (!string.IsNullOrEmpty(_baseUrl) && !_baseUrl.EndsWith("/")) _baseUrl += '/'; } }

var urlBuilder_ = new System.Text.StringBuilder(); if (!string.IsNullOrEmpty(_baseUrl)) urlBuilder_.Append(_baseUrl); // Operation Path: "api/v1/Controller/Method" urlBuilder_.Append("api/v1/Controller/Method");

bischofd avatar Mar 27 '24 15:03 bischofd