Skip to content

System.Text.Json: Fix polymorphic state bug when using ReferenceHandler.IgnoreCycles #111808

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

Merged
merged 5 commits into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@ internal bool TryHandleSerializedObjectReference(Utf8JsonWriter writer, object v
if (resolver.ContainsReferenceForCycleDetection(value))
{
writer.WriteNullValue();

if (polymorphicConverter is not null)
{
// Clear out any polymorphic state.
state.PolymorphicTypeDiscriminator = null;
state.PolymorphicTypeResolver = null;
}
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,48 @@ public async Task IgnoreCycles_BoxedValueShouldNotBeIgnored()
await Test_Serialize_And_SerializeAsync_Contains(root, expectedSubstring: @"""DayOfBirth"":15", expectedTimes: 2, s_optionsIgnoreCycles);
}

[Fact]
public async Task IgnoreCycles_DerivedType_InArray()
{
var worker = new OfficeWorker
{
Office = new Office
{
Dummy = new()
}
};

worker.Office.Staff = [worker, new RemoteWorker()];

await Test_Serialize_And_SerializeAsync(worker, """{"Office":{"Staff":[null,{"$type":"remote"}],"Dummy":{}}}""", s_optionsIgnoreCycles);

worker.Office.Staff = [worker];

await Test_Serialize_And_SerializeAsync(worker, """{"Office":{"Staff":[null],"Dummy":{}}}""", s_optionsIgnoreCycles);
}

[JsonDerivedType(typeof(OfficeWorker), "office")]
[JsonDerivedType(typeof(RemoteWorker), "remote")]
public abstract class EmployeeLocation
{
}

public class OfficeWorker : EmployeeLocation
{
public Office Office { get; set; }
}

public class RemoteWorker : EmployeeLocation
{
}

public class Office
{
public EmployeeLocation[] Staff { get; set; }

public EmptyClass Dummy { get; set; }
}

[Fact]
public async Task CycleDetectionStatePersistsAcrossContinuations()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,49 @@ public async Task CustomHashCode()
// otherwise objects would not be correctly identified when searching for them in the dictionary.
Assert.Same(listCopy[0], listCopy[1]);
}

[Fact]
public async Task Preserve_DerivedType_InArray()
{
var worker = new OfficeWorker
{
Office = new Office
{
Dummy = new()
}
};

worker.Office.Staff = [worker, new RemoteWorker()];

string json = await Serializer.SerializeWrapper(worker, s_serializerOptionsPreserve);
Assert.Equal("""{"$id":"1","Office":{"$id":"2","Staff":[{"$ref":"1"},{"$id":"3","$type":"remote"}],"Dummy":{"$id":"4"}}}""", json);

worker.Office.Staff = [worker];

json = await Serializer.SerializeWrapper(worker, s_serializerOptionsPreserve);
Assert.Equal("""{"$id":"1","Office":{"$id":"2","Staff":[{"$ref":"1"}],"Dummy":{"$id":"3"}}}""", json);
}

[JsonDerivedType(typeof(OfficeWorker), "office")]
[JsonDerivedType(typeof(RemoteWorker), "remote")]
public abstract class EmployeeLocation
{
}

public class OfficeWorker : EmployeeLocation
{
public Office Office { get; set; }
}

public class RemoteWorker : EmployeeLocation
{
}

public class Office
{
public EmployeeLocation[] Staff { get; set; }

public EmptyClass Dummy { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ public ReferenceHandlerTests_IgnoreCycles_Metadata(JsonSerializerWrapper seriali
[JsonSerializable(typeof(TreeNode<List<string>>))]
[JsonSerializable(typeof(TreeNode<object>))]
[JsonSerializable(typeof(int))]
[JsonSerializable(typeof(EmployeeLocation))]
[JsonSerializable(typeof(EmployeeLocation[]))]
[JsonSerializable(typeof(OfficeWorker))]
[JsonSerializable(typeof(Office))]
[JsonSerializable(typeof(RemoteWorker))]
internal sealed partial class ReferenceHandlerTests_IgnoreCyclesContext_Metadata : JsonSerializerContext
{
}
Expand Down Expand Up @@ -172,6 +177,11 @@ public ReferenceHandlerTests_IgnoreCycles_Default(JsonSerializerWrapper serializ
[JsonSerializable(typeof(TreeNode<List<string>>))]
[JsonSerializable(typeof(TreeNode<object>))]
[JsonSerializable(typeof(int))]
[JsonSerializable(typeof(EmployeeLocation))]
[JsonSerializable(typeof(EmployeeLocation[]))]
[JsonSerializable(typeof(OfficeWorker))]
[JsonSerializable(typeof(Office))]
[JsonSerializable(typeof(RemoteWorker))]
internal sealed partial class ReferenceHandlerTests_IgnoreCyclesContext_Default : JsonSerializerContext
{
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ public ReferenceHandlerTests_Metadata(JsonSerializerWrapper serializer)
[JsonSerializable(typeof(ClassWithConflictingIdProperty))]
[JsonSerializable(typeof(ClassWithIgnoredConflictingProperty))]
[JsonSerializable(typeof(ClassWithExtensionDataConflictingProperty))]
[JsonSerializable(typeof(EmployeeLocation))]
[JsonSerializable(typeof(EmployeeLocation[]))]
[JsonSerializable(typeof(OfficeWorker))]
[JsonSerializable(typeof(Office))]
[JsonSerializable(typeof(RemoteWorker))]
internal sealed partial class ReferenceHandlerTestsContext_Metadata : JsonSerializerContext
{
}
Expand Down Expand Up @@ -281,6 +286,11 @@ public ReferenceHandlerTests_Default(JsonSerializerWrapper serializer)
[JsonSerializable(typeof(ClassWithConflictingIdProperty))]
[JsonSerializable(typeof(ClassWithIgnoredConflictingProperty))]
[JsonSerializable(typeof(ClassWithExtensionDataConflictingProperty))]
[JsonSerializable(typeof(EmployeeLocation))]
[JsonSerializable(typeof(EmployeeLocation[]))]
[JsonSerializable(typeof(OfficeWorker))]
[JsonSerializable(typeof(Office))]
[JsonSerializable(typeof(RemoteWorker))]
internal sealed partial class ReferenceHandlerTestsContext_Default : JsonSerializerContext
{
}
Expand Down
Loading