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

Attribute foo used in [JsonPolymorphic(TypeDiscriminatorPropertyName = "foo")] does not get deserialized #79933

Closed
sebader opened this issue Dec 23, 2022 · 4 comments

Comments

@sebader
Copy link

sebader commented Dec 23, 2022

Description

I'm deserializing the followed JSON structure:

"data": {
        "resultType": "vector",  // or: "matrix"
        "result": [
            {
                // ...
            }
        ]
    }

For this I have build my polymorphic objects as follows:

    [JsonPolymorphic(TypeDiscriminatorPropertyName = "resultType")]
    [JsonDerivedType(typeof(Data))]
    [JsonDerivedType(typeof(MatrixData), typeDiscriminator: "matrix")]
    [JsonDerivedType(typeof(VectorData), typeDiscriminator: "vector")]
    public class Data
    {
        [JsonPropertyName("resultType")]
        public string resultType { get; set; }
    }

    public class VectorData : Data
    {
// ...
    }

    public class MatrixData : Data
    {
// ...
    }

The deserialization including polymorphism all works great, what does not work is the property resultType. It is always null.

So, I'm wondering if its use in [JsonPolymorphic(TypeDiscriminatorPropertyName = "resultType")] is messing with the deserialization of that field?!

Reproduction Steps

See above

Expected behavior

Any field that is being used for JsonPolymorphic should still be able to be deserialized

Actual behavior

Field is null
image

Regression?

No response

Known Workarounds

No response

Configuration

.NET SDK:
Version: 7.0.101
Commit: bb24aafa11

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22623
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.101\

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Dec 23, 2022
@ghost
Copy link

ghost commented Dec 23, 2022

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

Issue Details

Description

I'm deserializing the followed JSON structure:

"data": {
        "resultType": "vector",  // or: "matrix"
        "result": [
            {
                // ...
            }
        ]
    }

For this I have build my polymorphic objects as follows:

    [JsonPolymorphic(TypeDiscriminatorPropertyName = "resultType")]
    [JsonDerivedType(typeof(Data))]
    [JsonDerivedType(typeof(MatrixData), typeDiscriminator: "matrix")]
    [JsonDerivedType(typeof(VectorData), typeDiscriminator: "vector")]
    public class Data
    {
        [JsonPropertyName("resultType")]
        public string resultType { get; set; }
    }

    public class VectorData : Data
    {
// ...
    }

    public class MatrixData : Data
    {
// ...
    }

The deserialization including polymorphism all works great, what does not work is the property resultType. It is always null.

So, I'm wondering if its use in [JsonPolymorphic(TypeDiscriminatorPropertyName = "resultType")] is messing with the deserialization of that field?!

Reproduction Steps

See above

Expected behavior

Any field that is being used for JsonPolymorphic should still be able to be deserialized

Actual behavior

Field is null
image

Regression?

No response

Known Workarounds

No response

Configuration

.NET SDK:
Version: 7.0.101
Commit: bb24aafa11

Runtime Environment:
OS Name: Windows
OS Version: 10.0.22623
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\7.0.101\

Other information

No response

Author: sebader
Assignees: -
Labels:

area-System.Text.Json

Milestone: -

@pinkfloydx33
Copy link

This came up recently but I cannot find the issue. We had a similar problem. The fact it has a setter implies you can change the underlying type by changing the value, which wouldn't make sense. We were using the property for introspection without a type-check and originally resolved it by putting JsonIgnore on the property (to prevent double serialization) and turned it into an abstract, get-only auto-property that returned the expected value by type, which is a variation on what was recommended by dotnet folks.

We ended up just dropping the property (which was entirely a convenience anyways) and just perform the type check ourself now.

@sebader
Copy link
Author

sebader commented Dec 23, 2022

yeah its not a super hard requirement for me to have this, either, but it just felt like a bug that it's always null. If that is indeed the desired behavior, it should at least be documented on JsonPolymorphic

@layomia layomia added bug and removed untriaged New issue has not been triaged by the area owner labels Feb 7, 2023
@layomia layomia added this to the Future milestone Feb 7, 2023
Maximys pushed a commit to Maximys/runtime that referenced this issue Aug 21, 2023
Maximys pushed a commit to Maximys/runtime that referenced this issue Aug 21, 2023
Maximys pushed a commit to Maximys/runtime that referenced this issue Aug 21, 2023
@eiriktsarpalis
Copy link
Member

This is by design, the type discriminator identifies types and so cannot be mapped to a run time property value.

@eiriktsarpalis eiriktsarpalis closed this as not planned Won't fix, can't repro, duplicate, stale Nov 22, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Dec 23, 2023
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

4 participants