Json deserialization of polymorphic types does not expose the discriminator value
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 45/100
- Issue type
- Feature
- Clarity
- Mostly clear
- Activity status
- Stale
- Tech stack
- csharp
- Domain
- backend-api-design
Research direction
Start by reproducing the shown System.Text.Json polymorphic deserialization case with the Preference model, FallBackToNearestAncestor, IgnoreUnrecognizedTypeDiscriminators, and JsonExtensionData. Compare the resulting extension data with the input discriminator, then determine how the fallback behavior should expose that value and verify it with cases such as the distinct legume and beans payloads.
Written by the indexing model from the issue text.
Description
When using polymorphic types with System.Text.Json and configuring FallBackToNearestAncestor and IgnoreUnrecognizedTypeParameters to true you cannot tell the Json type discriminator of unrecognized objects.
To give a real-world example, I talk to a preferences API which returns multiple types of preferences, so I have a base class/record Preference
[JsonPolymorphic(UnknownDerivedTypeHandling =
JsonUnknownDerivedTypeHandling.FallBackToNearestAncestor,
IgnoreUnrecognizedTypeDiscriminators = true)]
[JsonDerivedType(typeof(......)]
public record Preference
{
[JsonConstructor]
public Preference() {}
[JsonExtensionData]
public Dictionary<string, JsonElement>? ExtensionData {get; set;}
}
The idea being that if the api needs a new type of preference I don't have a derived class for then deserialization will still succeed, falling back to create an instance of Preference, with the unknown properties exposed in the ExtensionData.
Then in parsing a collection of preferences I can use pattern matching to act on the derived types.
However, when deserialization falls back to using the preference class I get everything in the ExtensionData except for the type discriminator value. This makes it impossible to make a reasoned decision about what to do with the data.
If the preferences API added two new types which have the same shape, say
{
"$type": "legume",
"description": "a type of legume"
},
{
"$type": "beans",
"description" : "the best type of legume"
}
then after deserialization I would get is the description in the extension data with no way of telling the types apart. So, you need to expose the type property somewhere.
My suggestion would be if deserialization has fallen back because the type is unknown and there is an extension data property then also insert the type value into that, or, if the fallback class/record has a property of string where the JsonPropertyName value matches the TypeDiscriminatorPropertyName property on the JsonPolymorphic attribute, feed it into that as well.
If you go the property router then during serialization any JsonInclude property which has a JsonPropertyName which matches the TypeDiscriminatorPropertyName property on the JsonPolymorphicattribute could be ignored by default.
- Dominant language
- C#
- Stars
- 18.3k
- Forks
- 5.6k
- PR merge metrics
- PR metrics pending
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from dotnet/runtime
-
agentic-workflows untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
area-System.Reflection blocking-clean-ci-optional Known Build Error os-mac-os-x untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
-
area-CodeGen-coreclr untriaged
Difficulty 1/5 Under an hour Newbie friendliness 92/100
-
agentic-workflows untriaged
Difficulty 1/5 Under an hour Newbie friendliness 78/100
-
area-VM-meta-mono untriaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
-
:watch: Not Triaged 11.0 fundamentals/subsvc
Difficulty 2/5 1-3 hours Newbie friendliness 92/100
dotnet/AspNetCore.Docs#37699 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
SubtitleEdit/subtitleedit#15108 · 1 comment ·
-
area/docs-content Bug pulumi/docs
Difficulty 1/5 1-3 hours Newbie friendliness 94/100
-
Create parent directories only after the containment check in InstallHelper.TryExtractToDirectory Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
PowerShell/PSResourceGet#2056 ·