Skip to content

Commit

Permalink
Work with Null property
Browse files Browse the repository at this point in the history
  • Loading branch information
phnx47 committed Dec 18, 2019
1 parent c24ff37 commit f2ff98b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/FingerprintBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Runtime.Serialization.Formatters.Binary;

Expand Down Expand Up @@ -61,7 +62,11 @@ public IFingerprintBuilder<T> For<TProperty>(Expression<Func<T, TProperty>> expr
using (var memory = new MemoryStream())
{
foreach (var item in _fingerprints)
binaryFormatter.Serialize(memory, item.Value(obj));
{
var graph = item.Value(obj);
if (graph != null)
binaryFormatter.Serialize(memory, graph);
}
return _computeHash(memory.ToArray());
}
Expand Down
16 changes: 16 additions & 0 deletions tests/SimpleFingerprintBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ public void UserInfo_ToLowerCase_Sha256()
var hash1 = fingerprint(user).ToLowerHexString();
Assert.Equal(expectedHash, hash1);
}

[Fact]
public void UserInfo_Sha1_Null()
{
var fingerprint = FingerprintBuilder<UserInfo>
.Create(SHA1.Create().ComputeHash)
.For(p => p.FirstName)
.For(p => p.LastName)
.Build();

var user = new UserInfo { FirstName = "John", LastName = null };

var hash = fingerprint(user).ToLowerHexString();

Assert.Equal("5ab5aeba11346413348fb7c9361058e016ecf3ca", hash);
}

private class UserInfo
{
Expand Down

0 comments on commit f2ff98b

Please sign in to comment.