From 6988ecbccefb9df44faea2935cb59d97e896360a Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Thu, 15 Oct 2026 14:13:55 +0200 Subject: [PATCH] [8.0] Fix collection conversion with nullable element converters (#3965) Adapt element conversion input and output nullability while preserving null guards. Backport the array and list regression tests; the inherited nullable-comparer query test is unavailable in EF8. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: cdf2d065-d9c8-4f52-85ea-379a40d76928 --- .../ValueConversion/NpgsqlArrayConverter.cs | 32 ++++++++----------- .../Storage/NpgsqlArrayValueConverterTest.cs | 20 ++++++++++++ 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs b/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs index 0cbfea7203..46dd7c54c9 100644 --- a/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs +++ b/src/EFCore.PG/Storage/ValueConversion/NpgsqlArrayConverter.cs @@ -82,27 +82,23 @@ private static Expression> ArrayConversionExpression"); } - // 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"); diff --git a/test/EFCore.PG.Tests/Storage/NpgsqlArrayValueConverterTest.cs b/test/EFCore.PG.Tests/Storage/NpgsqlArrayValueConverterTest.cs index ebfe3ba451..a4909b27a6 100644 --- a/test/EFCore.PG.Tests/Storage/NpgsqlArrayValueConverterTest.cs +++ b/test/EFCore.PG.Tests/Storage/NpgsqlArrayValueConverterTest.cs @@ -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( + new ValueConverter(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, string[]>( + new ValueConverter(v => v.HasValue ? v.Value.ToString() : null, v => v == null ? null : int.Parse(v))); + + Assert.Equal(new[] { "1", "2" }, converter.ConvertToProviderExpression.Compile()(new List { 1, 2 })); + Assert.Equal(new[] { 1, 2 }, converter.ConvertFromProviderExpression.Compile()(new[] { "1", "2" })); + } + private enum Beatles { John = 7,