Skip to content

Commit

Permalink
Remove path part concatenation for paths witout parametrs and removed…
Browse files Browse the repository at this point in the history
… append and concatenation of single characteres
  • Loading branch information
paulomorgado committed Nov 16, 2023
1 parent 5785968 commit aff9300
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public async Task When_parameter_is_array_then_CSharp_is_correct()
var code = generator.GenerateFile();

// Assert
Assert.Contains(@"foreach (var item_ in elementId) { urlBuilder_.Append(System.Uri.EscapeDataString(""elementId"") + ""="").Append(System.Uri.EscapeDataString(ConvertToString(item_, System.Globalization.CultureInfo.InvariantCulture))).Append(""&""); }", code);
Assert.Contains(@"foreach (var item_ in elementId) { urlBuilder_.Append(System.Uri.EscapeDataString(""elementId"")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(item_, System.Globalization.CultureInfo.InvariantCulture))).Append('&'); }", code);
}

[Fact]
Expand Down
2 changes: 1 addition & 1 deletion src/NSwag.CodeGeneration.CSharp.Tests/ParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public void Deep_object_properties_are_correctly_named()
var code = generator.GenerateFile();

// Assert
Assert.Contains(@"""options[optionalOrder.id]"") + ""=""", code);
Assert.Contains(@"""options[optionalOrder.id]"")).Append('=')", code);
Assert.Contains("options.OptionalOrderId", code);
}

Expand Down
18 changes: 9 additions & 9 deletions src/NSwag.CodeGeneration.CSharp.Tests/QueryParameterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,10 @@ public void When_query_parameter_is_set_to_explode_and_style_is_form_object_para

// Assert
Assert.Contains(
"urlBuilder_.Append(System.Uri.EscapeDataString(\"page\") + \"=\").Append(System.Uri.EscapeDataString(ConvertToString(paging.Page, System.Globalization.CultureInfo.InvariantCulture))).Append(\"&\");",
"urlBuilder_.Append(System.Uri.EscapeDataString(\"page\")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(paging.Page, System.Globalization.CultureInfo.InvariantCulture))).Append('&');",
code);
Assert.Contains(
"urlBuilder_.Append(System.Uri.EscapeDataString(\"limit\") + \"=\").Append(System.Uri.EscapeDataString(ConvertToString(paging.Limit, System.Globalization.CultureInfo.InvariantCulture))).Append(\"&\");",
"urlBuilder_.Append(System.Uri.EscapeDataString(\"limit\")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(paging.Limit, System.Globalization.CultureInfo.InvariantCulture))).Append('&');",
code);
}

Expand Down Expand Up @@ -138,10 +138,10 @@ public void When_query_parameter_is_untyped_free_form_object_parameters_are_expa

// Assert
Assert.DoesNotContain(
"urlBuilder_.Append(System.Uri.EscapeDataString(\"extendedProperties\") + \"=\").Append(System.Uri.EscapeDataString(ConvertToString(extendedProperties, System.Globalization.CultureInfo.InvariantCulture))).Append(\"&\");",
"urlBuilder_.Append(System.Uri.EscapeDataString(\"extendedProperties\")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(extendedProperties, System.Globalization.CultureInfo.InvariantCulture))).Append('&');",
code);
Assert.Contains(
"foreach (var item_ in extendedProperties.AdditionalProperties) { urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key) + \"=\").Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append(\"&\"); }",
"foreach (var item_ in extendedProperties.AdditionalProperties) { urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key)).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append('&'); }",
code);
}

Expand Down Expand Up @@ -206,10 +206,10 @@ public void When_query_parameter_is_typed_free_form_object_parameters_are_expand

// Assert
Assert.DoesNotContain(
"urlBuilder_.Append(System.Uri.EscapeDataString(\"extendedProperties\") + \"=\").Append(System.Uri.EscapeDataString(ConvertToString(extendedProperties, System.Globalization.CultureInfo.InvariantCulture))).Append(\"&\");",
"urlBuilder_.Append(System.Uri.EscapeDataString(\"extendedProperties\")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(extendedProperties, System.Globalization.CultureInfo.InvariantCulture))).Append('&');",
code);
Assert.Contains(
"foreach (var item_ in extendedProperties) { urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key) + \"=\").Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append(\"&\"); }",
"foreach (var item_ in extendedProperties) { urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key)).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append('&'); }",
code);
}

Expand Down Expand Up @@ -289,13 +289,13 @@ public void When_query_parameter_is_mixed_free_form_object_parameters_are_expand

// Assert
Assert.DoesNotContain(
"urlBuilder_.Append(System.Uri.EscapeDataString(\"extendedProperties\") + \"=\").Append(System.Uri.EscapeDataString(ConvertToString(extendedProperties, System.Globalization.CultureInfo.InvariantCulture))).Append(\"&\");",
"urlBuilder_.Append(System.Uri.EscapeDataString(\"extendedProperties\")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(extendedProperties, System.Globalization.CultureInfo.InvariantCulture))).Append('&');",
code);
Assert.Contains(
"urlBuilder_.Append(System.Uri.EscapeDataString(\"default\") + \"=\").Append(System.Uri.EscapeDataString(ConvertToString(extendedProperties.Default, System.Globalization.CultureInfo.InvariantCulture))).Append(\"&\");",
"urlBuilder_.Append(System.Uri.EscapeDataString(\"default\")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(extendedProperties.Default, System.Globalization.CultureInfo.InvariantCulture))).Append('&');",
code);
Assert.Contains(
"foreach (var item_ in extendedProperties.AdditionalProperties) { urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key) + \"=\").Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append(\"&\"); }",
"foreach (var item_ in extendedProperties.AdditionalProperties) { urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key)).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append('&'); }",
code);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
{% if parameter.IsDateTimeArray -%}
foreach (var item_ in {{ parameter.VariableName }}) { urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}") + "=").Append(System.Uri.EscapeDataString(item_.ToString("{{ ParameterDateTimeFormat }}", System.Globalization.CultureInfo.InvariantCulture))).Append("&"); }
foreach (var item_ in {{ parameter.VariableName }}) { urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}")).Append('=').Append(System.Uri.EscapeDataString(item_.ToString("{{ ParameterDateTimeFormat }}", System.Globalization.CultureInfo.InvariantCulture))).Append('&'); }
{% elsif parameter.IsDateArray -%}
foreach (var item_ in {{ parameter.VariableName }}) { urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}") + "=").Append(System.Uri.EscapeDataString(item_.ToString("{{ ParameterDateFormat }}", System.Globalization.CultureInfo.InvariantCulture))).Append("&"); }
foreach (var item_ in {{ parameter.VariableName }}) { urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}")).Append('=').Append(System.Uri.EscapeDataString(item_.ToString("{{ ParameterDateFormat }}", System.Globalization.CultureInfo.InvariantCulture))).Append('&'); }
{% elsif parameter.IsDateTime -%}
urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}") + "=").Append(System.Uri.EscapeDataString({% if parameter.IsNullable and parameter.IsRequired %}{{ parameter.VariableName }} != null ? {% endif %}{{ parameter.VariableName }}{% if parameter.IsSystemNullable %}.Value{% endif %}.ToString("{{ ParameterDateTimeFormat }}", System.Globalization.CultureInfo.InvariantCulture){% if parameter.IsNullable and parameter.IsRequired %} : "{{ QueryNullValue }}"{% endif %})).Append("&");
urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}")).Append('=').Append(System.Uri.EscapeDataString({% if parameter.IsNullable and parameter.IsRequired %}{{ parameter.VariableName }} != null ? {% endif %}{{ parameter.VariableName }}{% if parameter.IsSystemNullable %}.Value{% endif %}.ToString("{{ ParameterDateTimeFormat }}", System.Globalization.CultureInfo.InvariantCulture){% if parameter.IsNullable and parameter.IsRequired %} : "{{ QueryNullValue }}"{% endif %})).Append('&');
{% elsif parameter.IsDate -%}
urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}") + "=").Append(System.Uri.EscapeDataString({% if parameter.IsNullable and parameter.IsRequired %}{{ parameter.VariableName }} != null ? {% endif %}{{ parameter.VariableName }}{% if parameter.IsSystemNullable %}.Value{% endif %}.ToString("{{ ParameterDateFormat }}", System.Globalization.CultureInfo.InvariantCulture){% if parameter.IsNullable and parameter.IsRequired %} : "{{ QueryNullValue }}"{% endif %})).Append("&");
urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}")).Append('=').Append(System.Uri.EscapeDataString({% if parameter.IsNullable and parameter.IsRequired %}{{ parameter.VariableName }} != null ? {% endif %}{{ parameter.VariableName }}{% if parameter.IsSystemNullable %}.Value{% endif %}.ToString("{{ ParameterDateFormat }}", System.Globalization.CultureInfo.InvariantCulture){% if parameter.IsNullable and parameter.IsRequired %} : "{{ QueryNullValue }}"{% endif %})).Append('&');
{% elsif parameter.IsArray -%}
foreach (var item_ in {{ parameter.VariableName }}) { urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}") + "=").Append(System.Uri.EscapeDataString(ConvertToString(item_, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); }
foreach (var item_ in {{ parameter.VariableName }}) { urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(item_, System.Globalization.CultureInfo.InvariantCulture))).Append('&'); }
{% elsif parameter.IsDictionary -%}
foreach (var item_ in {{ parameter.VariableName }}) { urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key) + "=").Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); }
foreach (var item_ in {{ parameter.VariableName }}) { urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key)).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append('&'); }
{% elsif parameter.IsDeepObject -%}
{% for property in parameter.PropertyNames -%}
if ({{parameter.Name}}.{{property.Name}} != null)
{
urlBuilder_.Append(System.Uri.EscapeDataString("{{parameter.Name}}[{{property.Key}}]") + "=").Append(System.Uri.EscapeDataString(ConvertToString({{parameter.Name}}.{{property.Name}}, System.Globalization.CultureInfo.InvariantCulture))).Append("&");
urlBuilder_.Append(System.Uri.EscapeDataString("{{parameter.Name}}[{{property.Key}}]")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString({{parameter.Name}}.{{property.Name}}, System.Globalization.CultureInfo.InvariantCulture))).Append('&');
}
{% endfor -%}
{% for property in parameter.CollectionPropertyNames -%}
if ({{parameter.Name}}.{{property.Name}} != null && {{parameter.Name}}.{{property.Name}}.Count > 0)
{
urlBuilder_.Append(System.Uri.EscapeDataString("{{parameter.Name}}[{{property.Key}}]") + "=");
urlBuilder_.Append(System.Uri.EscapeDataString("{{parameter.Name}}[{{property.Key}}]")).Append('=');
foreach (var p_ in {{parameter.Name}}.{{property.Name}})
{
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(p_, System.Globalization.CultureInfo.InvariantCulture))).Append(",");
urlBuilder_.Append(System.Uri.EscapeDataString(ConvertToString(p_, System.Globalization.CultureInfo.InvariantCulture))).Append(',');
}
urlBuilder_.Length--;
urlBuilder_.Append("&");
urlBuilder_.Append('&');
}
{% endfor -%}
{% elsif parameter.Explode and parameter.IsForm and parameter.IsObject -%}
{% for property in parameter.PropertyNames -%}
if ({{parameter.Name}}.{{property.Name}} != null)
{
urlBuilder_.Append(System.Uri.EscapeDataString("{{property.Key}}") + "=").Append(System.Uri.EscapeDataString(ConvertToString({{parameter.Name}}.{{property.Name}}, System.Globalization.CultureInfo.InvariantCulture))).Append("&");
urlBuilder_.Append(System.Uri.EscapeDataString("{{property.Key}}")).Append('=').Append(System.Uri.EscapeDataString(ConvertToString({{parameter.Name}}.{{property.Name}}, System.Globalization.CultureInfo.InvariantCulture))).Append('&');
}
{% endfor -%}
{% elsif parameter.HasAdditionalProperties == false -%}
urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}") + "=").Append(System.Uri.EscapeDataString({% if parameter.IsNullable and parameter.IsRequired %}{{ parameter.VariableName }} != null ? {% endif %}ConvertToString({{ parameter.VariableName }}, System.Globalization.CultureInfo.InvariantCulture){% if parameter.IsNullable and parameter.IsRequired %} : "{{ QueryNullValue }}"{% endif %})).Append("&");
urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}")).Append('=').Append(System.Uri.EscapeDataString({% if parameter.IsNullable and parameter.IsRequired %}{{ parameter.VariableName }} != null ? {% endif %}ConvertToString({{ parameter.VariableName }}, System.Globalization.CultureInfo.InvariantCulture){% if parameter.IsNullable and parameter.IsRequired %} : "{{ QueryNullValue }}"{% endif %})).Append('&');
{% endif -%}
{% if parameter.HasAdditionalProperties -%}
foreach (var item_ in {{parameter.Name}}.AdditionalProperties) { urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key) + "=").Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append("&"); }
foreach (var item_ in {{parameter.Name}}.AdditionalProperties) { urlBuilder_.Append(System.Uri.EscapeDataString(item_.Key)).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append('&'); }
{% endif -%}
52 changes: 29 additions & 23 deletions src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -265,39 +265,45 @@

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 operation.Path contains "{" -%}
{% 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 -%}
{% else -%}
{% template Client.Class.PathParameter %}
{% endif -%}
{% endif -%}
{% endif -%}
{% endfor -%}
{% else -%}
{% endfor -%}
{% else -%}
urlBuilder_.Append("{{ pathPart }}");
{% endif -%}
{% if forloop.last == false -%}
{% endif -%}
{% if forloop.last == false -%}
urlBuilder_.Append('/');
{% endif -%}
{% endunless -%}
{% endfor -%}
{% endunless -%}
{% endif -%}
{% endunless -%}
{% endfor -%}
{% endunless -%}
{% else -%}
{% unless operation.Path == empty -%}
urlBuilder_.Append("{{ operation.Path }}");
{% endunless -%}
{% endif -%}
{% if operation.HasQueryParameters -%}
urlBuilder_.Append('?');
{% for parameter in operation.QueryParameters -%}
Expand Down

0 comments on commit aff9300

Please sign in to comment.