From f868683c9930d7156003c61821e5124c4ed8d9c3 Mon Sep 17 00:00:00 2001 From: Sven Boemer Date: Wed, 30 Oct 2024 15:30:34 -0700 Subject: [PATCH] Address a few AOT warnings in Azure.Core (#46611) * Address a few AOT warnings in Azure.Core * Fix apicompat --- sdk/core/Azure.Core/api/Azure.Core.net6.0.cs | 2 ++ .../src/DynamicData/DynamicData.AllowList.cs | 11 ++++++++++- sdk/core/Azure.Core/src/JsonPatchDocument.cs | 5 +++++ .../tests/compatibility/ExpectedAotWarnings.txt | 3 --- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs b/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs index 1061bfa856d4..db853279328b 100644 --- a/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs +++ b/sdk/core/Azure.Core/api/Azure.Core.net6.0.cs @@ -95,8 +95,10 @@ public HttpAuthorization(string scheme, string parameter) { } } public partial class JsonPatchDocument { + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JsonObjectSerializer uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")] public JsonPatchDocument() { } public JsonPatchDocument(Azure.Core.Serialization.ObjectSerializer serializer) { } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JsonObjectSerializer uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")] public JsonPatchDocument(System.ReadOnlyMemory rawDocument) { } public JsonPatchDocument(System.ReadOnlyMemory rawDocument, Azure.Core.Serialization.ObjectSerializer serializer) { } public void AppendAddRaw(string path, string rawJsonValue) { } diff --git a/sdk/core/Azure.Core/src/DynamicData/DynamicData.AllowList.cs b/sdk/core/Azure.Core/src/DynamicData/DynamicData.AllowList.cs index e466a774b777..64ca5566ccee 100644 --- a/sdk/core/Azure.Core/src/DynamicData/DynamicData.AllowList.cs +++ b/sdk/core/Azure.Core/src/DynamicData/DynamicData.AllowList.cs @@ -4,6 +4,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Diagnostics.CodeAnalysis; using System.Reflection; using System.Text.Json; @@ -13,6 +14,7 @@ public partial class DynamicData { internal class AllowList { + [RequiresUnreferencedCode("Reflection over unknown type")] public static void AssertAllowedValue(T value) { if (value == null) @@ -26,6 +28,7 @@ public static void AssertAllowedValue(T value) } } + [RequiresUnreferencedCode("Reflection over unknown type")] private static bool IsAllowedValue(T value) { if (value == null) @@ -63,6 +66,7 @@ private static bool IsAllowedType(Type type) type == typeof(DynamicData); } + [RequiresUnreferencedCode("Reflection over unknown type")] private static bool IsAllowedCollectionValue(Type type, T value) { return @@ -71,6 +75,7 @@ private static bool IsAllowedCollectionValue(Type type, T value) IsAllowedDictionaryValue(type, value); } + [RequiresUnreferencedCode("Reflection over unknown type")] private static bool IsAllowedArrayValue(Type type, T value) { if (value is not Array array) @@ -92,6 +97,7 @@ private static bool IsAllowedArrayValue(Type type, T value) return IsAllowedEnumerableValue(elementType, array); } + [RequiresUnreferencedCode("Reflection over unknown type")] private static bool IsAllowedListValue(Type type, T value) { if (value == null) @@ -118,6 +124,7 @@ private static bool IsAllowedListValue(Type type, T value) return IsAllowedEnumerableValue(genericArgument, (IEnumerable)value); } + [RequiresUnreferencedCode("Reflection over unknown type")] private static bool IsAllowedDictionaryValue(Type type, T value) { if (value == null) @@ -149,6 +156,7 @@ private static bool IsAllowedDictionaryValue(Type type, T value) return IsAllowedEnumerableValue(genericArguments[1], ((IDictionary)value).Values); } + [RequiresUnreferencedCode("Reflection over unknown type")] private static bool IsAllowedEnumerableValue(Type elementType, IEnumerable enumerable) { foreach (var item in enumerable) @@ -174,7 +182,8 @@ private static bool IsAllowedEnumerableValue(Type elementType, IEnumerable enume return true; } - private static bool IsAllowedAnonymousValue(Type type, T value) + [RequiresUnreferencedCode("Reflection over unknown type")] + private static bool IsAllowedAnonymousValue([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] Type type, T value) { if (!IsAnonymousType(type)) { diff --git a/sdk/core/Azure.Core/src/JsonPatchDocument.cs b/sdk/core/Azure.Core/src/JsonPatchDocument.cs index e1728aa957d5..805921b52ef0 100644 --- a/sdk/core/Azure.Core/src/JsonPatchDocument.cs +++ b/sdk/core/Azure.Core/src/JsonPatchDocument.cs @@ -3,6 +3,7 @@ using System; using System.Collections.ObjectModel; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using System.Text; @@ -25,6 +26,8 @@ public class JsonPatchDocument /// /// Initializes a new instance of that uses as the default serializer. /// + [RequiresUnreferencedCode("JsonObjectSerializer uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")] + [RequiresDynamicCode("JsonObjectSerializer uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")] public JsonPatchDocument() : this(default(ReadOnlyMemory)) { } @@ -41,6 +44,8 @@ public JsonPatchDocument(ObjectSerializer serializer): this(default(ReadOnlyMemo /// Initializes a new instance of /// /// The binary representation of JSON Patch document. + [RequiresUnreferencedCode("JsonObjectSerializer uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")] + [RequiresDynamicCode("JsonObjectSerializer uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")] public JsonPatchDocument(ReadOnlyMemory rawDocument) : this(rawDocument, new JsonObjectSerializer()) { } diff --git a/sdk/core/Azure.Core/tests/compatibility/ExpectedAotWarnings.txt b/sdk/core/Azure.Core/tests/compatibility/ExpectedAotWarnings.txt index 7de79d8f192f..915a5ee86153 100644 --- a/sdk/core/Azure.Core/tests/compatibility/ExpectedAotWarnings.txt +++ b/sdk/core/Azure.Core/tests/compatibility/ExpectedAotWarnings.txt @@ -2,9 +2,6 @@ ILC : Trim analysis warning IL2026: Azure\.Core\.Serialization\.DynamicData: Usi ILC : AOT analysis warning IL3050: Azure\.Core\.Serialization\.DynamicData: Using member 'Azure\.Core\.Serialization\.DynamicData\.DynamicDataJsonConverter\.DynamicDataJsonConverter\(\)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling\. Using DynamicData or DynamicDataConverter is not compatible with trimming due to reflection-based serialization ILC : Trim analysis warning IL2026: Azure\.Core\.Json\.MutableJsonDocument: Using member 'Azure\.Core\.Json\.MutableJsonDocument\.MutableJsonDocumentConverter\.MutableJsonDocumentConverter\(\)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code\. Using MutableJsonDocument or MutableJsonDocumentConverter is not compatible with trimming due to reflection-based serialization ILC : AOT analysis warning IL3050: Azure\.Core\.Json\.MutableJsonDocument: Using member 'Azure\.Core\.Json\.MutableJsonDocument\.MutableJsonDocumentConverter\.MutableJsonDocumentConverter\(\)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling\. Using MutableJsonDocument or MutableJsonDocumentConverter is not compatible with trimming due to reflection-based serialization -.*Azure\.Core.src.JsonPatchDocument\.cs\(\d*\): Trim analysis warning IL2026: Azure\.JsonPatchDocument\.JsonPatchDocument\(ReadOnlyMemory`1\): Using member 'Azure\.Core\.Serialization\.JsonObjectSerializer\.JsonObjectSerializer\(\)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code\. This class uses reflection-based JSON serialization and deserialization that is not compatible with trimming -.*Azure\.Core.src.JsonPatchDocument\.cs\(\d*\): AOT analysis warning IL3050: Azure\.JsonPatchDocument\.JsonPatchDocument\(ReadOnlyMemory`1\): Using member 'Azure\.Core\.Serialization\.JsonObjectSerializer\.JsonObjectSerializer\(\)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling\. This class uses reflection-based JSON serialization and deserialization that is not compatible with trimming -.*Azure\.Core.src.DynamicData.DynamicData\.AllowList\.cs\(\d*\): Trim analysis warning IL2070: Azure\.Core\.Serialization\.DynamicData\.AllowList\.IsAllowedAnonymousValue\(Type,!!0\): 'this' argument does not satisfy 'DynamicallyAccessedMemberTypes\.PublicProperties' in call to 'System\.Type\.GetProperties\(\)'\. The parameter 'type' of method 'Azure\.Core\.Serialization\.DynamicData\.AllowList\.IsAllowedAnonymousValue\(Type,!!0\)' does not have matching annotations\. The source value must declare at least the same requirements as those declared on the target location it is assigned to .*Azure\.Core.src.DynamicData.DynamicData\.ArrayEnumerator\.cs\(\d*\): Trim analysis warning IL2026: Azure\.Core\.Serialization\.DynamicData\.ArrayEnumerator\.Current\.get: Using member 'Azure\.Core\.Serialization\.DynamicData\.DynamicData\(MutableJsonElement,DynamicDataOptions\)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code\. This class utilizes reflection-based JSON serialization and deserialization which is not compatible with trimming .*Azure\.Core.src.DynamicData.DynamicData\.ArrayEnumerator\.cs\(\d*\): AOT analysis warning IL3050: Azure\.Core\.Serialization\.DynamicData\.ArrayEnumerator\.Current\.get: Using member 'Azure\.Core\.Serialization\.DynamicData\.DynamicData\(MutableJsonElement,DynamicDataOptions\)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling\. This class utilizes reflection-based JSON serialization and deserialization which is not compatible with trimming .*Azure\.Core.src.DynamicData.DynamicData\.ObjectEnumerator\.cs\(\d*\): Trim analysis warning IL2026: Azure\.Core\.Serialization\.DynamicData\.ObjectEnumerator\.Current\.get: Using member 'Azure\.Core\.Serialization\.DynamicData\.DynamicData\(MutableJsonElement,DynamicDataOptions\)' which has 'RequiresUnreferencedCodeAttribute' can break functionality when trimming application code\. This class utilizes reflection-based JSON serialization and deserialization which is not compatible with trimming