From dfec16194a988890e4d528593dcdbfb3456b8de1 Mon Sep 17 00:00:00 2001 From: Paulo Morgado <470455+paulomorgado@users.noreply.github.com> Date: Sun, 7 Jan 2024 15:04:09 +0000 Subject: [PATCH] Fix missing assignment of base URL Fixes #4637 --- .../Templates/Client.Class.liquid | 57 +++++++++++++------ 1 file changed, 39 insertions(+), 18 deletions(-) diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid b/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid index abd40c0bcd..72e793c7a2 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid +++ b/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid @@ -17,30 +17,51 @@ private static System.Lazy<{{ JsonSerializerSettingsType }}> _settings = new System.Lazy<{{ JsonSerializerSettingsType }}>(CreateSerializerSettings, true); {% endif -%} +{% assign constructorParameters = "" -%} {% if HasConfigurationClass -%} - public {{ Class }}({{ ConfigurationClass }} configuration{% if InjectHttpClient %}, {{ HttpClientType }} httpClient{% endif %}) : base(configuration) - { -{% if InjectHttpClient -%} - _httpClient = httpClient; -{% endif -%} -{% elsif UseBaseUrl and HasBaseUrl == false %} - public {{ Class }}(string baseUrl{% if InjectHttpClient %}, {{ HttpClientType }} httpClient{% endif %}) +{% assign constructorParameters = ConfigurationClass | append: " configuration" -%} +{% endif -%} +{% if UseBaseUrl and HasBaseUrl == false and HasConfigurationClass == false -%} +{% unless constructorParameters == "" -%}{% assign constructorParameters = constructorParameters | append: ", " -%}{% endunless -%} +{% assign constructorParameters = constructorParameters | append: "string baseUrl" -%} +{% endif -%} +{% if InjectHttpClient -%} +{% unless constructorParameters == "" -%}{% assign constructorParameters = constructorParameters | append: ", " -%}{% endunless -%} +{% assign constructorParameters = constructorParameters | append: HttpClientType | append: " httpClient" -%} +{% endif -%} + public {{ Class }}({{ constructorParameters }}){% if HasConfigurationClass and HasBaseClass -%}{{ " : base(configuration)"}}{% endif %} { +{% if UseBaseUrl -%} +{% if HasBaseUrl -%} +{% if GenerateBaseUrlProperty -%} + BaseUrl = "{{ BaseUrl }}"; +{% else -%} +{% if BaseUrl != "" -%} +{% assign baseUrlLength = BaseUrl | size -%} +{% assign lastUrlCharIndex = baseUrlLength | minus: 1 -%} +{% assign lastUrlChar = BaseUrl | slice: lastUrlCharIndex, 1 -%} +{% if lastUrlChar == "/" -%} + _baseUrl = "{{ BaseUrl }}"; +{% else -%} + _baseUrl = "{{ BaseUrl }}/"; +{% endif -%} +{% endif -%} +{% endif -%} +{% else -%} +{% if GenerateBaseUrlProperty -%} BaseUrl = baseUrl; -{% if InjectHttpClient -%} - _httpClient = httpClient; +{% else -%} +{% if HasConfigurationClass == false -%} + _baseUrl = (string.IsNullOrEmpty(baseUrl) || baseUrl.EndsWith("/")) + ? baseUrl + : baseUrl + "/"; +{% endif -%} +{% endif -%} +{% endif -%} {% endif -%} -{% elsif InjectHttpClient %} - public {{ Class }}({{ HttpClientType }} httpClient) - { +{% if InjectHttpClient -%} _httpClient = httpClient; -{% else -%} - public {{ Class }}() - { -{% if UseBaseUrl and GenerateBaseUrlProperty -%} - BaseUrl = "{{ BaseUrl }}"; {% endif -%} -{% endif -%} {% template Client.Class.Constructor %} }