DeepEqual icon indicating copy to clipboard operation
DeepEqual copied to clipboard

StackOverflow exception on object graphs with cycles

Open davisnw opened this issue 11 years ago • 6 comments
trafficstars

In version 0.12.0.0 installed from NuGet, I get a StackOverflowException when an object graph contains cycles.

Sample below:

public class A
{
    public List<B> Bees { get; set; }
}

public class B
{
    public A A { get; set; }
}

public class C
{
    public D D { get; set; }
}

public class D
{
    public C C { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        var a = new A();
        var b = new B { A = a };
        a.Bees = new List<B> { b };

        var a1 = new A();
        var b1 = new B { A = a1 };
        a1.Bees = new List<B> { b1 };

        a1.ShouldDeepEqual(a); //stack overflow exception

        b1.ShouldDeepEqual(b); //stack overflow exception


        var c = new C();
        var d = new D { C = c };
        c.D = d;

        var c1 = new C();
        var d1 = new D { C = c1 };
        c1.D = d1;

        c1.ShouldDeepEqual(c); //stack overflow exception

        d1.ShouldDeepEqual(d); //stack overflow exception
    }
}

davisnw avatar May 21 '14 02:05 davisnw

This is still occurring in the latest version (as of today v1.4) from nuget.

agray avatar May 30 '15 02:05 agray

Looking forward to have it fixed :)

pellared avatar Feb 08 '16 16:02 pellared

Is there a workaround to it? Something as simple as a LimitRecursionDepth method would do.

stachenov avatar Jul 20 '16 14:07 stachenov

This is a common issue with navigation properties (1 - n) in entity framework! If you use this on a entity with said relationship in a unit test, the test spins for a while and then gets aborted!

Currently I have to ignore those properties and then loop through the list of childs and compare them separately...

Please look into this!

rose-a avatar Jan 31 '18 10:01 rose-a

I have worked around it using custom comparisons like this one: https://github.com/asgerhallas/ShouldBeLike/blob/master/ShouldBeLike/CyclesComparison.cs

asgerhallas avatar Nov 27 '20 11:11 asgerhallas

Note: Above comparison is not thread safe and should not be reused between comparisons. Make a new instance for every call to ShouldDeepEqual(). I have also made a thread safe version, but it was quite a bit more complex :)

asgerhallas avatar Mar 12 '21 10:03 asgerhallas

This also happens when using JsonObject from System.Text.Json. Even if you have 2 different objects.

douglasg14b avatar Jan 26 '23 17:01 douglasg14b

i got the same problems

sgf avatar Mar 22 '23 02:03 sgf

Note: Above comparison is not thread safe and should not be reused between comparisons. Make a new instance for every call to ShouldDeepEqual(). I have also made a thread safe version, but it was quite a bit more complex :)

if there any API like IsDeepEqual direct return true/false in you repository ShouldBeLike ?

sgf avatar Mar 22 '23 02:03 sgf

@jamesfoster any update for this issue ?

sgf avatar Mar 22 '23 02:03 sgf

Added ability to ignore circular references in v5.0.0

actual
    .WithDeepEqual(expected)
    .IgnoreCircularReferences()
    .Assert()

jamesfoster avatar Feb 06 '24 16:02 jamesfoster