Skip to content
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
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 @@ -69,10 +69,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 @@ -82,6 +78,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 @@ -148,12 +147,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 @@ -163,6 +156,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 @@ -366,10 +367,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 @@ -379,6 +376,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 @@ -69,10 +69,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 @@ -82,6 +78,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 @@ -148,12 +147,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 @@ -163,6 +156,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 @@ -366,10 +367,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 @@ -379,6 +376,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