Skip to content

Commit

Permalink
Issue:
Browse files Browse the repository at this point in the history
1) microsoft#706
2) microsoft#618

Fix:
Don’t serialize the type if that have self-referencing loop
  • Loading branch information
Faizan2304 committed May 10, 2017
1 parent 93a3a92 commit 6d07f2a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace Microsoft.TestPlatform.Protocol
{
using System.IO;

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
Expand All @@ -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.
Expand Down Expand Up @@ -68,7 +69,7 @@ public Message DeserializeMessage(string rawMessage)
public T DeserializePayload<T>(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))
Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}

0 comments on commit 6d07f2a

Please sign in to comment.