-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
System.Text.Json polymorphism serialized with ReferenceHandler.IgnoreCycles #111790
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis |
Oh wow. Thanks for reporting this. |
Don't have the ability to test the code right now, but it seems to me the problem lies here: runtime/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonConverterOfT.cs Lines 385 to 390 in 717d3b1
The object in the array enters This might fix it if (!isContinuation && options.ReferenceHandlingStrategy != JsonKnownReferenceHandler.Unspecified &&
TryHandleSerializedObjectReference(writer, value, options, polymorphicConverter, ref state))
{
+ if (polymorphicConverter is not null)
+ {
+ state.Current.ExitPolymorphicConverter(true);
+ }
// The reference handler wrote reference metadata, serialization complete.
return true;
} Just guessing though :) |
I won't be able to take a look at this soon, so if you have a good idea on how to fix this may I suggest submitting a PR? |
Clear the polymorphic state after ignoring a reference to prevent subsequent object writes from inheriting it.
Description
Under very specific conditions (using
ReferenceHandler.IgnoreCycles
,JsonDerivedType
, and objects contained within an array), the serializer incorrectly applies the discriminator to a unrelated object that is serialized after.After serializing a polymorphic object within an array - which is removed because its a circular reference - the serializer adds the discriminator to the next coming object.
Reproduction Steps
Expected behavior
Expected output:
Actual behavior
Regression?
No response
Known Workarounds
No response
Configuration
Tested on .NET version
8.0.112
and9.0.101
Other information
No response
The text was updated successfully, but these errors were encountered: