diff --git a/modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache b/modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache index 88489f886a72..fc1910c9eb3a 100644 --- a/modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/HttpSigningConfiguration.mustache @@ -25,8 +25,10 @@ namespace {{packageName}}.Client { HashAlgorithm = HashAlgorithmName.SHA256; SigningAlgorithm = "PKCS1-v15"; + {{^useHttpClient}} _skipUrlEncode = RuntimeInformation.FrameworkDescription.StartsWith(".NET ") && int.TryParse(RuntimeInformation.FrameworkDescription.Substring(5).Split('.')[0], out int fwMajor) && fwMajor >= 9; + {{/useHttpClient}} } /// @@ -69,6 +71,7 @@ namespace {{packageName}}.Client /// public int SignatureValidityPeriod { get; set; } + {{^useHttpClient}} // On .NET 9+, HttpUtility.ParseQueryString already URL-encodes keys internally, // so calling UrlEncode again would cause double-encoding and produce a signature // that does not match the actual request sent by RestSharp 112+. @@ -76,6 +79,7 @@ namespace {{packageName}}.Client // characters (e.g. '$' in OData params like $filter) are encoded the same way // in the signature as they are in the outgoing HTTP request. private readonly bool _skipUrlEncode; + {{/useHttpClient}} private enum PrivateKeyType { @@ -139,6 +143,7 @@ namespace {{packageName}}.Client } } + {{^useHttpClient}} var httpValues = HttpUtility.ParseQueryString(string.Empty); foreach (var parameter in requestOptions.QueryParameters) { @@ -171,6 +176,18 @@ namespace {{packageName}}.Client } var uriBuilder = new UriBuilder(string.Concat(basePath, path)); uriBuilder.Query = httpValues.ToString().Replace("+", "%20"); + {{/useHttpClient}} + {{#useHttpClient}} + // Build the pairs directly: HttpUtility.ParseQueryString is case-insensitive and would merge keys such as "tag" and "Tag". + var queryPairs = new List(); + foreach (var parameter in requestOptions.QueryParameters) + { + foreach (var value in parameter.Value) + queryPairs.Add(HttpUtility.UrlEncode(parameter.Key) + "=" + HttpUtility.UrlEncode(value)); + } + var uriBuilder = new UriBuilder(string.Concat(basePath, path)); + uriBuilder.Query = string.Join("&", queryPairs).Replace("+", "%20").Replace("%24", "$"); + {{/useHttpClient}} var dateTime = DateTime.Now; string Digest = string.Empty; diff --git a/modules/openapi-generator/src/main/resources/csharp/WebRequestPathBuilder.mustache b/modules/openapi-generator/src/main/resources/csharp/WebRequestPathBuilder.mustache index cc811ae4f983..1a4775d35f5d 100644 --- a/modules/openapi-generator/src/main/resources/csharp/WebRequestPathBuilder.mustache +++ b/modules/openapi-generator/src/main/resources/csharp/WebRequestPathBuilder.mustache @@ -1,6 +1,9 @@ {{>partial_header}} using System; using System.Collections.Generic; +{{#useHttpClient}} +using System.Web; +{{/useHttpClient}} namespace {{packageName}}.Client { @@ -28,6 +31,19 @@ namespace {{packageName}}.Client public void AddQueryParameters(Multimap parameters) { + {{#useHttpClient}} + // Build the pairs directly: HttpUtility.ParseQueryString is case-insensitive and would merge keys such as "tag" and "Tag". + var queryPairs = new List(); + foreach (var parameter in parameters) + { + foreach (var value in parameter.Value) + { + queryPairs.Add(HttpUtility.UrlEncode(parameter.Key) + "=" + HttpUtility.UrlEncode(value)); + } + } + _query = string.Join("&", queryPairs).Replace("+", "%20").Replace("%24", "$"); + {{/useHttpClient}} + {{^useHttpClient}} foreach (var parameter in parameters) { foreach (var value in parameter.Value) @@ -35,11 +51,17 @@ namespace {{packageName}}.Client _query = _query + parameter.Key + "=" + Uri.EscapeDataString(value) + "&"; } } + {{/useHttpClient}} } public string GetFullUri() { + {{#useHttpClient}} + return new UriBuilder(_baseUrl + _path) { Query = _query }.Uri.AbsoluteUri; + {{/useHttpClient}} + {{^useHttpClient}} return _baseUrl + _path + _query.Substring(0, _query.Length - 1); + {{/useHttpClient}} } } } diff --git a/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs b/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs index 19c67f8ad9a4..49c9a567680e 100644 --- a/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs +++ b/samples/client/petstore/csharp/httpclient/net10/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Generic; +using System.Web; namespace Org.OpenAPITools.Client { @@ -36,18 +37,21 @@ public void AddPathParameters(Dictionary parameters) public void AddQueryParameters(Multimap parameters) { + // Build the pairs directly: HttpUtility.ParseQueryString is case-insensitive and would merge keys such as "tag" and "Tag". + var queryPairs = new List(); foreach (var parameter in parameters) { foreach (var value in parameter.Value) { - _query = _query + parameter.Key + "=" + Uri.EscapeDataString(value) + "&"; + queryPairs.Add(HttpUtility.UrlEncode(parameter.Key) + "=" + HttpUtility.UrlEncode(value)); } } + _query = string.Join("&", queryPairs).Replace("+", "%20").Replace("%24", "$"); } public string GetFullUri() { - return _baseUrl + _path + _query.Substring(0, _query.Length - 1); + return new UriBuilder(_baseUrl + _path) { Query = _query }.Uri.AbsoluteUri; } } } diff --git a/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 963c14ffac6b..83b28bc667dd 100644 --- a/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -33,8 +33,6 @@ public HttpSigningConfiguration() { HashAlgorithm = HashAlgorithmName.SHA256; SigningAlgorithm = "PKCS1-v15"; - _skipUrlEncode = RuntimeInformation.FrameworkDescription.StartsWith(".NET ") && - int.TryParse(RuntimeInformation.FrameworkDescription.Substring(5).Split('.')[0], out int fwMajor) && fwMajor >= 9; } /// @@ -77,13 +75,6 @@ public HttpSigningConfiguration() /// public int SignatureValidityPeriod { get; set; } - // On .NET 9+, HttpUtility.ParseQueryString already URL-encodes keys internally, - // so calling UrlEncode again would cause double-encoding and produce a signature - // that does not match the actual request sent by RestSharp 112+. - // On .NET 8 and earlier, keys must be explicitly URL-encoded so that special - // characters (e.g. '$' in OData params like $filter) are encoded the same way - // in the signature as they are in the outgoing HTTP request. - private readonly bool _skipUrlEncode; private enum PrivateKeyType { @@ -147,38 +138,15 @@ public Dictionary GetHttpSignedHeader(string basePath,string met } } - var httpValues = HttpUtility.ParseQueryString(string.Empty); + // Build the pairs directly: HttpUtility.ParseQueryString is case-insensitive and would merge keys such as "tag" and "Tag". + var queryPairs = new List(); foreach (var parameter in requestOptions.QueryParameters) { -#if (NETCOREAPP) - string key = _skipUrlEncode ? parameter.Key : HttpUtility.UrlEncode(parameter.Key); - if (parameter.Value.Count > 1) - { // array - foreach (var value in parameter.Value) - { - httpValues.Add(key + "[]", value); - } - } - else - { - httpValues.Add(key, parameter.Value[0]); - } -#else - if (parameter.Value.Count > 1) - { // array - foreach (var value in parameter.Value) - { - httpValues.Add(parameter.Key + "[]", value); - } - } - else - { - httpValues.Add(parameter.Key, parameter.Value[0]); - } -#endif + foreach (var value in parameter.Value) + queryPairs.Add(HttpUtility.UrlEncode(parameter.Key) + "=" + HttpUtility.UrlEncode(value)); } var uriBuilder = new UriBuilder(string.Concat(basePath, path)); - uriBuilder.Query = httpValues.ToString().Replace("+", "%20"); + uriBuilder.Query = string.Join("&", queryPairs).Replace("+", "%20").Replace("%24", "$"); var dateTime = DateTime.Now; string Digest = string.Empty; diff --git a/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs b/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs index e4af746d7d6d..f4c9b4ae3582 100644 --- a/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs +++ b/samples/client/petstore/csharp/httpclient/net10/Petstore/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Generic; +using System.Web; namespace Org.OpenAPITools.Client { @@ -36,18 +37,21 @@ public void AddPathParameters(Dictionary parameters) public void AddQueryParameters(Multimap parameters) { + // Build the pairs directly: HttpUtility.ParseQueryString is case-insensitive and would merge keys such as "tag" and "Tag". + var queryPairs = new List(); foreach (var parameter in parameters) { foreach (var value in parameter.Value) { - _query = _query + parameter.Key + "=" + Uri.EscapeDataString(value) + "&"; + queryPairs.Add(HttpUtility.UrlEncode(parameter.Key) + "=" + HttpUtility.UrlEncode(value)); } } + _query = string.Join("&", queryPairs).Replace("+", "%20").Replace("%24", "$"); } public string GetFullUri() { - return _baseUrl + _path + _query.Substring(0, _query.Length - 1); + return new UriBuilder(_baseUrl + _path) { Query = _query }.Uri.AbsoluteUri; } } } diff --git a/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs b/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs index 19c67f8ad9a4..49c9a567680e 100644 --- a/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs +++ b/samples/client/petstore/csharp/httpclient/net9/Petstore-nonPublicApi/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Generic; +using System.Web; namespace Org.OpenAPITools.Client { @@ -36,18 +37,21 @@ public void AddPathParameters(Dictionary parameters) public void AddQueryParameters(Multimap parameters) { + // Build the pairs directly: HttpUtility.ParseQueryString is case-insensitive and would merge keys such as "tag" and "Tag". + var queryPairs = new List(); foreach (var parameter in parameters) { foreach (var value in parameter.Value) { - _query = _query + parameter.Key + "=" + Uri.EscapeDataString(value) + "&"; + queryPairs.Add(HttpUtility.UrlEncode(parameter.Key) + "=" + HttpUtility.UrlEncode(value)); } } + _query = string.Join("&", queryPairs).Replace("+", "%20").Replace("%24", "$"); } public string GetFullUri() { - return _baseUrl + _path + _query.Substring(0, _query.Length - 1); + return new UriBuilder(_baseUrl + _path) { Query = _query }.Uri.AbsoluteUri; } } } diff --git a/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 963c14ffac6b..83b28bc667dd 100644 --- a/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -33,8 +33,6 @@ public HttpSigningConfiguration() { HashAlgorithm = HashAlgorithmName.SHA256; SigningAlgorithm = "PKCS1-v15"; - _skipUrlEncode = RuntimeInformation.FrameworkDescription.StartsWith(".NET ") && - int.TryParse(RuntimeInformation.FrameworkDescription.Substring(5).Split('.')[0], out int fwMajor) && fwMajor >= 9; } /// @@ -77,13 +75,6 @@ public HttpSigningConfiguration() /// public int SignatureValidityPeriod { get; set; } - // On .NET 9+, HttpUtility.ParseQueryString already URL-encodes keys internally, - // so calling UrlEncode again would cause double-encoding and produce a signature - // that does not match the actual request sent by RestSharp 112+. - // On .NET 8 and earlier, keys must be explicitly URL-encoded so that special - // characters (e.g. '$' in OData params like $filter) are encoded the same way - // in the signature as they are in the outgoing HTTP request. - private readonly bool _skipUrlEncode; private enum PrivateKeyType { @@ -147,38 +138,15 @@ public Dictionary GetHttpSignedHeader(string basePath,string met } } - var httpValues = HttpUtility.ParseQueryString(string.Empty); + // Build the pairs directly: HttpUtility.ParseQueryString is case-insensitive and would merge keys such as "tag" and "Tag". + var queryPairs = new List(); foreach (var parameter in requestOptions.QueryParameters) { -#if (NETCOREAPP) - string key = _skipUrlEncode ? parameter.Key : HttpUtility.UrlEncode(parameter.Key); - if (parameter.Value.Count > 1) - { // array - foreach (var value in parameter.Value) - { - httpValues.Add(key + "[]", value); - } - } - else - { - httpValues.Add(key, parameter.Value[0]); - } -#else - if (parameter.Value.Count > 1) - { // array - foreach (var value in parameter.Value) - { - httpValues.Add(parameter.Key + "[]", value); - } - } - else - { - httpValues.Add(parameter.Key, parameter.Value[0]); - } -#endif + foreach (var value in parameter.Value) + queryPairs.Add(HttpUtility.UrlEncode(parameter.Key) + "=" + HttpUtility.UrlEncode(value)); } var uriBuilder = new UriBuilder(string.Concat(basePath, path)); - uriBuilder.Query = httpValues.ToString().Replace("+", "%20"); + uriBuilder.Query = string.Join("&", queryPairs).Replace("+", "%20").Replace("%24", "$"); var dateTime = DateTime.Now; string Digest = string.Empty; diff --git a/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs b/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs index e4af746d7d6d..f4c9b4ae3582 100644 --- a/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs +++ b/samples/client/petstore/csharp/httpclient/net9/Petstore/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Generic; +using System.Web; namespace Org.OpenAPITools.Client { @@ -36,18 +37,21 @@ public void AddPathParameters(Dictionary parameters) public void AddQueryParameters(Multimap parameters) { + // Build the pairs directly: HttpUtility.ParseQueryString is case-insensitive and would merge keys such as "tag" and "Tag". + var queryPairs = new List(); foreach (var parameter in parameters) { foreach (var value in parameter.Value) { - _query = _query + parameter.Key + "=" + Uri.EscapeDataString(value) + "&"; + queryPairs.Add(HttpUtility.UrlEncode(parameter.Key) + "=" + HttpUtility.UrlEncode(value)); } } + _query = string.Join("&", queryPairs).Replace("+", "%20").Replace("%24", "$"); } public string GetFullUri() { - return _baseUrl + _path + _query.Substring(0, _query.Length - 1); + return new UriBuilder(_baseUrl + _path) { Query = _query }.Uri.AbsoluteUri; } } } diff --git a/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs b/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs index 30f7cd154cf2..2c74acbb23c6 100644 --- a/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs +++ b/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/HttpSigningConfiguration.cs @@ -33,8 +33,6 @@ public HttpSigningConfiguration() { HashAlgorithm = HashAlgorithmName.SHA256; SigningAlgorithm = "PKCS1-v15"; - _skipUrlEncode = RuntimeInformation.FrameworkDescription.StartsWith(".NET ") && - int.TryParse(RuntimeInformation.FrameworkDescription.Substring(5).Split('.')[0], out int fwMajor) && fwMajor >= 9; } /// @@ -77,13 +75,6 @@ public HttpSigningConfiguration() /// public int SignatureValidityPeriod { get; set; } - // On .NET 9+, HttpUtility.ParseQueryString already URL-encodes keys internally, - // so calling UrlEncode again would cause double-encoding and produce a signature - // that does not match the actual request sent by RestSharp 112+. - // On .NET 8 and earlier, keys must be explicitly URL-encoded so that special - // characters (e.g. '$' in OData params like $filter) are encoded the same way - // in the signature as they are in the outgoing HTTP request. - private readonly bool _skipUrlEncode; private enum PrivateKeyType { @@ -147,38 +138,15 @@ public Dictionary GetHttpSignedHeader(string basePath,string met } } - var httpValues = HttpUtility.ParseQueryString(string.Empty); + // Build the pairs directly: HttpUtility.ParseQueryString is case-insensitive and would merge keys such as "tag" and "Tag". + var queryPairs = new List(); foreach (var parameter in requestOptions.QueryParameters) { -#if (NETCOREAPP) - string key = _skipUrlEncode ? parameter.Key : HttpUtility.UrlEncode(parameter.Key); - if (parameter.Value.Count > 1) - { // array - foreach (var value in parameter.Value) - { - httpValues.Add(key + "[]", value); - } - } - else - { - httpValues.Add(key, parameter.Value[0]); - } -#else - if (parameter.Value.Count > 1) - { // array - foreach (var value in parameter.Value) - { - httpValues.Add(parameter.Key + "[]", value); - } - } - else - { - httpValues.Add(parameter.Key, parameter.Value[0]); - } -#endif + foreach (var value in parameter.Value) + queryPairs.Add(HttpUtility.UrlEncode(parameter.Key) + "=" + HttpUtility.UrlEncode(value)); } var uriBuilder = new UriBuilder(string.Concat(basePath, path)); - uriBuilder.Query = httpValues.ToString().Replace("+", "%20"); + uriBuilder.Query = string.Join("&", queryPairs).Replace("+", "%20").Replace("%24", "$"); var dateTime = DateTime.Now; string Digest = string.Empty; diff --git a/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs b/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs index e4af746d7d6d..f4c9b4ae3582 100644 --- a/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs +++ b/samples/client/petstore/csharp/httpclient/standard2.0/Petstore/src/Org.OpenAPITools/Client/WebRequestPathBuilder.cs @@ -9,6 +9,7 @@ using System; using System.Collections.Generic; +using System.Web; namespace Org.OpenAPITools.Client { @@ -36,18 +37,21 @@ public void AddPathParameters(Dictionary parameters) public void AddQueryParameters(Multimap parameters) { + // Build the pairs directly: HttpUtility.ParseQueryString is case-insensitive and would merge keys such as "tag" and "Tag". + var queryPairs = new List(); foreach (var parameter in parameters) { foreach (var value in parameter.Value) { - _query = _query + parameter.Key + "=" + Uri.EscapeDataString(value) + "&"; + queryPairs.Add(HttpUtility.UrlEncode(parameter.Key) + "=" + HttpUtility.UrlEncode(value)); } } + _query = string.Join("&", queryPairs).Replace("+", "%20").Replace("%24", "$"); } public string GetFullUri() { - return _baseUrl + _path + _query.Substring(0, _query.Length - 1); + return new UriBuilder(_baseUrl + _path) { Query = _query }.Uri.AbsoluteUri; } } }