Skip to content

Commit c9a03cf

Browse files
author
Roman Köhler
committed
little bugfix
1 parent f3256ae commit c9a03cf

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

ObjectFiller.Test/ObjectFillerRecursiveTests.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,17 @@ public void RecursiveFill_ParentList_Succeeds()
177177
{
178178
var filler = new Filler<Parent>();
179179
var r = filler.Create();
180+
181+
Assert.IsNull(r.Childrens[0].Parent.Childrens);
180182
}
181183

182184
[TestMethod]
183185
public void RecursiveFill_ParentDictionary_Succeeds()
184186
{
185187
var filler = new Filler<ParentDictionary>();
186188
var r = filler.Create();
189+
}
187190

188191

189-
}
190192
}
191193
}

ObjectFiller/Filler.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -284,25 +284,34 @@ private object GetRandomEnumValue(Type type)
284284
return 0;
285285
}
286286

287-
private object GetFilledPoco(Type type, FillerSetupItem currentSetupItem, HashStack<Type> typeTracker)
287+
private bool CheckForCircularReference(Type targetType, HashStack<Type> typeTracker, FillerSetupItem currentSetupItem)
288288
{
289289
if (typeTracker != null)
290290
{
291-
if (typeTracker.Contains(type))
291+
if (typeTracker.Contains(targetType))
292292
{
293293
if (currentSetupItem.ThrowExceptionOnCircularReference)
294294
{
295295
throw new InvalidOperationException(
296296
string.Format(
297297
"The type {0} was already encountered before, which probably means you have a circular reference in your model. Either ignore the properties which cause this or specify explicit creation rules for them which do not rely on types.",
298-
type.Name));
298+
targetType.Name));
299299
}
300300

301-
return GetDefaultValueOfType(type);
301+
return true;
302302
}
303+
}
303304

304-
typeTracker.Push(type);
305+
return false;
306+
}
307+
308+
private object GetFilledPoco(Type type, FillerSetupItem currentSetupItem, HashStack<Type> typeTracker)
309+
{
310+
if (CheckForCircularReference(type, typeTracker, currentSetupItem))
311+
{
312+
return GetDefaultValueOfType(type);
305313
}
314+
typeTracker.Push(type);
306315

307316
object result = CreateInstanceOfType(type, currentSetupItem);
308317

@@ -352,6 +361,11 @@ private IList GetFilledList(Type propertyType, FillerSetupItem currentSetupItem,
352361
{
353362
Type genType = propertyType.GetGenericArguments()[0];
354363

364+
if (CheckForCircularReference(genType, typeTracker, currentSetupItem))
365+
{
366+
return null;
367+
}
368+
355369
IList list;
356370
if (!propertyType.IsInterface && propertyType.GetInterfaces().Any(x => x.GetGenericTypeDefinition() == typeof(ICollection<>)))
357371
{

0 commit comments

Comments
 (0)