diff --git a/src/Npgsql/Internal/ResolverFactories/JsonDynamicTypeInfoResolverFactory.cs b/src/Npgsql/Internal/ResolverFactories/JsonDynamicTypeInfoResolverFactory.cs index a5ec6da2ed..18cba7e5b1 100644 --- a/src/Npgsql/Internal/ResolverFactories/JsonDynamicTypeInfoResolverFactory.cs +++ b/src/Npgsql/Internal/ResolverFactories/JsonDynamicTypeInfoResolverFactory.cs @@ -28,28 +28,33 @@ public JsonDynamicTypeInfoResolverFactory(Type[]? jsonbClrTypes = null, Type[]? public override IPgTypeInfoResolver CreateResolver() => new Resolver(_jsonbClrTypes, _jsonClrTypes, _serializerOptions); public override IPgTypeInfoResolver CreateArrayResolver() => new ArrayResolver(_jsonbClrTypes, _jsonClrTypes, _serializerOptions); - public static void ThrowIfUnsupported(Type? type, DataTypeName? dataTypeName, PgSerializerOptions options) + // Split into a nested class to avoid erroneous trimming/AOT warnings because the JsonDynamicTypeInfoResolverFactory is marked as incompatible. + internal static class Support { - if (dataTypeName is { SchemaSpan: "pg_catalog", UnqualifiedNameSpan: "json" or "_json" or "jsonb" or "_jsonb" }) - throw new NotSupportedException( - string.Format( - NpgsqlStrings.DynamicJsonNotEnabled, - type is null || type == typeof(object) ? "" : type.Name, - nameof(NpgsqlSlimDataSourceBuilder.EnableDynamicJson), - typeof(TBuilder).Name)); + public static void ThrowIfUnsupported(Type? type, DataTypeName? dataTypeName) + { + if (dataTypeName is { SchemaSpan: "pg_catalog", UnqualifiedNameSpan: "json" or "_json" or "jsonb" or "_jsonb" }) + throw new NotSupportedException( + string.Format( + NpgsqlStrings.DynamicJsonNotEnabled, + type is null || type == typeof(object) ? "" : type.Name, + nameof(NpgsqlSlimDataSourceBuilder.EnableDynamicJson), + typeof(TBuilder).Name)); + } } + [RequiresUnreferencedCode("Json serializer may perform reflection on trimmed types.")] [RequiresDynamicCode("Serializing arbitrary types to json can require creating new generic types or methods, which requires creating code at runtime. This may not work when AOT compiling.")] class Resolver : DynamicTypeInfoResolver, IPgTypeInfoResolver { JsonSerializerOptions? _serializerOptions; JsonSerializerOptions SerializerOptions - #if NET7_0_OR_GREATER +#if NET7_0_OR_GREATER => _serializerOptions ??= JsonSerializerOptions.Default; - #else +#else => _serializerOptions ??= new(); - #endif +#endif readonly Type[] _jsonbClrTypes; readonly Type[] _jsonClrTypes; @@ -182,4 +187,3 @@ static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings, } } } - diff --git a/src/Npgsql/Internal/ResolverFactories/UnsupportedTypeInfoResolver.cs b/src/Npgsql/Internal/ResolverFactories/UnsupportedTypeInfoResolver.cs index 45d4568a26..2d47f86807 100644 --- a/src/Npgsql/Internal/ResolverFactories/UnsupportedTypeInfoResolver.cs +++ b/src/Npgsql/Internal/ResolverFactories/UnsupportedTypeInfoResolver.cs @@ -17,10 +17,7 @@ sealed class UnsupportedTypeInfoResolver : IPgTypeInfoResolver FullTextSearchTypeInfoResolverFactory.ThrowIfUnsupported(type, dataTypeName, options); LTreeTypeInfoResolverFactory.ThrowIfUnsupported(type, dataTypeName, options); - // The compiler can't see that these method(s) are completely safe, other methods force the attributes on the type(s). -#pragma warning disable IL3050, IL2026 - JsonDynamicTypeInfoResolverFactory.ThrowIfUnsupported(type, dataTypeName, options); -#pragma warning restore IL3050, IL2026 + JsonDynamicTypeInfoResolverFactory.Support.ThrowIfUnsupported(type, dataTypeName); switch (dataTypeName is null ? null : options.DatabaseInfo.GetPostgresType(dataTypeName.GetValueOrDefault())) { diff --git a/src/Npgsql/NpgsqlConnectionStringBuilder.cs b/src/Npgsql/NpgsqlConnectionStringBuilder.cs index 166a93ea68..88f3043fc6 100644 --- a/src/Npgsql/NpgsqlConnectionStringBuilder.cs +++ b/src/Npgsql/NpgsqlConnectionStringBuilder.cs @@ -15,6 +15,10 @@ namespace Npgsql; /// Provides a simple way to create and manage the contents of connection strings used by /// the class. /// +[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2112:ReflectionToRequiresUnreferencedCode", + Justification = "Suppressing the same warnings as suppressed in the base DbConnectionStringBuilder. See https://github.com/dotnet/runtime/issues/97057")] +[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2113:ReflectionToRequiresUnreferencedCode", + Justification = "Suppressing the same warnings as suppressed in the base DbConnectionStringBuilder. See https://github.com/dotnet/runtime/issues/97057")] public sealed partial class NpgsqlConnectionStringBuilder : DbConnectionStringBuilder, IDictionary { #region Fields