Skip to content

Commit

Permalink
Replace StringBuilder.Replace with StringBuilder.Append for building …
Browse files Browse the repository at this point in the history
…request URL. (RicoSuter#4579)
  • Loading branch information
paulomorgado authored and lahma committed Jan 20, 2024
1 parent fc58ba7 commit 66374a0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 61 deletions.
Original file line number Diff line number Diff line change
@@ -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 %}
77 changes: 50 additions & 27 deletions src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -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 -%}
Expand Down Expand Up @@ -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 -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ namespace MyNamespace
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task<string> 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
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -364,10 +365,6 @@ namespace MyNamespace
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task<FileResponse> 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
Expand All @@ -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();
Expand Down
29 changes: 15 additions & 14 deletions src/NSwag.Sample.NET70Minimal/GeneratedClientsCs.gen
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,6 @@ namespace MyNamespace
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task<string> 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
Expand All @@ -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();
Expand Down Expand Up @@ -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
Expand All @@ -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();
Expand Down Expand Up @@ -364,10 +365,6 @@ namespace MyNamespace
/// <exception cref="ApiException">A server side error occurred.</exception>
public virtual async System.Threading.Tasks.Task<FileResponse> 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
Expand All @@ -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();
Expand Down

0 comments on commit 66374a0

Please sign in to comment.