Skip to content

Commit

Permalink
Address trimming/AOT warnings
Browse files Browse the repository at this point in the history
- Split ThrowIfUnsupported out into a nested class of JsonDynamicTypeInfoResolverFactory to avoid erroneous warnings.
- Suppress warnings on NpgsqlConnectionStringBuilder to match base DbConnectionStringBuilder suppressions. See dotnet/runtime#97057 for an explanation of why these warnings happen.

Fix npgsql#5577
  • Loading branch information
eerhardt committed Feb 14, 2024
1 parent d137ac6 commit 2c8ec52
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<TBuilder>(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) ? "<unknown>" : type.Name,
nameof(NpgsqlSlimDataSourceBuilder.EnableDynamicJson),
typeof(TBuilder).Name));
public static void ThrowIfUnsupported<TBuilder>(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) ? "<unknown>" : 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;
Expand Down Expand Up @@ -182,4 +187,3 @@ static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings,
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ sealed class UnsupportedTypeInfoResolver<TBuilder> : IPgTypeInfoResolver
FullTextSearchTypeInfoResolverFactory.ThrowIfUnsupported<TBuilder>(type, dataTypeName, options);
LTreeTypeInfoResolverFactory.ThrowIfUnsupported<TBuilder>(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<TBuilder>(type, dataTypeName, options);
#pragma warning restore IL3050, IL2026
JsonDynamicTypeInfoResolverFactory.Support.ThrowIfUnsupported<TBuilder>(type, dataTypeName);

switch (dataTypeName is null ? null : options.DatabaseInfo.GetPostgresType(dataTypeName.GetValueOrDefault()))
{
Expand Down
4 changes: 4 additions & 0 deletions src/Npgsql/NpgsqlConnectionStringBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ namespace Npgsql;
/// Provides a simple way to create and manage the contents of connection strings used by
/// the <see cref="NpgsqlConnection"/> class.
/// </summary>
[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<string, object?>
{
#region Fields
Expand Down

0 comments on commit 2c8ec52

Please sign in to comment.