Json deserialization of polymorphic types does not expose the discriminator value

Open
#108,885 5 comments 11 reactions 0 assignees View on GitHub

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

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

api-suggestion area-System.Text.Json

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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.