From 85a80950e91989ccd65e5cca11ab8c760c09d543 Mon Sep 17 00:00:00 2001 From: Praveen Kuttappan Date: Wed, 22 Jan 2025 16:26:04 -0500 Subject: [PATCH] Update dotnet parser to skip RequiresUnreferencedCode attribute --- .../TreeToken/CodeFileBuilder.cs | 3 +++ .../CSharpAPIParserTests.csproj | 3 ++- .../CSharpAPIParserTests/CodeFileTests.cs | 23 ++++++++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/tools/apiview/parsers/csharp-api-parser/CSharpAPIParser/TreeToken/CodeFileBuilder.cs b/tools/apiview/parsers/csharp-api-parser/CSharpAPIParser/TreeToken/CodeFileBuilder.cs index 5853a16aed9..5d34b5e7cb6 100644 --- a/tools/apiview/parsers/csharp-api-parser/CSharpAPIParser/TreeToken/CodeFileBuilder.cs +++ b/tools/apiview/parsers/csharp-api-parser/CSharpAPIParser/TreeToken/CodeFileBuilder.cs @@ -9,6 +9,7 @@ using System.Collections.Immutable; using System.ComponentModel; using ApiView; +using System.Diagnostics.CodeAnalysis; namespace CSharpAPIParser.TreeToken { @@ -540,6 +541,8 @@ private bool IsSkippedAttribute(INamedTypeSymbol attributeAttributeClass) case "EditorBrowsableAttribute": case "NullableAttribute": case "NullableContextAttribute": + case "RequiresUnreferencedCodeAttribute": + case "RequiresDynamicCodeAttribute": return true; default: return false; diff --git a/tools/apiview/parsers/csharp-api-parser/CSharpAPIParserTests/CSharpAPIParserTests.csproj b/tools/apiview/parsers/csharp-api-parser/CSharpAPIParserTests/CSharpAPIParserTests.csproj index 006456c6bc2..22aedf7fd10 100644 --- a/tools/apiview/parsers/csharp-api-parser/CSharpAPIParserTests/CSharpAPIParserTests.csproj +++ b/tools/apiview/parsers/csharp-api-parser/CSharpAPIParserTests/CSharpAPIParserTests.csproj @@ -10,8 +10,9 @@ - + + diff --git a/tools/apiview/parsers/csharp-api-parser/CSharpAPIParserTests/CodeFileTests.cs b/tools/apiview/parsers/csharp-api-parser/CSharpAPIParserTests/CodeFileTests.cs index b4160e43a86..eb2b6e2fa4e 100644 --- a/tools/apiview/parsers/csharp-api-parser/CSharpAPIParserTests/CodeFileTests.cs +++ b/tools/apiview/parsers/csharp-api-parser/CSharpAPIParserTests/CodeFileTests.cs @@ -45,7 +45,7 @@ static CodeFileTests() { new object[] { templateCodeFile, "Azure.Template" , "1.0.3.0", 9}, new object[] { storageCodeFile , "Azure.Storage.Blobs", "12.21.2.0", 15}, - new object[] { coreCodeFile, "Azure.Core", "1.42.0.0", 27}, + new object[] { coreCodeFile, "Azure.Core", "1.44.1.0", 27}, }; [Theory] @@ -365,5 +365,26 @@ public void VerifyTemplateClassLine() Assert.NotNull(methodLine); Assert.Equal("public static DataFactoryElement FromKeyVaultSecret(DataFactoryKeyVaultSecret secret);", methodLine.ToString().Trim()); } + + [Fact] + public void VerifySkippedAttributes() + { + var serviceBusAssembly = Assembly.Load("Azure.Messaging.ServiceBus"); + var dllStream = serviceBusAssembly.GetFile("Azure.Messaging.ServiceBus.dll"); + var assemblySymbol = CompilationFactory.GetCompilation(dllStream, null); + var codeFile = new CSharpAPIParser.TreeToken.CodeFileBuilder().Build(assemblySymbol, true, null); + + var line = codeFile.ReviewLines.Where(l => l.LineId == "Microsoft.Extensions.Azure").FirstOrDefault(); + Assert.NotNull(line); + var classLine = line.Children?.Where(l => l.LineId == "Microsoft.Extensions.Azure.ServiceBusClientBuilderExtensions").FirstOrDefault(); + Assert.NotNull(classLine); + var methodLine = classLine.Children?.Where(l => l.LineId.Contains("Microsoft.Extensions.Azure.ServiceBusClientBuilderExtensions.AddServiceBusClient")).FirstOrDefault(); + Assert.NotNull(methodLine); + + bool isRequiresUnreferencedCodePresent = classLine.Children?.Any(l => l.Tokens.Any(t => t.Value == "RequiresUnreferencedCodeAttribute")) ?? false; + bool isRequiresDynamicCode = classLine.Children?.Any(l => l.Tokens.Any(t => t.Value == "RequiresDynamicCode")) ?? false; + Assert.False(isRequiresUnreferencedCodePresent); + Assert.False(isRequiresDynamicCode); + } } }