Skip to content

Commit 0b9efdb

Browse files
pshao25Pan Shao
andauthored
Add exception for Utf8JsonReader/Writer to AZC0014 (#8752)
* update * update * update --------- Co-authored-by: Pan Shao <[email protected]>
1 parent 0c1b964 commit 0b9efdb

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers.Tests/AZC0014Tests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class SomeClient
3939

4040

4141
[Theory]
42+
[InlineData("public void JsonModelWriteCore(Utf8JsonWriter writer) {}")]
43+
[InlineData("public void JsonModelCreateCore(ref Utf8JsonReader reader) {}")]
4244
[InlineData("internal class Class: System.IProgress<JsonElement> { public void Report (JsonElement value) {} }")]
4345
[InlineData("public void Report(string value) {}")]
4446
public async Task AZC0014NotProducedForNonPublicApisOrAllowedTypes(string usage)

src/dotnet/Azure.ClientSdk.Analyzers/Azure.ClientSdk.Analyzers/BannedAssembliesAnalyzer.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,10 @@ static void CheckType(ISymbolAnalysisContext context, ITypeSymbol type, ISymbol
3838
{
3939
if (BannedAssemblies.Contains(type.ContainingAssembly.Name))
4040
{
41-
context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0014, symbol.Locations.First(), BannedAssembliesMessageArgs), symbol);
41+
if (!IsUtf8JsonReaderWriter(type))
42+
{
43+
context.ReportDiagnostic(Diagnostic.Create(Descriptors.AZC0014, symbol.Locations.First(), BannedAssembliesMessageArgs), symbol);
44+
}
4245
}
4346

4447
if (namedTypeSymbol.IsGenericType)
@@ -86,5 +89,15 @@ static void CheckType(ISymbolAnalysisContext context, ITypeSymbol type, ISymbol
8689
break;
8790
}
8891
}
92+
93+
private static bool IsUtf8JsonReaderWriter(ITypeSymbol type)
94+
{
95+
return (type.Name == "Utf8JsonReader" || type.Name == "Utf8JsonWriter") && GetFullNamespace(type.ContainingNamespace) == "System.Text.Json";
96+
}
97+
98+
private static string GetFullNamespace(INamespaceSymbol namespaceSymbol)
99+
{
100+
return namespaceSymbol.ContainingNamespace.Name == "" ? namespaceSymbol.Name : $"{GetFullNamespace(namespaceSymbol.ContainingNamespace)}.{namespaceSymbol.Name}";
101+
}
89102
}
90103
}

0 commit comments

Comments
 (0)