Skip to content
Open
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
12 changes: 12 additions & 0 deletions src/EFCore.PG/Storage/Internal/Mapping/NpgsqlArrayTypeMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,18 @@ private static RelationalTypeMappingParameters CreateParameters(string storeType
{
providerElementType = providerElementType.MakeNullable();
}
else if (elementConverter.ConvertFromProviderExpression.ReturnType.IsNullableValueType()
&& elementConverter.ConvertFromProviderExpression.ReturnType.UnwrapNullableType().IsAssignableFrom(elementType))
{
var converterType = typeof(CastingConverter<,>).MakeGenericType(
elementType,
elementType.MakeNullable());

var converterTypeInstance = (ValueConverter)Activator.CreateInstance(
converterType)!;

elementConverter = converterTypeInstance.ComposeWith(elementConverter);
}

converter = (ValueConverter)Activator.CreateInstance(
typeof(NpgsqlArrayConverter<,,>).MakeGenericType(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2546,9 +2546,29 @@ WHERE NOT (p."NullableWrappedId" = ANY (@values) AND p."NullableWrappedId" = ANY
""");
}

public override Task Parameter_collection_of_structs_Contains_nullable_struct_with_nullable_comparer()
=> Assert.ThrowsAnyAsync<TargetInvocationException>(
() => base.Parameter_collection_of_structs_Contains_nullable_struct_with_nullable_comparer());
public override async Task Parameter_collection_of_structs_Contains_nullable_struct_with_nullable_comparer()
{
await base.Parameter_collection_of_structs_Contains_nullable_struct_with_nullable_comparer();

AssertSql(
"""
@values={ '22'
'33' } (DbType = Object)

SELECT p."Id", p."Bool", p."Bools", p."DateTime", p."DateTimes", p."Enum", p."Enums", p."Int", p."Ints", p."NullableInt", p."NullableInts", p."NullableString", p."NullableStrings", p."NullableWrappedId", p."NullableWrappedIdWithNullableComparer", p."String", p."Strings", p."WrappedId"
FROM "PrimitiveCollectionsEntity" AS p
WHERE p."NullableWrappedIdWithNullableComparer" = ANY (@values) OR (p."NullableWrappedIdWithNullableComparer" IS NULL AND array_position(@values, NULL) IS NOT NULL)
""",
//
"""
@values={ '11'
'44' } (DbType = Object)

SELECT p."Id", p."Bool", p."Bools", p."DateTime", p."DateTimes", p."Enum", p."Enums", p."Int", p."Ints", p."NullableInt", p."NullableInts", p."NullableString", p."NullableStrings", p."NullableWrappedId", p."NullableWrappedIdWithNullableComparer", p."String", p."Strings", p."WrappedId"
FROM "PrimitiveCollectionsEntity" AS p
WHERE NOT (p."NullableWrappedId" = ANY (@values) AND p."NullableWrappedId" = ANY (@values) IS NOT NULL) AND (p."NullableWrappedId" IS NOT NULL OR array_position(@values, NULL) IS NULL)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't it be NullableWrappedIdWithNullableComparer in the WHERE clause?

base test:

[Fact] // Issue #35117
public virtual async Task Parameter_collection_of_structs_Contains_nullable_struct_with_nullable_comparer()
{
    var values = new List<WrappedId> { new(22), new(33) };

    await AssertQuery(
        ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => values.Contains(c.NullableWrappedIdWithNullableComparer!.Value)),
        ss => ss.Set<PrimitiveCollectionsEntity>().Where(c
            => c.NullableWrappedIdWithNullableComparer != null && values.Contains(c.NullableWrappedIdWithNullableComparer.Value)));

    values = [new(11), new(44)];

    await AssertQuery(
        ss => ss.Set<PrimitiveCollectionsEntity>().Where(c => !values.Contains(/*Here*/c.NullableWrappedId!.Value)),
        ss => ss.Set<PrimitiveCollectionsEntity>().Where(c
            => c.NullableWrappedIdWithNullableComparer == null || !values.Contains(c.NullableWrappedIdWithNullableComparer!.Value)));
}

""");
}

public override async Task Parameter_collection_of_nullable_structs_Contains_struct()
{
Expand Down