Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 14 additions & 18 deletions src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,23 @@ private static Expression<Func<TInput, TOutput>> ArrayConversionExpression<TInpu
throw new ArgumentException("Both TInput and TOutput must be arrays or IList<T>");
}

// elementConversionExpression is always over non-nullable value types. If the array is over nullable types,
// we need to sanitize via an external null check.
if (elementConversionExpression is not null && inputElementType.IsNullableType() && outputElementType.IsNullableType())
if (elementConversionExpression is not null)
{
// p => p is null ? null : elementConversionExpression(p)
var p = Parameter(inputElementType, "foo");
elementConversionExpression = Lambda(
Condition(
// The collection element types and the converter's types may differ in nullability.
var p = Parameter(inputElementType, "p");
Expression converted = Convert(
Invoke(elementConversionExpression, Convert(p, elementConversionExpression.Parameters[0].Type)),
outputElementType);

if (inputElementType.IsNullableType() && outputElementType.IsNullableType())
{
converted = Condition(
Equal(p, Constant(null, inputElementType)),
Constant(null, outputElementType),
Convert(
Invoke(
elementConversionExpression,
// The user-provided conversion lambda typically accepts non-nullable (value) types, with EF Core doing the
// null-sanitization and conversion to non-nullable; do this here unless the user-provided lambda happens to
// accept a nullable value type parameter.
elementConversionExpression.Parameters[0].Type.IsNullableType()
? p
: Convert(p, inputElementType.UnwrapNullableType())),
outputElementType)),
p);
converted);
}

elementConversionExpression = Lambda(converted, p);
}

var input = Parameter(typeof(TInput), "value");
Expand Down
20 changes: 20 additions & 0 deletions test/EFCore.PG.Tests/Storage/NpgsqlArrayValueConverterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,26 @@ public void Can_convert_number_arrays_to_enum_arrays_object()
Assert.Null(converter(null));
}

[ConditionalFact]
public void Can_convert_non_nullable_arrays_with_nullable_element_converter()
{
var converter = new NpgsqlArrayConverter<int[], int[], string[]>(
new ValueConverter<int?, string>(v => v.HasValue ? v.Value.ToString() : null, v => v == null ? null : int.Parse(v)));

Assert.Equal(new[] { "1", "2" }, converter.ConvertToProviderExpression.Compile()(new[] { 1, 2 }));
Assert.Equal(new[] { 1, 2 }, converter.ConvertFromProviderExpression.Compile()(new[] { "1", "2" }));
}

[ConditionalFact]
public void Can_convert_non_nullable_lists_with_nullable_element_converter()
{
var converter = new NpgsqlArrayConverter<List<int>, List<int>, string[]>(
new ValueConverter<int?, string>(v => v.HasValue ? v.Value.ToString() : null, v => v == null ? null : int.Parse(v)));

Assert.Equal(new[] { "1", "2" }, converter.ConvertToProviderExpression.Compile()(new List<int> { 1, 2 }));
Assert.Equal(new[] { 1, 2 }, converter.ConvertFromProviderExpression.Compile()(new[] { "1", "2" }));
}

private enum Beatles
{
John = 7,
Expand Down
Loading