Skip to content

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

Closed as not planned

Description

@sebader

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

Activity

  1. ghost added
    untriaged
    on Dec 23, 2022
  2. pinkfloydx33 commented on Dec 23, 2022

    @pinkfloydx33

    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.

  3. sebader commented on Dec 23, 2022

    @sebader
    Author

    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

  4. added and removed
    untriaged
    on Feb 7, 2023
  5. added this to the Future milestone on Feb 7, 2023
  6. eiriktsarpalis commented on Nov 22, 2023

    @eiriktsarpalis
    Member

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

  7. locked and limited conversation to collaborators on Dec 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions