-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
System.Text.Json.Serialization.JsonStringEnumConverter isn't linker-safe #34449
Comments
cc: @eerhardt |
Another potential way to fix this is to add the correct dataflow analysis annotations (dotnet/linker#988) to JsonConverter. Using the current names in that proposal, all we would need to add |
Yes, we'll want to solve this through annotations - the fact that the other converters have their constructor rooted from elsewhere is hardly a contract one could rely on: they just happen to work right now. We are working on a linking solution that would let us guarantee things work after linking by construction (as opposed to "working thanks to luck"). |
For reference: Lines 28 to 46 in d59f143
Lines 25 to 27 in 4a80495
Lines 21 to 23 in 0e0e852
Line 22 in 0e0e852
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs Lines 57 to 59 in 0e0e852
|
Most other converters in S.T.J. (as far as I know) are safe because their default constructors are referenced statically. However, if you use
JsonStringEnumConverter
like this:then there is no static reference to
JsonStringEnumConverter
's default constructor, so the linker will strip it out. Then at runtime it will fail whenJsonSerializerOptions.GetConverterFromAttribute
tries to callActivator.CreateInstance(converterType)
(whereconverterType = typeof(JsonStringEnumConverter)
). This was originally reported at dotnet/aspnetcore#19086For Blazor 3.2.0, we'll put in a workaround into Blazor's built-in linker config. However it would make sense to fix the underlying issue in S.T.J. in the longer term.
Suggested fix
Either
System.Text.Json.dll
should embed its own linker config file preserving the constructors for all converter types, or consider extending the set of types listed inJsonSerializerOptions.DefaultSimpleConverters
to include an instantiation ofJsonStringEnumConverter
(or have any other static reference to its constructor).This applies to all converter types that may be used at runtime. The only one I know of that's causing a problem is
JsonStringEnumConverter
, but I didn't exhaustively check that all the others are linker-safe too.The text was updated successfully, but these errors were encountered: