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

DataContractJsonSerializer can't serialize contract with ExtensionDataObject #55270

Closed
sivukhin opened this issue Jul 7, 2021 · 2 comments · Fixed by #57533
Closed

DataContractJsonSerializer can't serialize contract with ExtensionDataObject #55270

sivukhin opened this issue Jul 7, 2021 · 2 comments · Fixed by #57533
Assignees
Milestone

Comments

@sivukhin
Copy link

sivukhin commented Jul 7, 2021

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!).

@dotnet-issue-labeler dotnet-issue-labeler bot added area-Serialization untriaged New issue has not been triaged by the area owner labels Jul 7, 2021
@sivukhin
Copy link
Author

Should I mention someone to draw attention to the issue?

@HongGit HongGit added JitUntriaged CLR JIT issues needing additional triage untriaged New issue has not been triaged by the area owner and removed untriaged New issue has not been triaged by the area owner JitUntriaged CLR JIT issues needing additional triage labels Jul 22, 2021
@StephenMolloy
Copy link
Member

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

@StephenMolloy StephenMolloy removed the untriaged New issue has not been triaged by the area owner label Jul 31, 2021
@StephenMolloy StephenMolloy self-assigned this Jul 31, 2021
@StephenMolloy StephenMolloy added this to the 6.0.0 milestone Jul 31, 2021
StephenMolloy added a commit to StephenMolloy/runtime that referenced this issue Aug 16, 2021
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Aug 16, 2021
StephenMolloy added a commit that referenced this issue Aug 21, 2021
* Align DCJS with 4.8 version. This work addresses #55270.
@ghost ghost removed the in-pr There is an active PR which will close this issue when it is merged label Aug 21, 2021
ViktorHofer pushed a commit that referenced this issue Aug 24, 2021
* Align DCJS with 4.8 version. This work addresses #55270.

* Close() nits.

* Remove some extraneous trimmer attributes.

* Another trimmer nit.

Co-authored-by: Steve Molloy <[email protected]>
@ghost ghost locked as resolved and limited conversation to collaborators Sep 20, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants