From a931393501bf77434a087e00d47a47d0f16162a3 Mon Sep 17 00:00:00 2001 From: Alex chow Date: Mon, 13 Jan 2025 13:44:17 +0100 Subject: [PATCH 1/8] fix CacheManager GetLocalizedString Key.Value Null --- src/BootstrapBlazor/Services/CacheManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Services/CacheManager.cs b/src/BootstrapBlazor/Services/CacheManager.cs index 936a8b3302f..3464232682f 100644 --- a/src/BootstrapBlazor/Services/CacheManager.cs +++ b/src/BootstrapBlazor/Services/CacheManager.cs @@ -242,7 +242,7 @@ private static JsonLocalizationOptions GetJsonLocalizationOption() var sections = Instance.GetOrCreate(key, entry => option.GetJsonStringFromAssembly(assembly, cultureName)); var items = sections.FirstOrDefault(kv => typeName.Equals(kv.Key, StringComparison.OrdinalIgnoreCase))? .GetChildren() - .SelectMany(kv => new[] { new LocalizedString(kv.Key, kv.Value!, false, typeName) }); + .SelectMany(kv => new[] { new LocalizedString(kv.Key, kv.Value?? kv.Key, false, typeName) }); #if NET8_0_OR_GREATER return items?.ToFrozenSet(); #else From 89d791732fa1647d548f9288d33c32c445478ec6 Mon Sep 17 00:00:00 2001 From: Argo-iMac Date: Wed, 15 Jan 2025 10:17:10 +0800 Subject: [PATCH 2/8] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20UseKeyWhenValu?= =?UTF-8?q?eIsNull=20=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Localization/Json/JsonLocalizationOptions.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/BootstrapBlazor/Localization/Json/JsonLocalizationOptions.cs b/src/BootstrapBlazor/Localization/Json/JsonLocalizationOptions.cs index 2136e06fca1..1668284f1a2 100644 --- a/src/BootstrapBlazor/Localization/Json/JsonLocalizationOptions.cs +++ b/src/BootstrapBlazor/Localization/Json/JsonLocalizationOptions.cs @@ -43,6 +43,11 @@ public class JsonLocalizationOptions : LocalizationOptions /// public bool IgnoreLocalizerMissing { get; set; } + /// + /// 获得/设置 如果 Value 值为 null 时使用 Key 代替 默认 false 触发异常 + /// + public bool UseKeyWhenValueIsNull { get; set; } + /// /// 获得/设置 资源文件是否热加载 默认 false /// From e215329dc0ad28ad1a8a835b6142003060eff7f7 Mon Sep 17 00:00:00 2001 From: Argo-iMac Date: Wed, 15 Jan 2025 10:17:23 +0800 Subject: [PATCH 3/8] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Services/CacheManager.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/BootstrapBlazor/Services/CacheManager.cs b/src/BootstrapBlazor/Services/CacheManager.cs index 3464232682f..b32777d1369 100644 --- a/src/BootstrapBlazor/Services/CacheManager.cs +++ b/src/BootstrapBlazor/Services/CacheManager.cs @@ -152,10 +152,7 @@ public static int ElementCount(object? value) entry.SetDynamicAssemblyPolicy(type); return LambdaExtensions.CountLambda(type).Compile(); }); - if (invoker != null) - { - ret = invoker(value); - } + ret = invoker(value); } return ret; } From 41fc5d254044ba2f9084e33465b5e556226405fb Mon Sep 17 00:00:00 2001 From: Argo-iMac Date: Wed, 15 Jan 2025 10:17:40 +0800 Subject: [PATCH 4/8] =?UTF-8?q?refactor:=20=E4=BC=98=E5=8C=96=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Services/CacheManager.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/BootstrapBlazor/Services/CacheManager.cs b/src/BootstrapBlazor/Services/CacheManager.cs index b32777d1369..dda14ac2306 100644 --- a/src/BootstrapBlazor/Services/CacheManager.cs +++ b/src/BootstrapBlazor/Services/CacheManager.cs @@ -234,9 +234,9 @@ private static JsonLocalizationOptions GetJsonLocalizationOption() Instance.Cache.Remove(key); Instance.Cache.Remove(typeKey); } - return Instance.GetOrCreate(typeKey, entry => + return Instance.GetOrCreate(typeKey, _ => { - var sections = Instance.GetOrCreate(key, entry => option.GetJsonStringFromAssembly(assembly, cultureName)); + var sections = Instance.GetOrCreate(key, _ => option.GetJsonStringFromAssembly(assembly, cultureName)); var items = sections.FirstOrDefault(kv => typeName.Equals(kv.Key, StringComparison.OrdinalIgnoreCase))? .GetChildren() .SelectMany(kv => new[] { new LocalizedString(kv.Key, kv.Value?? kv.Key, false, typeName) }); From dfea8cc5cf7e4ae2a91c6b0de3c1e8ea84cf7173 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Wed, 15 Jan 2025 10:28:35 +0800 Subject: [PATCH 5/8] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=20UseKeyWhenValu?= =?UTF-8?q?eIsNull=20=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Services/CacheManager.cs | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Services/CacheManager.cs b/src/BootstrapBlazor/Services/CacheManager.cs index dda14ac2306..66143cf2fc2 100644 --- a/src/BootstrapBlazor/Services/CacheManager.cs +++ b/src/BootstrapBlazor/Services/CacheManager.cs @@ -239,7 +239,15 @@ private static JsonLocalizationOptions GetJsonLocalizationOption() var sections = Instance.GetOrCreate(key, _ => option.GetJsonStringFromAssembly(assembly, cultureName)); var items = sections.FirstOrDefault(kv => typeName.Equals(kv.Key, StringComparison.OrdinalIgnoreCase))? .GetChildren() - .SelectMany(kv => new[] { new LocalizedString(kv.Key, kv.Value?? kv.Key, false, typeName) }); + .Select(kv => + { + var value = kv.Value; + if (value == null && option.UseKeyWhenValueIsNull == true) + { + value = kv.Key; + } + return new LocalizedString(kv.Key, value!, false, typeName); + }); #if NET8_0_OR_GREATER return items?.ToFrozenSet(); #else From eec9725642471936aa3da9ab8048452cc91c9c2e Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Wed, 15 Jan 2025 11:12:20 +0800 Subject: [PATCH 6/8] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Locales/en-US.json | 4 ++++ test/UnitTest/Utils/UtilityTest.cs | 17 +++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/test/UnitTest/Locales/en-US.json b/test/UnitTest/Locales/en-US.json index 92afa009eb6..9f650f7779b 100644 --- a/test/UnitTest/Locales/en-US.json +++ b/test/UnitTest/Locales/en-US.json @@ -25,5 +25,9 @@ "PlaceHolder": "Click to select ...", "Primary": "Primary", "Middle": "Middle" + }, + "UnitTest.Utils.UtilityTest": { + "Test-Null": null, + "Test-Key:": "" } } diff --git a/test/UnitTest/Utils/UtilityTest.cs b/test/UnitTest/Utils/UtilityTest.cs index d3d8759fd79..48cc4472f8d 100644 --- a/test/UnitTest/Utils/UtilityTest.cs +++ b/test/UnitTest/Utils/UtilityTest.cs @@ -379,6 +379,23 @@ public void GetJsonStringByTypeName_Ok() Utility.GetJsonStringByTypeName(option, dynamicType!.Assembly, "Test"); } + [Fact] + public void GetJsonStringByTypeName_Exception() + { + // improve code coverage + var option = Context.Services.GetRequiredService>().Value; + option.UseKeyWhenValueIsNull = true; + var items = Utility.GetJsonStringByTypeName(option, this.GetType().Assembly, "UnitTest.Utils.UtilityTest", "en-US", true); + + var test1 = items.FirstOrDefault(i => i.Name == "Test-Null"); + Assert.NotNull(test1); + Assert.Equal("", test1.Value); + + var test2 = items.FirstOrDefault(i => i.Name == "Test-Key"); + Assert.NotNull(test2); + Assert.Equal("Test-Key", test2.Value); + } + private class MockDynamicObject : IDynamicObject { public Guid DynamicObjectPrimaryKey { get; set; } From 84e8c8422ee101861814313041dc34e08b45f8d0 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Wed, 15 Jan 2025 11:24:11 +0800 Subject: [PATCH 7/8] =?UTF-8?q?refactor:=20=E5=A2=9E=E5=8A=A0=20null=20?= =?UTF-8?q?=E4=BF=9D=E6=8A=A4=E9=98=B2=E6=AD=A2=E6=8A=A5=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Services/CacheManager.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Services/CacheManager.cs b/src/BootstrapBlazor/Services/CacheManager.cs index 66143cf2fc2..f8d7158a014 100644 --- a/src/BootstrapBlazor/Services/CacheManager.cs +++ b/src/BootstrapBlazor/Services/CacheManager.cs @@ -246,7 +246,7 @@ private static JsonLocalizationOptions GetJsonLocalizationOption() { value = kv.Key; } - return new LocalizedString(kv.Key, value!, false, typeName); + return new LocalizedString(kv.Key, value ?? "", false, typeName); }); #if NET8_0_OR_GREATER return items?.ToFrozenSet(); From d942d6d794708dd0d7e16eb1cb5045e00cb44ecd Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Wed, 15 Jan 2025 11:31:49 +0800 Subject: [PATCH 8/8] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Utils/UtilityTest.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/test/UnitTest/Utils/UtilityTest.cs b/test/UnitTest/Utils/UtilityTest.cs index 48cc4472f8d..2d543cc0cc8 100644 --- a/test/UnitTest/Utils/UtilityTest.cs +++ b/test/UnitTest/Utils/UtilityTest.cs @@ -380,7 +380,7 @@ public void GetJsonStringByTypeName_Ok() } [Fact] - public void GetJsonStringByTypeName_Exception() + public void GetJsonStringByTypeName_UseKeyWhenValueIsNull() { // improve code coverage var option = Context.Services.GetRequiredService>().Value; @@ -394,6 +394,17 @@ public void GetJsonStringByTypeName_Exception() var test2 = items.FirstOrDefault(i => i.Name == "Test-Key"); Assert.NotNull(test2); Assert.Equal("Test-Key", test2.Value); + + option.UseKeyWhenValueIsNull = false; + items = Utility.GetJsonStringByTypeName(option, this.GetType().Assembly, "UnitTest.Utils.UtilityTest", "en-US", true); + + test1 = items.FirstOrDefault(i => i.Name == "Test-Null"); + Assert.NotNull(test1); + Assert.Equal("", test1.Value); + + test2 = items.FirstOrDefault(i => i.Name == "Test-Key"); + Assert.NotNull(test2); + Assert.Equal("", test2.Value); } private class MockDynamicObject : IDynamicObject