From 82cd471add602e965901efd20a1d1b22226effff Mon Sep 17 00:00:00 2001 From: Yaser Moradi Date: Wed, 12 Feb 2025 17:51:21 +0100 Subject: [PATCH] feat(templates) Use Uri.EscapeDataString instead of HttpUtility.UrlEncode in Boilerplate #9868 (#9875) --- .../HttpClientProxySourceGenerator.cs | 18 ++----- .../Services/ODataQuery.cs | 4 +- .../src/Shared/Controllers/IAppController.cs | 4 +- .../src/Shared/Extensions/UriExtensions.cs | 8 ++- .../Services/AppQueryStringCollection.cs | 50 +------------------ 5 files changed, 11 insertions(+), 73 deletions(-) diff --git a/src/SourceGenerators/Bit.SourceGenerators/HttpClientProxy/HttpClientProxySourceGenerator.cs b/src/SourceGenerators/Bit.SourceGenerators/HttpClientProxy/HttpClientProxySourceGenerator.cs index 68f6700a9d..80f13ba946 100644 --- a/src/SourceGenerators/Bit.SourceGenerators/HttpClientProxy/HttpClientProxySourceGenerator.cs +++ b/src/SourceGenerators/Bit.SourceGenerators/HttpClientProxy/HttpClientProxySourceGenerator.cs @@ -107,12 +107,7 @@ public static void AddTypedHttpClients(this IServiceCollection services) internal class AppControllerBase {{ - System.Collections.Specialized.NameValueCollection queryString = HttpUtility.ParseQueryString(string.Empty); - - public void AddQueryString(string existingQueryString) - {{ - queryString.Add(HttpUtility.ParseQueryString(existingQueryString)); - }} + AppQueryStringCollection queryString = []; public void AddQueryString(string key, object? value) {{ @@ -129,19 +124,12 @@ public void AddQueryStrings(Dictionary queryString) protected string? GetDynamicQueryString() {{ - if (queryString is not {{ Count: > 0 }}) - return null; - var collection = HttpUtility.ParseQueryString(string.Empty); - - foreach (string key in queryString) - {{ - collection.Add(key, queryString[key]); - }} + var result = queryString.ToString(); queryString.Clear(); - return collection.ToString(); + return result; }} }} diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ODataQuery.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ODataQuery.cs index b896669226..6b7df90d72 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ODataQuery.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Client/Boilerplate.Client.Core/Services/ODataQuery.cs @@ -28,7 +28,7 @@ public string OrFilter public override string ToString() { - var qs = new Dictionary(); + var qs = new AppQueryStringCollection(); if (Top is not null) { @@ -65,7 +65,7 @@ public override string ToString() qs.Add("$search", Search); } - return string.Join('&', qs.Select(kv => $"{kv.Key}={kv.Value}")); + return qs.ToString(); } public static implicit operator string(ODataQuery query) => query.ToString(); diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Controllers/IAppController.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Controllers/IAppController.cs index 3028e07401..ffb3751051 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Controllers/IAppController.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Controllers/IAppController.cs @@ -4,7 +4,6 @@ namespace Boilerplate.Shared.Controllers { public interface IAppController { - void AddQueryString(string existingQueryString) { } void AddQueryString(string key, object? value) { } void AddQueryStrings(Dictionary queryString) { } } @@ -17,8 +16,7 @@ public static class IAppControllerExtensions public static TAppController WithQuery(this TAppController controller, string existingQueryString) where TAppController : IAppController { - controller.AddQueryString(existingQueryString); - return controller; + return controller.WithQuery(queryString: AppQueryStringCollection.Parse(existingQueryString)); } public static TAppController WithQuery(this TAppController controller, string key, object? value) diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Extensions/UriExtensions.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Extensions/UriExtensions.cs index 57eaaa37ae..a7e8ecfc24 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Extensions/UriExtensions.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Extensions/UriExtensions.cs @@ -9,7 +9,7 @@ public static string GetUrlWithoutQueryParameter(this Uri uri, string key) string pagePathWithoutQueryString = uri.GetLeftPart(UriPartial.Path); - return qsCollection.IsEmpty is false + return qsCollection is { Count: > 0 } ? $"{pagePathWithoutQueryString}?{qsCollection}" : pagePathWithoutQueryString; } @@ -23,11 +23,9 @@ public static string GetUrlWithoutQueryParameter(this Uri uri, string key) { if (CultureInfoManager.MultilingualEnabled is false) return null; - - var culture = AppQueryStringCollection.Parse(uri.Query)["culture"]; - if (string.IsNullOrEmpty(culture) is false) - return culture; + if (AppQueryStringCollection.Parse(uri.Query).TryGetValue("culture", out var culture)) + return culture?.ToString(); foreach (var segment in uri.Segments.Take(2)) { diff --git a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/AppQueryStringCollection.cs b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/AppQueryStringCollection.cs index 9f6b6a7c35..1f905e6244 100644 --- a/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/AppQueryStringCollection.cs +++ b/src/Templates/Boilerplate/Bit.Boilerplate/src/Shared/Services/AppQueryStringCollection.cs @@ -5,57 +5,11 @@ namespace System; /// /// An alternative to that utilizes instead of . /// -public class AppQueryStringCollection +public class AppQueryStringCollection() : Dictionary(StringComparer.OrdinalIgnoreCase) { - private readonly Dictionary keyValues = []; - - public AppQueryStringCollection Add(string key, string? value) - { - keyValues[Uri.EscapeDataString(Uri.UnescapeDataString(key))] = Uri.EscapeDataString(Uri.UnescapeDataString(value ?? "")); - - return this; - } - - public AppQueryStringCollection Add(AppQueryStringCollection queryStringCollection) - { - foreach (var kv in queryStringCollection.keyValues) - { - keyValues[kv.Key] = kv.Value; - } - - return this; - } - - public AppQueryStringCollection Remove(string key) - { - keyValues.Remove(Uri.EscapeDataString(Uri.UnescapeDataString(key))); - - return this; - } - - public AppQueryStringCollection Clear() - { - keyValues.Clear(); - return this; - } - - public bool IsEmpty => keyValues.Any() is false; - - public string? this[string key] - { - get - { - key = Uri.EscapeDataString(Uri.UnescapeDataString(key)); - if (keyValues.TryGetValue(key, out var value)) - return value; - return null; - } - set => Add(key, value); - } - public override string ToString() { - return string.Join("&", keyValues.Select(kv => $"{kv.Key}={kv.Value}")); + return string.Join("&", this.Select(kv => $"{Uri.EscapeDataString(Uri.UnescapeDataString(kv.Key))}={Uri.EscapeDataString(Uri.UnescapeDataString(kv.Value?.ToString() ?? ""))}")); } public static AppQueryStringCollection Parse(string query)