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

BUGFIX - Reference loop checking (check reference, not equality) #214

Closed
wants to merge 1 commit into from
Closed
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 @@ -55,6 +55,23 @@ public void ReferenceLoopHandlingTest()
Assert.AreEqual(ReferenceLoopHandling.Ignore, attribute.ReferenceLoopHandling);
}

[Test]
public void HandleObjectReferenceLoopBadEquality()
{
ReferenceLoopHandlingObjectBadEquality o = new ReferenceLoopHandlingObjectBadEquality();
o.Value = new ReferenceLoopHandlingObjectBadEquality();

string json = JsonConvert.SerializeObject(o, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Error
});
Assert.AreEqual(@"{
""Value"": {
""Value"": null
}
}", json);
}

[Test]
public void IgnoreObjectReferenceLoop()
{
Expand Down Expand Up @@ -306,6 +323,22 @@ public class ReferenceLoopHandlingDictionary : Dictionary<string, ReferenceLoopH
{
}

[JsonObject(ItemReferenceLoopHandling = ReferenceLoopHandling.Error)]
public class ReferenceLoopHandlingObjectBadEquality
{
public override bool Equals(object obj)
{
return true;
}

public override int GetHashCode()
{
return 0;
}

public ReferenceLoopHandlingObjectBadEquality Value { get; set; }
}

[JsonObject(ItemReferenceLoopHandling = ReferenceLoopHandling.Ignore)]
public class ReferenceLoopHandlingObjectContainerAttribute
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ private bool CheckForCircularReference(JsonWriter writer, object value, JsonProp
if (referenceLoopHandling == null && containerContract != null)
referenceLoopHandling = containerContract.ItemReferenceLoopHandling;

if (_serializeStack.IndexOf(value) != -1)
if (_serializeStack.IndexOf(o => ReferenceEquals(o,value)) != -1)
{
string message = "Self referencing loop detected";
if (property != null)
Expand Down