From b1cc0da5012d9324f477d56327fe85b6357566c3 Mon Sep 17 00:00:00 2001 From: Yifan Zhu Date: Sun, 15 Sep 2024 18:42:40 +0800 Subject: [PATCH 1/2] Add NullRedactor to the RedactorProvider during initialization. Fix #5265 --- .../RedactorProvider.cs | 8 +++++++ .../RedactorProviderTests.cs | 24 ++++++++++++++----- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/Libraries/Microsoft.Extensions.Compliance.Redaction/RedactorProvider.cs b/src/Libraries/Microsoft.Extensions.Compliance.Redaction/RedactorProvider.cs index 0963784fa80..6c307e8a1fe 100644 --- a/src/Libraries/Microsoft.Extensions.Compliance.Redaction/RedactorProvider.cs +++ b/src/Libraries/Microsoft.Extensions.Compliance.Redaction/RedactorProvider.cs @@ -5,6 +5,7 @@ using System.Collections.Frozen; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Linq; using Microsoft.Extensions.Compliance.Classification; using Microsoft.Extensions.Options; using Microsoft.Shared.Diagnostics; @@ -37,6 +38,12 @@ public Redactor GetRedactor(DataClassificationSet classifications) private static FrozenDictionary GetClassRedactorMap(IEnumerable redactors, Dictionary map) { + if (!map.ContainsKey(DataClassification.None)) + { + map.Add(DataClassification.None, typeof(NullRedactor)); + redactors = redactors.Concat([NullRedactor.Instance]); + } + var dict = new Dictionary(map.Count); foreach (var m in map) { @@ -45,6 +52,7 @@ private static FrozenDictionary GetClassRedacto if (r.GetType() == m.Value) { dict[m.Key] = r; + break; } } } diff --git a/test/Libraries/Microsoft.Extensions.Compliance.Redaction.Tests/RedactorProviderTests.cs b/test/Libraries/Microsoft.Extensions.Compliance.Redaction.Tests/RedactorProviderTests.cs index 0a7c97f34f1..9a69e83be50 100644 --- a/test/Libraries/Microsoft.Extensions.Compliance.Redaction.Tests/RedactorProviderTests.cs +++ b/test/Libraries/Microsoft.Extensions.Compliance.Redaction.Tests/RedactorProviderTests.cs @@ -10,6 +10,16 @@ namespace Microsoft.Extensions.Compliance.Redaction.Test; public class RedactorProviderTests { + [Fact] + public void RedactorProvider_Returns_NullRedactor_For_NoneDataClassification() + { + var redactorProvider = new RedactorProvider( + redactors: [ErasingRedactor.Instance], + options: Microsoft.Extensions.Options.Options.Create(new RedactorProviderOptions())); + + Assert.IsType(redactorProvider.GetRedactor(DataClassification.None)); + } + [Fact] public void RedactorProvider_Returns_Redactor_For_Every_Data_Classification() { @@ -38,13 +48,15 @@ public void RedactorProvider_Returns_Redactor_For_Data_Classifications() redactors: new Redactor[] { ErasingRedactor.Instance, NullRedactor.Instance }, options: Microsoft.Extensions.Options.Options.Create(opt)); - var r1 = redactorProvider.GetRedactor(_dataClassification1); - var r2 = redactorProvider.GetRedactor(_dataClassification2); - var r3 = redactorProvider.GetRedactor(_dataClassification3); + Redactor r1 = redactorProvider.GetRedactor(_dataClassification1); + Redactor r2 = redactorProvider.GetRedactor(_dataClassification2); + Redactor r3 = redactorProvider.GetRedactor(_dataClassification3); + Redactor r4 = redactorProvider.GetRedactor(DataClassification.None); - Assert.Equal(typeof(ErasingRedactor), r1.GetType()); - Assert.Equal(typeof(NullRedactor), r2.GetType()); - Assert.Equal(typeof(ErasingRedactor), r3.GetType()); + Assert.IsType(r1); + Assert.IsType(r2); + Assert.IsType(r3); + Assert.IsType(r4); } [Fact] From bc0a965e72b1d3d776265f4ede9f70bc37a93be5 Mon Sep 17 00:00:00 2001 From: Yifan Zhu Date: Thu, 19 Sep 2024 19:54:17 +0800 Subject: [PATCH 2/2] Use spread operator to avoid the need of LINQ --- .../RedactorProvider.cs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Libraries/Microsoft.Extensions.Compliance.Redaction/RedactorProvider.cs b/src/Libraries/Microsoft.Extensions.Compliance.Redaction/RedactorProvider.cs index 6c307e8a1fe..18a356d1a10 100644 --- a/src/Libraries/Microsoft.Extensions.Compliance.Redaction/RedactorProvider.cs +++ b/src/Libraries/Microsoft.Extensions.Compliance.Redaction/RedactorProvider.cs @@ -5,7 +5,6 @@ using System.Collections.Frozen; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; -using System.Linq; using Microsoft.Extensions.Compliance.Classification; using Microsoft.Extensions.Options; using Microsoft.Shared.Diagnostics; @@ -41,7 +40,7 @@ private static FrozenDictionary GetClassRedacto if (!map.ContainsKey(DataClassification.None)) { map.Add(DataClassification.None, typeof(NullRedactor)); - redactors = redactors.Concat([NullRedactor.Instance]); + redactors = [.. redactors, NullRedactor.Instance]; } var dict = new Dictionary(map.Count);