Skip to content

Commit

Permalink
Fixed issue with circular references.
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh Close committed Jan 19, 2018
1 parent b3d35d4 commit 4ed29fe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
24 changes: 24 additions & 0 deletions src/CsvHelper.Tests/AutoMapping/CircularReferenceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ namespace CsvHelper.Tests.AutoMapping
[TestClass]
public class CircularReferenceTests
{
[TestMethod]
public void SelfCircularDependencyTest()
{
var config = new CsvHelper.Configuration.Configuration();
var map = config.AutoMap<SelfCircularA>();
}

[TestMethod]
public void CircularDependencyTest()
{
Expand All @@ -27,6 +34,23 @@ public void CircularDependencyWithMultiplePropertiesTest()
Assert.AreEqual( 3, map.ReferenceMaps.Count );
}

private class SelfCircularA
{
public SelfCircularB Circular { get; set; }
}

private class SelfCircularB
{
public SelfCircularB Self { get; set; }

public SelfCircularC C { get; set; }
}

private class SelfCircularC
{
public string Id { get; set; }
}

private class ACircular
{
public string Id { get; set; }
Expand Down
13 changes: 6 additions & 7 deletions src/CsvHelper/LinkedListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,14 @@ public static void Drop<T>( this LinkedList<T> list, LinkedListNode<T> node )
return;
}

var nodeToDrop = list.Find( node.Value );
while( list.Count > 0 && list.Last != nodeToDrop )
{
list.RemoveLast();
}

if( list.Last == nodeToDrop )
while( list.Count > 0 )
{
var nodeToRemove = list.Last;
list.RemoveLast();
if( nodeToRemove == node )
{
break;
}
}
}
}
Expand Down

0 comments on commit 4ed29fe

Please sign in to comment.