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

Fix missing assignment of base URL #4691

Merged
merged 1 commit into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 40 additions & 19 deletions src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{% if UseBaseUrl and GenerateBaseUrlProperty -%}
#pragma warning disable 8618 // Set by constructor via BaseUrl property
private string _baseUrl;
#pragma restore disable 8618 // Set by constructor via BaseUrl property
#pragma warning restore 8618 // Set by constructor via BaseUrl property
{% endif -%}
{% if InjectHttpClient -%}
private {{ HttpClientType }} _httpClient;
Expand All @@ -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 %}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace MyNamespace
{
#pragma warning disable 8618 // Set by constructor via BaseUrl property
private string _baseUrl;
#pragma restore disable 8618 // Set by constructor via BaseUrl property
#pragma warning restore 8618 // Set by constructor via BaseUrl property
private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

Expand Down Expand Up @@ -498,7 +498,7 @@ namespace MyNamespace
{
#pragma warning disable 8618 // Set by constructor via BaseUrl property
private string _baseUrl;
#pragma restore disable 8618 // Set by constructor via BaseUrl property
#pragma warning restore 8618 // Set by constructor via BaseUrl property
private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

Expand Down
4 changes: 2 additions & 2 deletions src/NSwag.Sample.NET70Minimal/GeneratedClientsCs.gen
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace MyNamespace
{
#pragma warning disable 8618 // Set by constructor via BaseUrl property
private string _baseUrl;
#pragma restore disable 8618 // Set by constructor via BaseUrl property
#pragma warning restore 8618 // Set by constructor via BaseUrl property
private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

Expand Down Expand Up @@ -498,7 +498,7 @@ namespace MyNamespace
{
#pragma warning disable 8618 // Set by constructor via BaseUrl property
private string _baseUrl;
#pragma restore disable 8618 // Set by constructor via BaseUrl property
#pragma warning restore 8618 // Set by constructor via BaseUrl property
private System.Net.Http.HttpClient _httpClient;
private static System.Lazy<Newtonsoft.Json.JsonSerializerSettings> _settings = new System.Lazy<Newtonsoft.Json.JsonSerializerSettings>(CreateSerializerSettings, true);

Expand Down
Loading