Skip to content

Commit b73c3ec

Browse files
izanhzhArgoZhang
andauthored
fix(JsonStringLocalizer): add type check prevent endless loop (#4653)
* fix(JsonStringLocalizer):Endless loop bug * chore: bump version 9.0.0-beta01 --------- Co-authored-by: Argo Zhang <[email protected]>
1 parent a8fa755 commit b73c3ec

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.0.0</Version>
4+
<Version>9.0.0-beta01</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Localization/Json/JsonStringLocalizer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public override LocalizedString this[string name]
8282
{
8383
string? ret = null;
8484
var localizer = Utility.GetStringLocalizerFromService(Assembly, typeName);
85-
if (localizer != null)
85+
if (localizer != null && localizer is not JsonStringLocalizer)
8686
{
8787
ret = GetLocalizerValueFromCache(localizer, name);
8888
}
@@ -177,7 +177,7 @@ public override IEnumerable<LocalizedString> GetAllStrings(bool includeParentCul
177177
{
178178
IEnumerable<LocalizedString>? ret = null;
179179
var localizer = Utility.GetStringLocalizerFromService(Assembly, typeName);
180-
if (localizer != null)
180+
if (localizer != null && localizer is not JsonStringLocalizer)
181181
{
182182
ret = localizer.GetAllStrings(includeParentCultures);
183183
}

test/UnitTest/Localization/JsonStringLocalizerTest.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
55

6+
using Microsoft.Extensions.DependencyInjection;
67
using Microsoft.Extensions.Localization;
78
using System.ComponentModel.DataAnnotations;
89
using System.Reflection;
@@ -306,6 +307,25 @@ public void Validate_ResourceManagerStringLocalizerType()
306307
Assert.Equal("Test", result[0].ErrorMessage);
307308
}
308309

310+
[Fact]
311+
public void ValidateFixEndlessLoop()
312+
{
313+
var sc = new ServiceCollection();
314+
sc.AddConfiguration();
315+
sc.AddBootstrapBlazor();
316+
sc.AddSingleton<IStringLocalizerFactory, MockLocalizerFactory>();
317+
sc.AddSingleton<IStringLocalizerFactory, MockLocalizerFactory2>();
318+
319+
var provider = sc.BuildServiceProvider();
320+
var localizer = provider.GetRequiredService<IStringLocalizer<Foo>>();
321+
322+
Assert.Equal("姓名", localizer["Name"]);
323+
324+
var items = localizer.GetAllStrings(false);
325+
Assert.Equal("姓名", items.First(i => i.Name == "Name").Value);
326+
Assert.DoesNotContain("Test-JsonName", items.Select(i => i.Name));
327+
}
328+
309329
private class MockTypeInfo : TypeDelegator
310330
{
311331
public override string? FullName => null;
@@ -318,6 +338,37 @@ private class MockLocalizerFactory : IStringLocalizerFactory
318338
public IStringLocalizer Create(string baseName, string location) => new MockStringLocalizer();
319339
}
320340

341+
private class MockLocalizerFactory2 : IStringLocalizerFactory
342+
{
343+
private readonly IServiceProvider _serviceProvider;
344+
345+
public MockLocalizerFactory2(IServiceProvider serviceProvider)
346+
{
347+
_serviceProvider = serviceProvider;
348+
}
349+
350+
public IStringLocalizer Create(Type resourceSource)
351+
{
352+
var stringLocalizerFactorys = _serviceProvider.GetServices<IStringLocalizerFactory>();
353+
IStringLocalizerFactory stringLocalizerFactory;
354+
if (resourceSource == typeof(Foo))
355+
{
356+
stringLocalizerFactory = stringLocalizerFactorys.Single(s => s.GetType().Name == "JsonStringLocalizerFactory");
357+
}
358+
else
359+
{
360+
stringLocalizerFactory = _serviceProvider.GetServices<IStringLocalizerFactory>().Single(s => s is MockLocalizerFactory);
361+
}
362+
return stringLocalizerFactory.Create(resourceSource);
363+
}
364+
365+
public IStringLocalizer Create(string baseName, string location)
366+
{
367+
var stringLocalizerFactory = _serviceProvider.GetServices<IStringLocalizerFactory>().Single(s => s is MockLocalizerFactory);
368+
return stringLocalizerFactory.Create(baseName, location);
369+
}
370+
}
371+
321372
private class MockStringLocalizer : IStringLocalizer
322373
{
323374
public LocalizedString this[string name] => GetAllStrings(true).FirstOrDefault(l => l.Name == name) ?? new LocalizedString(name, name, true);

0 commit comments

Comments
 (0)