diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.PathParameter.liquid b/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.PathParameter.liquid index 38b36b5ece..afbecb8a5e 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.PathParameter.liquid +++ b/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.PathParameter.liquid @@ -1,13 +1,13 @@ {% if parameter.IsDateTimeArray -%} -urlBuilder_.Replace("{{ "{" }}{{ parameter.Name }}}", System.Uri.EscapeDataString(string.Join(",", System.Linq.Enumerable.Select({{ parameter.VariableName }}, s_ => s_.ToString("{{ ParameterDateTimeFormat }}", System.Globalization.CultureInfo.InvariantCulture))))); +urlBuilder_.Append(System.Uri.EscapeDataString(string.Join(",", System.Linq.Enumerable.Select({{ parameter.VariableName }}, s_ => s_.ToString("{{ ParameterDateTimeFormat }}", System.Globalization.CultureInfo.InvariantCulture))))); {% elsif parameter.IsDateArray -%} -urlBuilder_.Replace("{{ "{" }}{{ parameter.Name }}}", System.Uri.EscapeDataString(string.Join(",", System.Linq.Enumerable.Select({{ parameter.VariableName }}, s_ => s_.ToString("{{ ParameterDateFormat }}", System.Globalization.CultureInfo.InvariantCulture))))); +urlBuilder_.Append(System.Uri.EscapeDataString(string.Join(",", System.Linq.Enumerable.Select({{ parameter.VariableName }}, s_ => s_.ToString("{{ ParameterDateFormat }}", System.Globalization.CultureInfo.InvariantCulture))))); {% elsif parameter.IsDateTime -%} -urlBuilder_.Replace("{{ "{" }}{{ parameter.Name }}}", System.Uri.EscapeDataString({{ parameter.VariableName }}.ToString("{{ ParameterDateTimeFormat }}", System.Globalization.CultureInfo.InvariantCulture))); +urlBuilder_.Append(System.Uri.EscapeDataString({{ parameter.VariableName }}.ToString("{{ ParameterDateTimeFormat }}", System.Globalization.CultureInfo.InvariantCulture))); {% elsif parameter.IsDate -%} -urlBuilder_.Replace("{{ "{" }}{{ parameter.Name }}}", System.Uri.EscapeDataString({{ parameter.VariableName }}.ToString("{{ ParameterDateFormat }}", System.Globalization.CultureInfo.InvariantCulture))); +urlBuilder_.Append(System.Uri.EscapeDataString({{ parameter.VariableName }}.ToString("{{ ParameterDateFormat }}", System.Globalization.CultureInfo.InvariantCulture))); {% elsif parameter.IsArray -%} -urlBuilder_.Replace("{{ "{" }}{{ parameter.Name }}}", System.Uri.EscapeDataString(string.Join(",", System.Linq.Enumerable.Select({{ parameter.VariableName }}, s_ => ConvertToString(s_, System.Globalization.CultureInfo.InvariantCulture))))); +urlBuilder_.Append(System.Uri.EscapeDataString(string.Join(",", System.Linq.Enumerable.Select({{ parameter.VariableName }}, s_ => ConvertToString(s_, System.Globalization.CultureInfo.InvariantCulture))))); {% else -%} -urlBuilder_.Replace("{{ "{" }}{{ parameter.Name }}}", System.Uri.EscapeDataString(ConvertToString({{ parameter.VariableName }}, System.Globalization.CultureInfo.InvariantCulture))); +urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString({{ parameter.VariableName }}, System.Globalization.CultureInfo.InvariantCulture))); {%- endif %} diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid b/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid index 04bf5ecc8e..1666ea275b 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid +++ b/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid @@ -139,33 +139,6 @@ throw new System.ArgumentNullException("{{ operation.ContentParameter.VariableName }}"); {% endif -%} - var urlBuilder_ = new System.Text.StringBuilder(); - {% if UseBaseUrl %}if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);{% endif %} - urlBuilder_.Append("{{ operation.Path }}{% if operation.HasQueryParameters %}?{% endif %}"); -{% for parameter in operation.PathParameters -%} -{% if parameter.IsOptional -%} - if ({{ parameter.VariableName }} != null) - {% template Client.Class.PathParameter %} - else - urlBuilder_.Replace("/{{ "{" }}{{ parameter.Name }}}", string.Empty); -{% else -%} - {% template Client.Class.PathParameter %} -{% endif -%} -{% endfor -%} -{% for parameter in operation.QueryParameters -%} -{% if parameter.IsOptional -%} - if ({{ parameter.VariableName }} != null) - { - {% template Client.Class.QueryParameter %} - } -{% else -%} - {% template Client.Class.QueryParameter %} -{% endif -%} -{% endfor -%} -{% if operation.HasQueryParameters -%} - urlBuilder_.Length--; -{% endif -%} - {% if InjectHttpClient -%} var client_ = _httpClient; {% elsif UseHttpClientCreationMethod -%} @@ -290,6 +263,56 @@ request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("{{ operation.Produces }}")); {% endif -%} + var urlBuilder_ = new System.Text.StringBuilder(); + {% if UseBaseUrl %}if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl);{% endif %} +{% assign pathParts = operation.Path | split: "/" -%} +{% unless operation.Path == "" -%} +{% for pathPart in pathParts -%} +{% assign pathPartLength = pathPart | size -%} +{% unless pathPartLength == 0 -%} +{% assign firstPathPartChar = pathPart | slice: 0 -%} +{% assign lastPathPartChar = pathPart | slice: -1 -%} +{% if firstPathPartChar == "{" and lastPathPartChar == "}" -%} +{% assign pathParameterPartLength = pathPartLength | minus: 2 -%} +{% assign pathParameterPart = pathPart | slice: 1, pathParameterPartLength -%} +{% for parameter in operation.PathParameters -%} +{% if parameter.Name == pathParameterPart -%} +{% if parameter.IsOptional -%} + if ({{ parameter.VariableName }} != null) + { + {% template Client.Class.PathParameter %} + } + else + if (urlBuilder_.Length > 0) urlBuilder_.Length--; +{% else -%} + {% template Client.Class.PathParameter %} +{% endif -%} +{% endif -%} +{% endfor -%} +{% else -%} + urlBuilder_.Append("{{ pathPart }}"); +{% endif -%} +{% if forloop.last == false -%} + urlBuilder_.Append('/'); +{% endif -%} +{% endunless -%} +{% endfor -%} +{% endunless -%} +{% if operation.HasQueryParameters -%} + urlBuilder_.Append('?'); +{% for parameter in operation.QueryParameters -%} +{% if parameter.IsOptional -%} + if ({{ parameter.VariableName }} != null) + { + {% template Client.Class.QueryParameter %} + } +{% else -%} + {% template Client.Class.QueryParameter %} +{% endif -%} +{% endfor -%} + urlBuilder_.Length--; +{% endif -%} + {% if GeneratePrepareRequestAndProcessResponseAsAsyncMethods %} await PrepareRequestAsync(client_, request_, urlBuilder_, cancellationToken).ConfigureAwait(false); {% else -%} diff --git a/src/NSwag.ConsoleCore.Tests/GenerateSampleSpecificationTests.CheckCSharpClientsAsync_projectName=NSwag.Sample.NET70Minimal_targetFramework=net7.0_generatesCode=True.verified.txt b/src/NSwag.ConsoleCore.Tests/GenerateSampleSpecificationTests.CheckCSharpClientsAsync_projectName=NSwag.Sample.NET70Minimal_targetFramework=net7.0_generatesCode=True.verified.txt index 761bd123ac..881d4e2b51 100644 --- a/src/NSwag.ConsoleCore.Tests/GenerateSampleSpecificationTests.CheckCSharpClientsAsync_projectName=NSwag.Sample.NET70Minimal_targetFramework=net7.0_generatesCode=True.verified.txt +++ b/src/NSwag.ConsoleCore.Tests/GenerateSampleSpecificationTests.CheckCSharpClientsAsync_projectName=NSwag.Sample.NET70Minimal_targetFramework=net7.0_generatesCode=True.verified.txt @@ -68,10 +68,6 @@ namespace MyNamespace /// A server side error occurred. public virtual async System.Threading.Tasks.Task GetAsync(System.Threading.CancellationToken cancellationToken) { - var urlBuilder_ = new System.Text.StringBuilder(); - if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl); - urlBuilder_.Append(""); - var client_ = _httpClient; var disposeClient_ = false; try @@ -81,6 +77,9 @@ namespace MyNamespace request_.Method = new System.Net.Http.HttpMethod("GET"); request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json")); + var urlBuilder_ = new System.Text.StringBuilder(); + if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl); + PrepareRequest(client_, request_, urlBuilder_); var url_ = urlBuilder_.ToString(); @@ -147,12 +146,6 @@ namespace MyNamespace if (b == null) throw new System.ArgumentNullException("b"); - var urlBuilder_ = new System.Text.StringBuilder(); - if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl); - urlBuilder_.Append("sum/{a}/{b}"); - urlBuilder_.Replace("{a}", System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture))); - urlBuilder_.Replace("{b}", System.Uri.EscapeDataString(ConvertToString(b, System.Globalization.CultureInfo.InvariantCulture))); - var client_ = _httpClient; var disposeClient_ = false; try @@ -162,6 +155,14 @@ namespace MyNamespace request_.Method = new System.Net.Http.HttpMethod("GET"); request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json")); + var urlBuilder_ = new System.Text.StringBuilder(); + if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl); + urlBuilder_.Append("sum"); + urlBuilder_.Append('/'); + urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture))); + urlBuilder_.Append('/'); + urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(b, System.Globalization.CultureInfo.InvariantCulture))); + PrepareRequest(client_, request_, urlBuilder_); var url_ = urlBuilder_.ToString(); @@ -364,10 +365,6 @@ namespace MyNamespace /// A server side error occurred. public virtual async System.Threading.Tasks.Task GetAsync(System.Threading.CancellationToken cancellationToken) { - var urlBuilder_ = new System.Text.StringBuilder(); - if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl); - urlBuilder_.Append("examples"); - var client_ = _httpClient; var disposeClient_ = false; try @@ -377,6 +374,10 @@ namespace MyNamespace request_.Method = new System.Net.Http.HttpMethod("GET"); request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/octet-stream")); + var urlBuilder_ = new System.Text.StringBuilder(); + if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl); + urlBuilder_.Append("examples"); + PrepareRequest(client_, request_, urlBuilder_); var url_ = urlBuilder_.ToString(); diff --git a/src/NSwag.Sample.NET70Minimal/GeneratedClientsCs.gen b/src/NSwag.Sample.NET70Minimal/GeneratedClientsCs.gen index 01d43706a4..3f53433a6a 100644 --- a/src/NSwag.Sample.NET70Minimal/GeneratedClientsCs.gen +++ b/src/NSwag.Sample.NET70Minimal/GeneratedClientsCs.gen @@ -68,10 +68,6 @@ namespace MyNamespace /// A server side error occurred. public virtual async System.Threading.Tasks.Task GetAsync(System.Threading.CancellationToken cancellationToken) { - var urlBuilder_ = new System.Text.StringBuilder(); - if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl); - urlBuilder_.Append(""); - var client_ = _httpClient; var disposeClient_ = false; try @@ -81,6 +77,9 @@ namespace MyNamespace request_.Method = new System.Net.Http.HttpMethod("GET"); request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json")); + var urlBuilder_ = new System.Text.StringBuilder(); + if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl); + PrepareRequest(client_, request_, urlBuilder_); var url_ = urlBuilder_.ToString(); @@ -147,12 +146,6 @@ namespace MyNamespace if (b == null) throw new System.ArgumentNullException("b"); - var urlBuilder_ = new System.Text.StringBuilder(); - if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl); - urlBuilder_.Append("sum/{a}/{b}"); - urlBuilder_.Replace("{a}", System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture))); - urlBuilder_.Replace("{b}", System.Uri.EscapeDataString(ConvertToString(b, System.Globalization.CultureInfo.InvariantCulture))); - var client_ = _httpClient; var disposeClient_ = false; try @@ -162,6 +155,14 @@ namespace MyNamespace request_.Method = new System.Net.Http.HttpMethod("GET"); request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/json")); + var urlBuilder_ = new System.Text.StringBuilder(); + if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl); + urlBuilder_.Append("sum"); + urlBuilder_.Append('/'); + urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(a, System.Globalization.CultureInfo.InvariantCulture))); + urlBuilder_.Append('/'); + urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(b, System.Globalization.CultureInfo.InvariantCulture))); + PrepareRequest(client_, request_, urlBuilder_); var url_ = urlBuilder_.ToString(); @@ -364,10 +365,6 @@ namespace MyNamespace /// A server side error occurred. public virtual async System.Threading.Tasks.Task GetAsync(System.Threading.CancellationToken cancellationToken) { - var urlBuilder_ = new System.Text.StringBuilder(); - if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl); - urlBuilder_.Append("examples"); - var client_ = _httpClient; var disposeClient_ = false; try @@ -377,6 +374,10 @@ namespace MyNamespace request_.Method = new System.Net.Http.HttpMethod("GET"); request_.Headers.Accept.Add(System.Net.Http.Headers.MediaTypeWithQualityHeaderValue.Parse("application/octet-stream")); + var urlBuilder_ = new System.Text.StringBuilder(); + if (!string.IsNullOrEmpty(BaseUrl)) urlBuilder_.Append(BaseUrl); + urlBuilder_.Append("examples"); + PrepareRequest(client_, request_, urlBuilder_); var url_ = urlBuilder_.ToString();