Skip to content

Commit 1d9d44f

Browse files
makazeuYifan Zhu
andauthored
Add NullRedactor to the RedactorProvider during initialization (#5424)
* Add NullRedactor to the RedactorProvider during initialization. Fix #5265 --------- Co-authored-by: Yifan Zhu <[email protected]>
1 parent 539f3a3 commit 1d9d44f

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

src/Libraries/Microsoft.Extensions.Compliance.Redaction/RedactorProvider.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public Redactor GetRedactor(DataClassificationSet classifications)
3737

3838
private static FrozenDictionary<DataClassificationSet, Redactor> GetClassRedactorMap(IEnumerable<Redactor> redactors, Dictionary<DataClassificationSet, Type> map)
3939
{
40+
if (!map.ContainsKey(DataClassification.None))
41+
{
42+
map.Add(DataClassification.None, typeof(NullRedactor));
43+
redactors = [.. redactors, NullRedactor.Instance];
44+
}
45+
4046
var dict = new Dictionary<DataClassificationSet, Redactor>(map.Count);
4147
foreach (var m in map)
4248
{
@@ -45,6 +51,7 @@ private static FrozenDictionary<DataClassificationSet, Redactor> GetClassRedacto
4551
if (r.GetType() == m.Value)
4652
{
4753
dict[m.Key] = r;
54+
break;
4855
}
4956
}
5057
}

test/Libraries/Microsoft.Extensions.Compliance.Redaction.Tests/RedactorProviderTests.cs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,16 @@ namespace Microsoft.Extensions.Compliance.Redaction.Test;
1010

1111
public class RedactorProviderTests
1212
{
13+
[Fact]
14+
public void RedactorProvider_Returns_NullRedactor_For_NoneDataClassification()
15+
{
16+
var redactorProvider = new RedactorProvider(
17+
redactors: [ErasingRedactor.Instance],
18+
options: Microsoft.Extensions.Options.Options.Create(new RedactorProviderOptions()));
19+
20+
Assert.IsType<NullRedactor>(redactorProvider.GetRedactor(DataClassification.None));
21+
}
22+
1323
[Fact]
1424
public void RedactorProvider_Returns_Redactor_For_Every_Data_Classification()
1525
{
@@ -38,13 +48,15 @@ public void RedactorProvider_Returns_Redactor_For_Data_Classifications()
3848
redactors: new Redactor[] { ErasingRedactor.Instance, NullRedactor.Instance },
3949
options: Microsoft.Extensions.Options.Options.Create(opt));
4050

41-
var r1 = redactorProvider.GetRedactor(_dataClassification1);
42-
var r2 = redactorProvider.GetRedactor(_dataClassification2);
43-
var r3 = redactorProvider.GetRedactor(_dataClassification3);
51+
Redactor r1 = redactorProvider.GetRedactor(_dataClassification1);
52+
Redactor r2 = redactorProvider.GetRedactor(_dataClassification2);
53+
Redactor r3 = redactorProvider.GetRedactor(_dataClassification3);
54+
Redactor r4 = redactorProvider.GetRedactor(DataClassification.None);
4455

45-
Assert.Equal(typeof(ErasingRedactor), r1.GetType());
46-
Assert.Equal(typeof(NullRedactor), r2.GetType());
47-
Assert.Equal(typeof(ErasingRedactor), r3.GetType());
56+
Assert.IsType<ErasingRedactor>(r1);
57+
Assert.IsType<NullRedactor>(r2);
58+
Assert.IsType<ErasingRedactor>(r3);
59+
Assert.IsType<NullRedactor>(r4);
4860
}
4961

5062
[Fact]

0 commit comments

Comments
 (0)