From e24544858c43a8ff9564a7430bb04bf90ec8204c Mon Sep 17 00:00:00 2001 From: Maxwell Weru Date: Wed, 5 Jun 2024 11:27:45 +0300 Subject: [PATCH] Replace IsTrimmable with IsAotCompatible which contains all necessary analyzers --- src/Directory.Build.props | 2 +- .../SharedKey/SharedKeyEvents.cs | 2 +- .../Tingle.AspNetCore.JsonPatch.csproj | 2 +- .../Tingle.AspNetCore.Swagger.csproj | 2 +- .../ContinuationTokenModelBinderProvider.cs | 2 ++ .../Extensions/IMvcBuilderExtensions.cs | 3 +++ src/Tingle.AspNetCore.Tokens/MessageStrings.cs | 2 ++ .../Mvc/ContinuationTokenResult.cs | 4 +++- .../DatabaseSetup.cs | 2 ++ .../Extensions/IServiceCollectionExtensions.cs | 9 ++++----- .../MessageStrings.cs | 2 ++ .../Converters/JsonPatchDocumentConverter.cs | 13 ++++++------- .../Converters/TypedJsonPatchDocumentConverter.cs | 15 ++++----------- src/Tingle.Extensions.JsonPatch/MessageStrings.cs | 7 +++++++ .../MongoDbContextOptions.cs | 2 +- .../Converters/EnumConverterHelper.cs | 4 ++-- 16 files changed, 42 insertions(+), 31 deletions(-) create mode 100644 src/Tingle.Extensions.JsonPatch/MessageStrings.cs diff --git a/src/Directory.Build.props b/src/Directory.Build.props index b37618be..bbe8d80a 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -8,7 +8,7 @@ true Tingle Software - true + true diff --git a/src/Tingle.AspNetCore.Authentication/SharedKey/SharedKeyEvents.cs b/src/Tingle.AspNetCore.Authentication/SharedKey/SharedKeyEvents.cs index a426e6e0..42fa27d1 100644 --- a/src/Tingle.AspNetCore.Authentication/SharedKey/SharedKeyEvents.cs +++ b/src/Tingle.AspNetCore.Authentication/SharedKey/SharedKeyEvents.cs @@ -11,7 +11,7 @@ public class SharedKeyEvents public Func OnAuthenticationFailed { get; set; } = context => Task.CompletedTask; /// - /// Invoked if Authorization fails and results in a Forbidden response + /// Invoked if Authorization fails and results in a Forbidden response /// public Func OnForbidden { get; set; } = context => Task.CompletedTask; diff --git a/src/Tingle.AspNetCore.JsonPatch/Tingle.AspNetCore.JsonPatch.csproj b/src/Tingle.AspNetCore.JsonPatch/Tingle.AspNetCore.JsonPatch.csproj index 8d755303..8d08b946 100644 --- a/src/Tingle.AspNetCore.JsonPatch/Tingle.AspNetCore.JsonPatch.csproj +++ b/src/Tingle.AspNetCore.JsonPatch/Tingle.AspNetCore.JsonPatch.csproj @@ -3,7 +3,7 @@ JSON Patch support for AspNetCore using System.Text.Json false - false + false diff --git a/src/Tingle.AspNetCore.Swagger/Tingle.AspNetCore.Swagger.csproj b/src/Tingle.AspNetCore.Swagger/Tingle.AspNetCore.Swagger.csproj index d85e2a15..d53e4801 100644 --- a/src/Tingle.AspNetCore.Swagger/Tingle.AspNetCore.Swagger.csproj +++ b/src/Tingle.AspNetCore.Swagger/Tingle.AspNetCore.Swagger.csproj @@ -3,7 +3,7 @@ Usability extensions for Swagger middleware including smaller ReDoc support net8.0 - false + false diff --git a/src/Tingle.AspNetCore.Tokens/Binders/ContinuationTokenModelBinderProvider.cs b/src/Tingle.AspNetCore.Tokens/Binders/ContinuationTokenModelBinderProvider.cs index 2f901762..2de3afb8 100644 --- a/src/Tingle.AspNetCore.Tokens/Binders/ContinuationTokenModelBinderProvider.cs +++ b/src/Tingle.AspNetCore.Tokens/Binders/ContinuationTokenModelBinderProvider.cs @@ -1,6 +1,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.ModelBinding; using Microsoft.AspNetCore.Mvc.ModelBinding.Binders; +using System.Diagnostics.CodeAnalysis; namespace Tingle.AspNetCore.Tokens.Binders; @@ -12,6 +13,7 @@ namespace Tingle.AspNetCore.Tokens.Binders; /// /// if applicable. /// +[RequiresDynamicCode(MessageStrings.ModelBindingGenericsRequiresDynamicCodeMessage)] internal class ContinuationTokenModelBinderProvider : IModelBinderProvider { /// diff --git a/src/Tingle.AspNetCore.Tokens/Extensions/IMvcBuilderExtensions.cs b/src/Tingle.AspNetCore.Tokens/Extensions/IMvcBuilderExtensions.cs index 2c57d296..2951a249 100644 --- a/src/Tingle.AspNetCore.Tokens/Extensions/IMvcBuilderExtensions.cs +++ b/src/Tingle.AspNetCore.Tokens/Extensions/IMvcBuilderExtensions.cs @@ -1,4 +1,6 @@ using Microsoft.AspNetCore.Mvc; +using System.Diagnostics.CodeAnalysis; +using Tingle.AspNetCore.Tokens; using Tingle.AspNetCore.Tokens.Binders; using Tingle.AspNetCore.Tokens.Protection; @@ -25,6 +27,7 @@ public static class IMvcBuilderExtensions /// /// The application's MVC builder. /// The modified builder. + [RequiresDynamicCode(MessageStrings.ModelBindingGenericsRequiresDynamicCodeMessage)] public static IMvcBuilder AddTokens(this IMvcBuilder builder) { // Register the protector services diff --git a/src/Tingle.AspNetCore.Tokens/MessageStrings.cs b/src/Tingle.AspNetCore.Tokens/MessageStrings.cs index 7afc554a..e50aa242 100644 --- a/src/Tingle.AspNetCore.Tokens/MessageStrings.cs +++ b/src/Tingle.AspNetCore.Tokens/MessageStrings.cs @@ -4,4 +4,6 @@ internal class MessageStrings { public const string SerializationUnreferencedCodeMessage = "JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved."; public const string SerializationRequiresDynamicCodeMessage = "JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications."; + + public const string ModelBindingGenericsRequiresDynamicCodeMessage = "Model binding for generic types requires dynamic code generation which is not support for native AOT applications."; } diff --git a/src/Tingle.AspNetCore.Tokens/Mvc/ContinuationTokenResult.cs b/src/Tingle.AspNetCore.Tokens/Mvc/ContinuationTokenResult.cs index 504004b7..915a26ab 100644 --- a/src/Tingle.AspNetCore.Tokens/Mvc/ContinuationTokenResult.cs +++ b/src/Tingle.AspNetCore.Tokens/Mvc/ContinuationTokenResult.cs @@ -54,7 +54,7 @@ public override void OnFormatting(ActionContext context) { base.OnFormatting(context); // required so that it can write the statusCode - // we can only set the header if + // we can only set the header if // 1) the provided token instance is not null // 2) the underlying value is not null // 3) the protected value is not null or empty @@ -64,6 +64,7 @@ public override void OnFormatting(ActionContext context) var protector = context.HttpContext.RequestServices.GetRequiredService>(); #pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code +#pragma warning disable IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling. // generate a new protected value based on the type of token string protected_val; var value = token.GetValue(); @@ -85,6 +86,7 @@ public override void OnFormatting(ActionContext context) if (!string.IsNullOrWhiteSpace(protected_val)) context.HttpContext.Response.Headers[headerName] = protected_val; } +#pragma warning restore IL3050 // Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling. #pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code } } diff --git a/src/Tingle.Extensions.EntityFrameworkCore/DatabaseSetup.cs b/src/Tingle.Extensions.EntityFrameworkCore/DatabaseSetup.cs index 90c62fe8..ccdd8390 100644 --- a/src/Tingle.Extensions.EntityFrameworkCore/DatabaseSetup.cs +++ b/src/Tingle.Extensions.EntityFrameworkCore/DatabaseSetup.cs @@ -3,6 +3,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; +using System.Diagnostics.CodeAnalysis; namespace Tingle.Extensions.EntityFrameworkCore; @@ -10,6 +11,7 @@ namespace Tingle.Extensions.EntityFrameworkCore; /// Helper for performing migrations or creation. /// /// The type of context to be used. +[RequiresDynamicCode(MessageStrings.MigrationsRequiresDynamicCodeMessage)] public class DatabaseSetup : IHostedService where TContext : DbContext { private readonly IServiceScopeFactory scopeFactory; diff --git a/src/Tingle.Extensions.EntityFrameworkCore/Extensions/IServiceCollectionExtensions.cs b/src/Tingle.Extensions.EntityFrameworkCore/Extensions/IServiceCollectionExtensions.cs index 40cd216c..ce167c54 100644 --- a/src/Tingle.Extensions.EntityFrameworkCore/Extensions/IServiceCollectionExtensions.cs +++ b/src/Tingle.Extensions.EntityFrameworkCore/Extensions/IServiceCollectionExtensions.cs @@ -1,5 +1,6 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Hosting; +using System.Diagnostics.CodeAnalysis; using Tingle.Extensions.EntityFrameworkCore; namespace Microsoft.Extensions.DependencyInjection; @@ -17,9 +18,7 @@ public static class IServiceCollectionExtensions /// Migrations are done when the configuration value EFCORE_PERFORM_MIGRATIONS is set to . /// Database creation is done when configuration value EFCORE_CREATE_DATABASE is set to . /// - public static IServiceCollection AddDatabaseSetup(this IServiceCollection services) - where TContext : DbContext - { - return services.AddHostedService>(); - } + [RequiresDynamicCode(MessageStrings.MigrationsRequiresDynamicCodeMessage)] + public static IServiceCollection AddDatabaseSetup(this IServiceCollection services) where TContext : DbContext + => services.AddHostedService>(); } diff --git a/src/Tingle.Extensions.EntityFrameworkCore/MessageStrings.cs b/src/Tingle.Extensions.EntityFrameworkCore/MessageStrings.cs index 1886bc29..58dd6f53 100644 --- a/src/Tingle.Extensions.EntityFrameworkCore/MessageStrings.cs +++ b/src/Tingle.Extensions.EntityFrameworkCore/MessageStrings.cs @@ -4,4 +4,6 @@ internal class MessageStrings { public const string SerializationUnreferencedCodeMessage = "JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved."; public const string SerializationRequiresDynamicCodeMessage = "JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications."; + + public const string MigrationsRequiresDynamicCodeMessage = "Migration operations are not supported with Native AOT. Use a migration bundle or an alternate way of executing migration operations."; } diff --git a/src/Tingle.Extensions.JsonPatch/Converters/JsonPatchDocumentConverter.cs b/src/Tingle.Extensions.JsonPatch/Converters/JsonPatchDocumentConverter.cs index 29e9d0e3..181b3191 100644 --- a/src/Tingle.Extensions.JsonPatch/Converters/JsonPatchDocumentConverter.cs +++ b/src/Tingle.Extensions.JsonPatch/Converters/JsonPatchDocumentConverter.cs @@ -1,9 +1,12 @@ -using System.Text.Json; +using System.Diagnostics.CodeAnalysis; +using System.Text.Json; using System.Text.Json.Serialization; using Tingle.Extensions.JsonPatch.Operations; namespace Tingle.Extensions.JsonPatch.Converters; +[RequiresUnreferencedCode(MessageStrings.SerializationUnreferencedCodeMessage)] +[RequiresDynamicCode(MessageStrings.SerializationRequiresDynamicCodeMessage)] public class JsonPatchDocumentConverter : JsonConverter { /// @@ -11,19 +14,15 @@ public class JsonPatchDocumentConverter : JsonConverter { if (reader.TokenType == JsonTokenType.Null) return default; -#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code - var targetOperations = JsonSerializer.Deserialize>(ref reader, options); -#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code + var operations = JsonSerializer.Deserialize>(ref reader, options); - return new JsonPatchDocument(targetOperations ?? []); + return new JsonPatchDocument(operations ?? []); } /// public override void Write(Utf8JsonWriter writer, JsonPatchDocument value, JsonSerializerOptions options) { // we write an array of the operations -#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code JsonSerializer.Serialize(writer, value.Operations, options); -#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code } } diff --git a/src/Tingle.Extensions.JsonPatch/Converters/TypedJsonPatchDocumentConverter.cs b/src/Tingle.Extensions.JsonPatch/Converters/TypedJsonPatchDocumentConverter.cs index b8be5928..92a3837c 100644 --- a/src/Tingle.Extensions.JsonPatch/Converters/TypedJsonPatchDocumentConverter.cs +++ b/src/Tingle.Extensions.JsonPatch/Converters/TypedJsonPatchDocumentConverter.cs @@ -3,13 +3,10 @@ using System.Text.Json.Serialization; using Tingle.Extensions.JsonPatch.Operations; -#if NETCOREAPP -#else -#pragma warning disable IDE0079 // Remove unnecessary suppression -#endif - namespace Tingle.Extensions.JsonPatch.Converters; +[RequiresUnreferencedCode(MessageStrings.SerializationUnreferencedCodeMessage)] +[RequiresDynamicCode(MessageStrings.SerializationRequiresDynamicCodeMessage)] public class TypedJsonPatchDocumentConverter : JsonConverterFactory { public override bool CanConvert(Type typeToConvert) @@ -20,12 +17,12 @@ public override bool CanConvert(Type typeToConvert) public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options) { var modelType = typeToConvert.GetGenericArguments()[0]; -#pragma warning disable IL2055 // Either the type on which the MakeGenericType is called can't be statically determined, or the type parameters to be used for generic arguments can't be statically determined. var conveterType = typeof(TypedJsonPatchDocumentConverterInner<>).MakeGenericType(modelType); -#pragma warning restore IL2055 // Either the type on which the MakeGenericType is called can't be statically determined, or the type parameters to be used for generic arguments can't be statically determined. return (JsonConverter?)Activator.CreateInstance(conveterType); } + [RequiresUnreferencedCode(MessageStrings.SerializationUnreferencedCodeMessage)] + [RequiresDynamicCode(MessageStrings.SerializationRequiresDynamicCodeMessage)] public class TypedJsonPatchDocumentConverterInner<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)] T> : JsonConverter> where T : class { /// @@ -33,9 +30,7 @@ public class TypedJsonPatchDocumentConverterInner<[DynamicallyAccessedMembers(Dy { if (reader.TokenType == JsonTokenType.Null) return default; -#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code var ops = JsonSerializer.Deserialize>(ref reader, options); -#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code var operations = ops?.Select(o => new Operation { path = o.path, @@ -51,9 +46,7 @@ public override void Write(Utf8JsonWriter writer, JsonPatchDocument value, Js { // we write an array of the operations var ops = value.Operations.Select(o => (Operation)o).ToList(); -#pragma warning disable IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code JsonSerializer.Serialize(writer, ops, options); -#pragma warning restore IL2026 // Members annotated with 'RequiresUnreferencedCodeAttribute' require dynamic access otherwise can break functionality when trimming application code } } } diff --git a/src/Tingle.Extensions.JsonPatch/MessageStrings.cs b/src/Tingle.Extensions.JsonPatch/MessageStrings.cs new file mode 100644 index 00000000..ec26f422 --- /dev/null +++ b/src/Tingle.Extensions.JsonPatch/MessageStrings.cs @@ -0,0 +1,7 @@ +namespace Tingle.Extensions.JsonPatch; + +internal class MessageStrings +{ + public const string SerializationUnreferencedCodeMessage = "JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved."; + public const string SerializationRequiresDynamicCodeMessage = "JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications."; +} diff --git a/src/Tingle.Extensions.MongoDB/MongoDbContextOptions.cs b/src/Tingle.Extensions.MongoDB/MongoDbContextOptions.cs index 26fb71f8..194c47fc 100644 --- a/src/Tingle.Extensions.MongoDB/MongoDbContextOptions.cs +++ b/src/Tingle.Extensions.MongoDB/MongoDbContextOptions.cs @@ -244,7 +244,7 @@ public MongoDbContextOptionsBuilder() : this(new MongoDbContextOptions /// /// The options to use for instrumentation. /// - public new virtual MongoDbContextOptionsBuilder UseMongoConnectionString(string connectionString, InstrumentationOptions? instrumentationOptions=null) + public new virtual MongoDbContextOptionsBuilder UseMongoConnectionString(string connectionString, InstrumentationOptions? instrumentationOptions = null) => (MongoDbContextOptionsBuilder)base.UseMongoConnectionString(connectionString, instrumentationOptions); /// diff --git a/src/Tingle.Extensions.Primitives/Converters/EnumConverterHelper.cs b/src/Tingle.Extensions.Primitives/Converters/EnumConverterHelper.cs index f1779fca..a2bbfc4d 100644 --- a/src/Tingle.Extensions.Primitives/Converters/EnumConverterHelper.cs +++ b/src/Tingle.Extensions.Primitives/Converters/EnumConverterHelper.cs @@ -42,8 +42,8 @@ public EnumConverterHelper(JsonNamingPolicy? namingPolicy, bool allowIntegerValu enumTypeCode = Type.GetTypeCode(type); isFlags = type.IsDefined(typeof(FlagsAttribute), true); - var names = type.GetEnumNames(); - var builtInValues = type.GetEnumValues(); + var names = Enum.GetNames(); + var builtInValues = Enum.GetValues(); int numberOfNames = names.Length;