-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Comments
Should I mention someone to draw attention to the issue? |
Some more information. I can reproduce this exception on .Net 6, but not on .Net 4.8. |
StephenMolloy
added a commit
to StephenMolloy/runtime
that referenced
this issue
Aug 16, 2021
StephenMolloy
added a commit
that referenced
this issue
Aug 21, 2021
* Align DCJS with 4.8 version. This work addresses #55270.
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]>
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Description
If you use
DataContractJsonSerializer
withExtensionDataObject
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:
If you try to deserialize JSON
{"item":{"id":0}}
to the typeContractGeneric
and after that immediately serialize this data back to JSON then you get the exception: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:runtime/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/XmlObjectSerializerReadContext.cs
Line 969 in 57bfe47
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!).The text was updated successfully, but these errors were encountered: