Skip to content

Commit

Permalink
Fix #1416 nullable members in LoadFromCollection() (#1417)
Browse files Browse the repository at this point in the history
* Fix #1416 nullable members in FromCollection

* Replaced issue test with more appropriate thourough test

* Added comments and ensuring headers
  • Loading branch information
OssianEPPlus authored Apr 23, 2024
1 parent 795102c commit 09285c3
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/EPPlus/LoadFunctions/ReflectionHelpers/TypeExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ public static Type GetTypeOrUnderlyingType(this Type type)
{
t = ut;
}

return t;
}

public static bool IsComplexType(this Type type)
{
type = GetTypeOrUnderlyingType(type);
return type != typeof(string) && (type.IsClass || type.IsInterface || type.IsGenericType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using OfficeOpenXml.Table;

namespace EPPlusTest.LoadFunctions
{
[TestClass]
public class LoadFromCollectionAttributesComplexTypeTests
public class LoadFromCollectionAttributesComplexTypeTests : TestBase
{
private List<Outer> _collection = new List<Outer>();
private List<OuterWithHeaders> _collectionHeaders = new List<OuterWithHeaders>();
private List<OuterReversedSortOrder> _collectionReversed = new List<OuterReversedSortOrder>();
private List<OuterSubclass> _collectionInheritence = new List<OuterSubclass>();
private List<ColumnsWithoutAttributes> _collectionNoAttributes = new List<ColumnsWithoutAttributes>();

[TestInitialize]
public void Initialize()
Expand Down Expand Up @@ -67,6 +69,14 @@ public void Initialize()
},
Acknowledged = true
});
_collectionNoAttributes.Add(new ColumnsWithoutAttributes
{
NullableInt = 5,
NonNull = 15,
NullableDateTime = new DateTime(2021, 7, 1),
NestedNullableNullable = new NestedNullable { NullableValue = -2 },
ExplicitlyNullableString = "I'm nullable"
}) ;
}

[TestCleanup]
Expand Down Expand Up @@ -245,5 +255,41 @@ public void HiddenTest1()
Assert.AreEqual("Name 1", sheet.Cells[1, 4].Value);
}
}

//Testing i1416 I1416 Issue1416
[TestMethod]
public void NullablePropertiesShouldLoad()
{
using (var package = OpenPackage("LoadFromCollectionNullables.xlsx",true))
{
var ws = package.Workbook.Worksheets.Add("test");

var allMembers = typeof(ColumnsWithoutAttributes).GetMembers().Where(m => m.MemberType == MemberTypes.Property).ToArray();

ws.Cells[1, 1].LoadFromCollection(_collectionNoAttributes, PrintHeaders: true, TableStyle: TableStyles.Light1,
memberFlags: BindingFlags.Public | BindingFlags.Instance,
Members: allMembers);

var child0 = _collectionNoAttributes[0];

Assert.AreEqual("NullableInt", ws.Cells["A1"].Value);
Assert.AreEqual("NonNull", ws.Cells["B1"].Value);
Assert.AreEqual("NullableDateTime", ws.Cells["C1"].Value);
//Nested nullable table column with property gets the property name
Assert.AreEqual("NullableValue", ws.Cells["D1"].Value);
Assert.AreEqual("ExplicitlyNullableString", ws.Cells["E1"].Value);
Assert.IsNull(ws.Cells["F1"].Value);

Assert.AreEqual(child0.NullableInt.Value, ws.Cells["A2"].Value);
Assert.AreEqual(child0.NonNull, ws.Cells["B2"].Value);
Assert.AreEqual(child0.NullableDateTime.Value, ws.Cells["C2"].Value);
//Nested nullable table column with property
Assert.AreEqual(child0.NestedNullableNullable.NullableValue.Value, ws.Cells["D2"].Value);
Assert.AreEqual(child0.ExplicitlyNullableString, ws.Cells["E2"].Value);
Assert.IsNull(ws.Cells["F2"].Value);

SaveAndCleanup(package);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,21 @@ public class OuterWithHiddenColumn
[EpplusTableColumn(Header = "Name", Order = 4)]
public string Name { get; set; }
}
#nullable enable
public class NestedNullable
{
public int? NullableValue { get; set; }
}

public class ColumnsWithoutAttributes
{
public int? NullableInt { get; set; }
public int NonNull { get; set; }
public DateTime? NullableDateTime { get; set; }
[EpplusNestedTableColumn]
public NestedNullable? NestedNullableNullable { get; set; }
public string? ExplicitlyNullableString { get; set; }
public int? IntThatIsNull = null;
}
#nullable disable
}

0 comments on commit 09285c3

Please sign in to comment.