runtime: Deserializing inherited class throws exception
Description
Deserializing an inherited F# class throws an exception:
Reproduction Steps
type Animal() = class end
type Foo() =
inherit Animal()
member val Name = "init" with get, set
let js = """{"Name":"Ed"}"""
let x = System.Text.Json.JsonSerializer.Deserialize<Foo>(js) // zzz not good
Expected behavior
No exception
Actual behavior
AmbiguousMatchException: Multiple custom attributes of the same type 'Microsoft.FSharp.Core.CompilationMappingAttribute' found.
at System.Attribute.GetCustomAttribute(MemberInfo element, Type attributeType, Boolean inherit)
at System.Text.Json.Serialization.Metadata.FSharpCoreReflectionProxy.IsFSharpType(Type type)
at System.Text.Json.Serialization.Converters.FSharpTypeConverterFactory.CanConvert(Type typeToConvert)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetBuiltInConverter(Type typeToConvert)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetConverterForType(Type typeToConvert, JsonSerializerOptions options, Boolean resolveJsonConverterAttribute)
at System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver.GetTypeInfo(Type type, JsonSerializerOptions options)
at System.Text.Json.JsonSerializerOptions.GetTypeInfoNoCaching(Type type)
at System.Text.Json.JsonSerializerOptions.CachingContext.CreateCacheEntry(Type type, CachingContext context)
--- End of stack trace from previous location ---
at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type type, Boolean ensureConfigured, Nullable`1 ensureNotNull, Boolean resolveIfMutable, Boolean fallBackToNearestAncestorType)
at System.Text.Json.JsonSerializerOptions.GetTypeInfoForRootType(Type type, Boolean fallBackToNearestAncestorType)
at System.Text.Json.JsonSerializer.GetTypeInfo[T](JsonSerializerOptions options)
at System.Text.Json.JsonSerializer.Deserialize[TValue](String json, JsonSerializerOptions options)
:
--- End of stack trace from previous location ---
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 23 (20 by maintainers)
Side topic: can we add more tests for BCL integration with F# to help capturing these?
It makes sense to try to improve the STJ code for .NET 10. In terms of backporting that change to .NET 8 and .NET 9 we need to be able to justify that it is not practical to revert the FSharp.Core change that triggered the issue, is that something we can do?
I will say though that I’m very surprised such a change made its way into a servicing release. We should make sure that this does not happen in the future.
The change is bound to the version of FSharp.Core at runtime. If not using a pinned down version, it means that whoever builds their product with SDK 9.0.3 or newer will get that new FSharp.Core.
My proposition:
Right. This worked fine in .net 9.0.2 but failed in 9.0.3.
BTW: sharplab.io is neat!