From 96efb773197b1433b8fb1f53a6146746d908bae4 Mon Sep 17 00:00:00 2001 From: claudiunicola Date: Wed, 11 Dec 2024 13:42:36 +0200 Subject: [PATCH 1/2] Fix query string url builder for Dictionary Prepended property name for dictionary keys Before: ?key1=value1&key2=value2 After: ?propertyDictName.key1=value1&propertyDictName.key2=value2 --- .../Templates/Client.Class.QueryParameter.liquid | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.QueryParameter.liquid b/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.QueryParameter.liquid index 3a6ea85864..6b7155da7a 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.QueryParameter.liquid +++ b/src/NSwag.CodeGeneration.CSharp/Templates/Client.Class.QueryParameter.liquid @@ -9,7 +9,7 @@ urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}")).Append(' {% elsif parameter.IsArray -%} 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('=').Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append('&'); } +foreach (var item_ in {{ parameter.VariableName }}) { urlBuilder_.Append(System.Uri.EscapeDataString("{{ parameter.Name }}")).Append('.').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) From 731dcfa5cfd57830a23cdd6bcb02adeff12c3018 Mon Sep 17 00:00:00 2001 From: Claudiu Nicola Date: Wed, 11 Dec 2024 15:22:23 +0200 Subject: [PATCH 2/2] Updated unit tests --- src/NSwag.CodeGeneration.CSharp.Tests/QueryParameterTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/QueryParameterTests.cs b/src/NSwag.CodeGeneration.CSharp.Tests/QueryParameterTests.cs index 50d3ec5d11..2f939a6ae2 100644 --- a/src/NSwag.CodeGeneration.CSharp.Tests/QueryParameterTests.cs +++ b/src/NSwag.CodeGeneration.CSharp.Tests/QueryParameterTests.cs @@ -209,7 +209,7 @@ public async Task When_query_parameter_is_typed_free_form_object_parameters_are_ "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('=').Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append('&'); }", + "foreach (var item_ in extendedProperties) { urlBuilder_.Append(System.Uri.EscapeDataString(\"extendedProperties\")).Append('.').Append(System.Uri.EscapeDataString(item_.Key)).Append('=').Append(System.Uri.EscapeDataString(ConvertToString(item_.Value, System.Globalization.CultureInfo.InvariantCulture))).Append('&'); }", code); }