Skip to content

Commit 3abfb59

Browse files
Don't throw on null value for Uri (#57)
* Add null URI to existing converter test * Don't invoke the type converter when the incoming complex type value is null * Undo formatting change
1 parent 670f8a1 commit 3abfb59

3 files changed

Lines changed: 17 additions & 3 deletions

File tree

src/PhenX.EntityFrameworkCore.BulkInsert/Metadata/PropertyAccessor.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,18 @@ internal static class PropertyAccessor
3939
converterInput = Expression.Convert(getterExpression, converterParamType);
4040
}
4141

42-
getterExpression = Expression.Invoke(converter, converterInput);
42+
var invokeConverter = Expression.Invoke(converter, converterInput);
43+
44+
if (propertyType.IsClass)
45+
{
46+
var nullCondition = Expression.Equal(getterExpression, Expression.Constant(null, propertyType));
47+
var nullResult = Expression.Constant(null, converter.ReturnType);
48+
getterExpression = Expression.Condition(nullCondition, nullResult, invokeConverter);
49+
}
50+
else
51+
{
52+
getterExpression = invokeConverter;
53+
}
4354

4455
propertyType = getterExpression.Type;
4556
}

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/DbContext/TestEntityWithConverters.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,8 @@ public class TestEntityWithConverters : TestEntityBase
1414

1515
[Column("created_at")]
1616
public DateTime CreatedAt { get; set; }
17+
18+
[Column("uri")]
19+
public Uri? Uri { get; set; }
1720
}
1821

tests/PhenX.EntityFrameworkCore.BulkInsert.Tests/Tests/Basic/BasicTestsBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ public async Task InsertEntities_AndReturn_WithEntityWithValueConverters(InsertS
176176

177177
var entities = new List<TestEntityWithConverters>
178178
{
179-
new TestEntityWithConverters() { Name = $"{_run}_Entity1", CreatedAt = now },
180-
new TestEntityWithConverters() { Name = $"{_run}_Entity2", CreatedAt = now.AddDays(-1) }
179+
new TestEntityWithConverters() { Name = $"{_run}_Entity1", CreatedAt = now, Uri = null },
180+
new TestEntityWithConverters() { Name = $"{_run}_Entity2", CreatedAt = now.AddDays(-1), Uri = new Uri("http://example.com/test") }
181181
};
182182

183183
// Act

0 commit comments

Comments
 (0)