Skip to content

Commit

Permalink
Address a few AOT warnings in Azure.Core (#46611)
Browse files Browse the repository at this point in the history
* Address a few AOT warnings in Azure.Core

* Fix apicompat
  • Loading branch information
sbomer authored Oct 30, 2024
1 parent f6f56f9 commit f868683
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 4 deletions.
2 changes: 2 additions & 0 deletions sdk/core/Azure.Core/api/Azure.Core.net6.0.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> rawDocument) { }
public JsonPatchDocument(System.ReadOnlyMemory<byte> rawDocument, Azure.Core.Serialization.ObjectSerializer serializer) { }
public void AppendAddRaw(string path, string rawJsonValue) { }
Expand Down
11 changes: 10 additions & 1 deletion sdk/core/Azure.Core/src/DynamicData/DynamicData.AllowList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Text.Json;

Expand All @@ -13,6 +14,7 @@ public partial class DynamicData
{
internal class AllowList
{
[RequiresUnreferencedCode("Reflection over unknown type")]
public static void AssertAllowedValue<T>(T value)
{
if (value == null)
Expand All @@ -26,6 +28,7 @@ public static void AssertAllowedValue<T>(T value)
}
}

[RequiresUnreferencedCode("Reflection over unknown type")]
private static bool IsAllowedValue<T>(T value)
{
if (value == null)
Expand Down Expand Up @@ -63,6 +66,7 @@ private static bool IsAllowedType(Type type)
type == typeof(DynamicData);
}

[RequiresUnreferencedCode("Reflection over unknown type")]
private static bool IsAllowedCollectionValue<T>(Type type, T value)
{
return
Expand All @@ -71,6 +75,7 @@ private static bool IsAllowedCollectionValue<T>(Type type, T value)
IsAllowedDictionaryValue(type, value);
}

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

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

[RequiresUnreferencedCode("Reflection over unknown type")]
private static bool IsAllowedDictionaryValue<T>(Type type, T value)
{
if (value == null)
Expand Down Expand Up @@ -149,6 +156,7 @@ private static bool IsAllowedDictionaryValue<T>(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)
Expand All @@ -174,7 +182,8 @@ private static bool IsAllowedEnumerableValue(Type elementType, IEnumerable enume
return true;
}

private static bool IsAllowedAnonymousValue<T>(Type type, T value)
[RequiresUnreferencedCode("Reflection over unknown type")]
private static bool IsAllowedAnonymousValue<T>([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] Type type, T value)
{
if (!IsAnonymousType(type))
{
Expand Down
5 changes: 5 additions & 0 deletions sdk/core/Azure.Core/src/JsonPatchDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.Collections.ObjectModel;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Text;
Expand All @@ -25,6 +26,8 @@ public class JsonPatchDocument
/// <summary>
/// Initializes a new instance of <see cref="JsonPatchDocument"/> that uses <see cref="JsonObjectSerializer"/> as the default serializer.
/// </summary>
[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<byte>))
{
}
Expand All @@ -41,6 +44,8 @@ public JsonPatchDocument(ObjectSerializer serializer): this(default(ReadOnlyMemo
/// Initializes a new instance of <see cref="JsonPatchDocument"/>
/// </summary>
/// <param name="rawDocument">The binary representation of JSON Patch document.</param>
[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<byte> rawDocument) : this(rawDocument, new JsonObjectSerializer())
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
.*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
.*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
.*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
Expand Down

0 comments on commit f868683

Please sign in to comment.