Skip to content
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 Polymorphism Type Discriminator Not Deserialized #102455

Closed
9ParsonsB opened this issue May 20, 2024 · 2 comments
Closed

System.Text.Json Polymorphism Type Discriminator Not Deserialized #102455

9ParsonsB opened this issue May 20, 2024 · 2 comments

Comments

@9ParsonsB
Copy link

Description

When deserializing a polymorphic type with TypeDiscriminatorPropertyName set to an existing Property name, that Property is no longer deserialized

Reproduction Steps

[JsonPolymorphic(TypeDiscriminatorPropertyName = "event")]
[JsonDerivedType(typeof(ChildEvent), "child")]
public abstract class EventBase
{
    [JsonPropertyName("timestamp")]
    public DateTimeOffset Timestamp { get; init; }
    
    [JsonPropertyName("event")]
    public string Event { get; set; }
}

public class ChildEvent : EventBase;

var json = """
           { "event": "child", "timestamp":"2024-05-20T12:36:10Z" }
           """;

var obj = JsonSerializer.Derserialize<EventBase>(json);

Console.WriteLine(obj.Event); // ""

// obj.Event = null;

Expected behavior

TypeDiscriminator Property is still deserialized

Actual behavior

TypeDiscriminator Property is always null/default

Regression?

No response

Known Workarounds

No response

Configuration

.NET 9.0.100-preview.3.24204.13
Arch Linux 6.8.9-arch1-1 x86-64

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label May 20, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@eiriktsarpalis
Copy link
Member

Duplicate of #72170 (comment). The runtime type of a property cannot be used to influence the discriminator which is a type-level mapping.

@eiriktsarpalis eiriktsarpalis closed this as not planned Won't fix, can't repro, duplicate, stale May 20, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label May 20, 2024
@github-actions github-actions bot locked and limited conversation to collaborators Jun 21, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants