You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 when JsonSerializerOptions.GetConverterFromAttribute tries to call Activator.CreateInstance(converterType) (where converterType = typeof(JsonStringEnumConverter)). This was originally reported at dotnet/aspnetcore#19086
For 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 in JsonSerializerOptions.DefaultSimpleConverters to include an instantiation of JsonStringEnumConverter (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.
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 [DynamicallyAccessedMembers(MemberKinds.DefaultConstructor)] to JsonConverterAttribute, which would tell the linker to keep the default constructor on any type passed into JsonConverter.
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").
Activity
stephentoub commentedon Apr 3, 2020
cc: @eerhardt
eerhardt commentedon Apr 3, 2020
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
[DynamicallyAccessedMembers(MemberKinds.DefaultConstructor)]toJsonConverterAttribute, which would tell the linker to keep the default constructor on any type passed intoJsonConverter./cc @vitek-karas @MichalStrehovsky @sbomer
MichalStrehovsky commentedon Apr 3, 2020
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").
stephentoub commentedon Apr 3, 2020
For reference:
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Collection/IEnumerableConverterFactory.cs
Lines 28 to 46 in d59f143
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Object/ObjectConverterFactory.cs
Lines 25 to 27 in 4a80495
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/EnumConverterFactory.cs
Lines 21 to 23 in 0e0e852
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/Converters/Value/KeyValuePairConverterFactory.cs
Line 22 in 0e0e852
runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonStringEnumConverter.cs
Lines 57 to 59 in 0e0e852