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

Loop referencing exceptions only with .Net Core 2.2 #2066

Open
Anthony-Breneliere opened this issue May 8, 2019 · 1 comment
Open

Loop referencing exceptions only with .Net Core 2.2 #2066

Anthony-Breneliere opened this issue May 8, 2019 · 1 comment

Comments

@Anthony-Breneliere
Copy link

Anthony-Breneliere commented May 8, 2019

I have got a loop referencing exception with .Net Core 2.2 but not Framework .NET 4.72, when serializing mocked objects with moq. Shouldn't the behavior be the same ? Here is the code to reproduce the exception:

using Moq;
using System;
using System.Runtime.Serialization;

namespace ConsoleApp1
{
    public interface IAction
    {
        string Description { get; }
        object Argument { get; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            var actionMock = new Mock<IAction>().SetupAllProperties();
            // actionMock.As<ISerializable>();
            Console.WriteLine(Newtonsoft.Json.JsonConvert.SerializeObject(actionMock.Object));
        }
    }
}

Result: Newtonsoft.Json.JsonSerializationException: 'Self referencing loop detected for property 'Object' with type 'Castle.Proxies.IActionProxy'. Path 'Mock'.'

With .Net Framework the object actionMock.Object is properly serialized.

Moq is 4.10.1 and Newtonsoft.Json 12.0.2

@dbc2
Copy link

dbc2 commented May 9, 2019

Json.NET's implementation of self-referencing loop detection depends on the object.Equals() implementations of the underlying objects. For details see:

If the implementations of object.Equals() for mocked objects differ between .Net Core 2.2 and
.NET Framework 4.72, you'll get different results for reference loop detection.

Using your own equality comparer on both frameworks, e.g. ObjectReferenceEqualityComparer<object> from IEqualityComparer<T> that uses ReferenceEquals, may make the two frameworks behave consistently.

Also, did you see How to mock (with Moq) an interface that is serialized by Newtonsoft.json?? The error looks to be identical.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants