Skip to content

[S.T.Json] EnumConverterFactory IL3050 when NativeAOT publish with JsonSerializerIsReflectionEnabledByDefault=true #109177

@jkurdek

Description

@jkurdek

Description:

When publishing a .NET application using System.Text.Json with NativeAOT, setting JsonSerializerIsReflectionEnabledByDefault to true triggers the following warning during compilation:

/_/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverterFactory.cs(33): 
AOT analysis warning IL3050: System.Text.Json.Serialization.Converters.EnumConverterFactory.CreateConverter(Type,JsonSerializerOptions): 
Using member 'System.Text.Json.Serialization.Converters.EnumConverterFactory.Create(Type,EnumConverterOptions,JsonNamingPolicy,JsonSerializerOptions)' which has 'RequiresDynamicCodeAttribute' can break functionality when AOT compiling. 
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.

Repro:

using System.Text.Json;

class Program
{
    static void Main()
    {
        var something = JsonSerializer.Deserialize<String>("{}"); // Any call to JsonSerializer.Deserialize will do
    }
}

Enable AOT and JsonSerializerIsReflectionEnabledByDefault

<PublishAot>true</PublishAot> 
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
dotnet publish -c Release -p:TrimmerSingleWarn=false

Possible cause:

[SuppressMessage("AOT", "IL3050:Calling members annotated with 'RequiresDynamicCodeAttribute' may break functionality when AOT compiling.",
Justification = "The constructor has been annotated with RequiredDynamicCodeAttribute.")]
public override JsonConverter CreateConverter(Type type, JsonSerializerOptions options)
{
Debug.Assert(CanConvert(type));
return Create(type, EnumConverterOptions.AllowNumbers, namingPolicy: null, options);
}
should be an UnconditionalSuppressMessage ?

Context:

The issue was found in MAUI - dotnet/maui#25077

Metadata

Metadata

Labels

area-System.Text.Jsonin-prThere is an active PR which will close this issue when it is merged

Type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions