Skip to content

DataContractJsonSerializer can't serialize contract with ExtensionDataObject #55270

Closed

Description

@sivukhin

Description

If you use DataContractJsonSerializer with ExtensionDataObject and try to serialize data when extension data contains a nested complex object with the single property then the serializer throws an exception because the deserializer incorrectly decides that property of a nested object has the type of array but this assumption breaks serializer invariants.

Consider the following data contract:

[DataContract]
public class ContractGeneric : IExtensibleDataObject
{
    public ExtensionDataObject ExtensionData { get; set; }
}

If you try to deserialize JSON {"item":{"id":0}} to the type ContractGeneric and after that immediately serialize this data back to JSON then you get the exception:

Unhandled exception. System.Runtime.Serialization.SerializationException: 
There was an error serializing the object of type DataContractJsonSerializeExample.ContractGeneric. 
Encountered unexpected element local name 'id' for item in collection. 'item' is the only valid local name for elements in a collection.

You can find a repository with the full example here: https://github.com/Umqra/DataContractJsonSerializeExample
Also, there is a docker image with a console application that reproduces the bug: https://hub.docker.com/r/sivukhin/data-contract-json-serializer-example

Configuration

Runtime: .NET 5.0 but problem also reproduced in netcoreapp2.0
Operation System: Windows
Architecture: x64

Problem

Probably the cause of the problem lies in the automatic type detection for ExtensionDataObject content, more precisely inconsistency in behaviour of deserializer for JSON may happen at the following line:

Here XmlObjectSerializerReadContext tries to determine type of element and in case when object contains only single property the algorithm decide that this is an array (which is wrong!).

Activity

  1. ghost added
    untriaged
    on Jul 7, 2021
  2. sivukhin commented on Jul 13, 2021

    @sivukhin
    Author

    Should I mention someone to draw attention to the issue?

  3. added
    JitUntriaged
    untriaged
    and removed
    untriaged
    JitUntriaged
    on Jul 22, 2021
  4. StephenMolloy commented on Jul 31, 2021

    @StephenMolloy
    Contributor

    Some more information. I can reproduce this exception on .Net 6, but not on .Net 4.8.

  5. removed
    untriaged
    on Jul 31, 2021
  6. added this to the 6.0.0 milestone on Jul 31, 2021
  7. ghost added
    in-pr
    on Aug 16, 2021
  8. ghost removed
    in-pr
    on Aug 21, 2021
  9. ghost locked as resolved and limited conversation to collaborators on Sep 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions