From 6d07f2a4b1b0a8dc2461d9695e2dd9753694d1f3 Mon Sep 17 00:00:00 2001 From: Faizan Ahmad Date: Wed, 10 May 2017 06:36:13 -0700 Subject: [PATCH] Issue: 1) https://github.com/Microsoft/vstest/issues/706 2) https://github.com/Microsoft/vstest/issues/618 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix: Don’t serialize the type if that have self-referencing loop --- .../Communication/JsonDataSerializer.cs | 19 ++++----- .../JsonDataSerializer.cs | 3 +- .../Client/Events/TestRunCompleteEventArgs.cs | 2 +- .../JsonDataSerializerTests.cs | 40 +++++++++++++++++++ 4 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/JsonDataSerializerTests.cs diff --git a/samples/Microsoft.TestPlatform.Protocol/Communication/JsonDataSerializer.cs b/samples/Microsoft.TestPlatform.Protocol/Communication/JsonDataSerializer.cs index b5800254a5..08c0dc0135 100644 --- a/samples/Microsoft.TestPlatform.Protocol/Communication/JsonDataSerializer.cs +++ b/samples/Microsoft.TestPlatform.Protocol/Communication/JsonDataSerializer.cs @@ -4,7 +4,7 @@ namespace Microsoft.TestPlatform.Protocol { using System.IO; - + using Newtonsoft.Json; using Newtonsoft.Json.Linq; using Newtonsoft.Json.Serialization; @@ -25,12 +25,13 @@ private JsonDataSerializer() { serializer = JsonSerializer.Create( new JsonSerializerSettings - { - DateFormatHandling = DateFormatHandling.IsoDateFormat, - DateParseHandling = DateParseHandling.DateTimeOffset, - DateTimeZoneHandling = DateTimeZoneHandling.Utc, - TypeNameHandling = TypeNameHandling.None - }); + { + DateFormatHandling = DateFormatHandling.IsoDateFormat, + DateParseHandling = DateParseHandling.DateTimeOffset, + DateTimeZoneHandling = DateTimeZoneHandling.Utc, + TypeNameHandling = TypeNameHandling.None, + ReferenceLoopHandling = ReferenceLoopHandling.Ignore + }); #if DEBUG // MemoryTraceWriter can help diagnose serialization issues. Enable it for // debug builds only. @@ -68,7 +69,7 @@ public Message DeserializeMessage(string rawMessage) public T DeserializePayload(Message message) { T retValue = default(T); - + // TODO: Currently we use json serializer auto only for non-testmessage types // CHECK: Can't we just use auto for everything if (Microsoft.TestPlatform.Protocol.MessageType.TestMessage.Equals(message.MessageType)) @@ -117,7 +118,7 @@ public string SerializeMessage(string messageType) public string SerializePayload(string messageType, object payload) { JToken serializedPayload = null; - + // TODO: Currently we use json serializer auto only for non-testmessage types // CHECK: Can't we just use auto for everything if (MessageType.TestMessage.Equals(messageType)) diff --git a/src/Microsoft.TestPlatform.CommunicationUtilities/JsonDataSerializer.cs b/src/Microsoft.TestPlatform.CommunicationUtilities/JsonDataSerializer.cs index ae4bd2f689..2da632cd13 100644 --- a/src/Microsoft.TestPlatform.CommunicationUtilities/JsonDataSerializer.cs +++ b/src/Microsoft.TestPlatform.CommunicationUtilities/JsonDataSerializer.cs @@ -32,7 +32,8 @@ private JsonDataSerializer() DateFormatHandling = DateFormatHandling.IsoDateFormat, DateParseHandling = DateParseHandling.DateTimeOffset, DateTimeZoneHandling = DateTimeZoneHandling.Utc, - TypeNameHandling = TypeNameHandling.None + TypeNameHandling = TypeNameHandling.None, + ReferenceLoopHandling = ReferenceLoopHandling.Ignore }; payloadSerializer = JsonSerializer.Create(jsonSettings); diff --git a/src/Microsoft.TestPlatform.ObjectModel/Client/Events/TestRunCompleteEventArgs.cs b/src/Microsoft.TestPlatform.ObjectModel/Client/Events/TestRunCompleteEventArgs.cs index 6cfd375802..4ad82f5fb5 100644 --- a/src/Microsoft.TestPlatform.ObjectModel/Client/Events/TestRunCompleteEventArgs.cs +++ b/src/Microsoft.TestPlatform.ObjectModel/Client/Events/TestRunCompleteEventArgs.cs @@ -29,7 +29,7 @@ public TestRunCompleteEventArgs(ITestRunStatistics stats, bool isCanceled, bool this.TestRunStatistics = stats; this.IsCanceled = isCanceled; this.IsAborted = isAborted; - this.Error = null; // Passing error value as null, should be pass exception. Issue: https://github.com/Microsoft/vstest/issues/618 + this.Error = error; this.AttachmentSets = attachmentSets; this.ElapsedTimeInRunningTests = elapsedTime; } diff --git a/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/JsonDataSerializerTests.cs b/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/JsonDataSerializerTests.cs new file mode 100644 index 0000000000..dc10a8ce02 --- /dev/null +++ b/test/Microsoft.TestPlatform.CommunicationUtilities.UnitTests/JsonDataSerializerTests.cs @@ -0,0 +1,40 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +namespace Microsoft.TestPlatform.CommunicationUtilities.UnitTests +{ + using System; + using Microsoft.VisualStudio.TestPlatform.CommunicationUtilities; + using Microsoft.VisualStudio.TestTools.UnitTesting; + + [TestClass] + public class JsonDataSerializerTests + { + [TestMethod] + public void SerializePayloadShouldSerializeAnObjectWithSelfReferencingLoop() + { + var classWithSelfReferencingLoop = new ClassWithSelfReferencingLoop(null); + classWithSelfReferencingLoop = new ClassWithSelfReferencingLoop(classWithSelfReferencingLoop); + classWithSelfReferencingLoop.InfiniteRefernce.InfiniteRefernce = classWithSelfReferencingLoop; + + var sut = JsonDataSerializer.Instance; + + // this line should not throw exception + sut.SerializePayload("dummy", classWithSelfReferencingLoop); + } + + public class ClassWithSelfReferencingLoop + { + public ClassWithSelfReferencingLoop(ClassWithSelfReferencingLoop ir) + { + this.InfiniteRefernce = ir; + } + + public ClassWithSelfReferencingLoop InfiniteRefernce + { + get; + set; + } + } + } +}