Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(CacheManager): GetJsonStringByTypeName prevent throw ArgumentNull exception #5106

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ public class JsonLocalizationOptions : LocalizationOptions
/// </summary>
public bool IgnoreLocalizerMissing { get; set; }

/// <summary>
/// 获得/设置 如果 Value 值为 null 时使用 Key 代替 默认 false 触发异常
/// </summary>
public bool UseKeyWhenValueIsNull { get; set; }

/// <summary>
/// 获得/设置 资源文件是否热加载 默认 false
/// </summary>
Expand Down
19 changes: 12 additions & 7 deletions src/BootstrapBlazor/Services/CacheManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -237,12 +234,20 @@ 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!, 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
Expand Down
4 changes: 4 additions & 0 deletions test/UnitTest/Locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@
"PlaceHolder": "Click to select ...",
"Primary": "Primary",
"Middle": "Middle"
},
"UnitTest.Utils.UtilityTest": {
"Test-Null": null,
"Test-Key:": ""
}
}
28 changes: 28 additions & 0 deletions test/UnitTest/Utils/UtilityTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,34 @@ public void GetJsonStringByTypeName_Ok()
Utility.GetJsonStringByTypeName(option, dynamicType!.Assembly, "Test");
}

[Fact]
public void GetJsonStringByTypeName_UseKeyWhenValueIsNull()
{
// improve code coverage
var option = Context.Services.GetRequiredService<IOptions<JsonLocalizationOptions>>().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);

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
{
public Guid DynamicObjectPrimaryKey { get; set; }
Expand Down
Loading