Skip to content

Commit f868683

Browse files
authored
Address a few AOT warnings in Azure.Core (Azure#46611)
* Address a few AOT warnings in Azure.Core * Fix apicompat
1 parent f6f56f9 commit f868683

File tree

4 files changed

+17
-4
lines changed

4 files changed

+17
-4
lines changed

sdk/core/Azure.Core/api/Azure.Core.net6.0.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,10 @@ public HttpAuthorization(string scheme, string parameter) { }
9595
}
9696
public partial class JsonPatchDocument
9797
{
98+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JsonObjectSerializer uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")]
9899
public JsonPatchDocument() { }
99100
public JsonPatchDocument(Azure.Core.Serialization.ObjectSerializer serializer) { }
101+
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("JsonObjectSerializer uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")]
100102
public JsonPatchDocument(System.ReadOnlyMemory<byte> rawDocument) { }
101103
public JsonPatchDocument(System.ReadOnlyMemory<byte> rawDocument, Azure.Core.Serialization.ObjectSerializer serializer) { }
102104
public void AppendAddRaw(string path, string rawJsonValue) { }

sdk/core/Azure.Core/src/DynamicData/DynamicData.AllowList.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections;
66
using System.Collections.Generic;
7+
using System.Diagnostics.CodeAnalysis;
78
using System.Reflection;
89
using System.Text.Json;
910

@@ -13,6 +14,7 @@ public partial class DynamicData
1314
{
1415
internal class AllowList
1516
{
17+
[RequiresUnreferencedCode("Reflection over unknown type")]
1618
public static void AssertAllowedValue<T>(T value)
1719
{
1820
if (value == null)
@@ -26,6 +28,7 @@ public static void AssertAllowedValue<T>(T value)
2628
}
2729
}
2830

31+
[RequiresUnreferencedCode("Reflection over unknown type")]
2932
private static bool IsAllowedValue<T>(T value)
3033
{
3134
if (value == null)
@@ -63,6 +66,7 @@ private static bool IsAllowedType(Type type)
6366
type == typeof(DynamicData);
6467
}
6568

69+
[RequiresUnreferencedCode("Reflection over unknown type")]
6670
private static bool IsAllowedCollectionValue<T>(Type type, T value)
6771
{
6872
return
@@ -71,6 +75,7 @@ private static bool IsAllowedCollectionValue<T>(Type type, T value)
7175
IsAllowedDictionaryValue(type, value);
7276
}
7377

78+
[RequiresUnreferencedCode("Reflection over unknown type")]
7479
private static bool IsAllowedArrayValue<T>(Type type, T value)
7580
{
7681
if (value is not Array array)
@@ -92,6 +97,7 @@ private static bool IsAllowedArrayValue<T>(Type type, T value)
9297
return IsAllowedEnumerableValue(elementType, array);
9398
}
9499

100+
[RequiresUnreferencedCode("Reflection over unknown type")]
95101
private static bool IsAllowedListValue<T>(Type type, T value)
96102
{
97103
if (value == null)
@@ -118,6 +124,7 @@ private static bool IsAllowedListValue<T>(Type type, T value)
118124
return IsAllowedEnumerableValue(genericArgument, (IEnumerable)value);
119125
}
120126

127+
[RequiresUnreferencedCode("Reflection over unknown type")]
121128
private static bool IsAllowedDictionaryValue<T>(Type type, T value)
122129
{
123130
if (value == null)
@@ -149,6 +156,7 @@ private static bool IsAllowedDictionaryValue<T>(Type type, T value)
149156
return IsAllowedEnumerableValue(genericArguments[1], ((IDictionary)value).Values);
150157
}
151158

159+
[RequiresUnreferencedCode("Reflection over unknown type")]
152160
private static bool IsAllowedEnumerableValue(Type elementType, IEnumerable enumerable)
153161
{
154162
foreach (var item in enumerable)
@@ -174,7 +182,8 @@ private static bool IsAllowedEnumerableValue(Type elementType, IEnumerable enume
174182
return true;
175183
}
176184

177-
private static bool IsAllowedAnonymousValue<T>(Type type, T value)
185+
[RequiresUnreferencedCode("Reflection over unknown type")]
186+
private static bool IsAllowedAnonymousValue<T>([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] Type type, T value)
178187
{
179188
if (!IsAnonymousType(type))
180189
{

sdk/core/Azure.Core/src/JsonPatchDocument.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.Collections.ObjectModel;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.IO;
78
using System.Linq;
89
using System.Text;
@@ -25,6 +26,8 @@ public class JsonPatchDocument
2526
/// <summary>
2627
/// Initializes a new instance of <see cref="JsonPatchDocument"/> that uses <see cref="JsonObjectSerializer"/> as the default serializer.
2728
/// </summary>
29+
[RequiresUnreferencedCode("JsonObjectSerializer uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")]
30+
[RequiresDynamicCode("JsonObjectSerializer uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")]
2831
public JsonPatchDocument() : this(default(ReadOnlyMemory<byte>))
2932
{
3033
}
@@ -41,6 +44,8 @@ public JsonPatchDocument(ObjectSerializer serializer): this(default(ReadOnlyMemo
4144
/// Initializes a new instance of <see cref="JsonPatchDocument"/>
4245
/// </summary>
4346
/// <param name="rawDocument">The binary representation of JSON Patch document.</param>
47+
[RequiresUnreferencedCode("JsonObjectSerializer uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")]
48+
[RequiresDynamicCode("JsonObjectSerializer uses reflection-based JSON serialization and deserialization that is not compatible with trimming.")]
4449
public JsonPatchDocument(ReadOnlyMemory<byte> rawDocument) : this(rawDocument, new JsonObjectSerializer())
4550
{
4651
}

sdk/core/Azure.Core/tests/compatibility/ExpectedAotWarnings.txt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@ ILC : Trim analysis warning IL2026: Azure\.Core\.Serialization\.DynamicData: Usi
22
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
33
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
44
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
5-
.*Azure\.Core.src.JsonPatchDocument\.cs\(\d*\): Trim analysis warning IL2026: Azure\.JsonPatchDocument\.JsonPatchDocument\(ReadOnlyMemory`1<Byte>\): 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
6-
.*Azure\.Core.src.JsonPatchDocument\.cs\(\d*\): AOT analysis warning IL3050: Azure\.JsonPatchDocument\.JsonPatchDocument\(ReadOnlyMemory`1<Byte>\): 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
7-
.*Azure\.Core.src.DynamicData.DynamicData\.AllowList\.cs\(\d*\): Trim analysis warning IL2070: Azure\.Core\.Serialization\.DynamicData\.AllowList\.IsAllowedAnonymousValue<T>\(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<T>\(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
85
.*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
96
.*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
107
.*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

0 commit comments

Comments
 (0)