Skip to content

Commit

Permalink
fixed the null exception when dealing with nested null values (#8)
Browse files Browse the repository at this point in the history
* fixed the null exception when dealing with 5th level null values

* updated test case name
  • Loading branch information
apetrut authored and danielwertheim committed Jan 6, 2020
1 parent 4ed4862 commit e0b19b9
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/projects/Structurizer/Schemas/IndexAccessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ private IList<IStructureIndexValue> EvaluateCallstack<T>(T startNode, int startA
startAtCallstackIndex: callstackIndex + 1,
startPath: $"{currentProperty.Parent.Path}[{i.ToString()}].{currentProperty.Name}");

values.AddRange(tmpValues);
if (tmpValues != null)
{
values.AddRange(tmpValues);
}
}
return values;
}
Expand Down
33 changes: 31 additions & 2 deletions src/tests/UnitTests/StructureBuilderTests/StructureBuilderTests.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Structurizer;
using System.Collections.Generic;
using System.Linq;

namespace UnitTests.StructureBuilderTests
{
Expand Down Expand Up @@ -72,6 +72,28 @@ public void CreateStructure_When_structure_has_null_collection_It_should_create_
Assert.AreEqual("Temp", structure.Indexes[0].Path);
}

[TestMethod]
public void CreateStructure_When_5thLevel_has_null_values_It_should_create_structure_with_index_for_other_members()
{
Builder = StructureBuilder.Create(c => c.Register<TestItemForFifthLevel>());
var item = new TestItemForFifthLevel
{
Containers = new List<Container>()
{
new Container()
{
IntValue = 27,
MyItem = new Item()
}
}
};

var structure = Builder.CreateStructure(item);

Assert.AreEqual(1, structure.Indexes.Count);
Assert.AreEqual("Containers[0].IntValue", structure.Indexes[0].Path);
}

private class TestItemForFirstLevel
{
public int IntValue { get; set; }
Expand All @@ -83,9 +105,16 @@ private class TestItemForSecondLevel
public Container Container { get; set; }
}

private class TestItemForFifthLevel
{
public List<Container> Containers { get; set; }
}

private class Container
{
public int IntValue { get; set; }

public Item MyItem { get; set; }
}

private class IHaveStruct
Expand Down

0 comments on commit e0b19b9

Please sign in to comment.