Skip to content

Fix TargetInvocationException with non nullable value types with HasConversion - #3920

Closed
henriquewr wants to merge 1 commit into
npgsql:mainfrom
henriquewr:targetInvocationNonNullable
Closed

henriquewr wants to merge 1 commit into
npgsql:mainfrom
henriquewr:targetInvocationNonNullable

Conversation

@henriquewr

Copy link
Copy Markdown
Contributor

fixes #3919

I'm almost 100% certain that this also fixes #3716
seems to be the same stack trace


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)));
}

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Compared this fix with #3965. Both correctly address #3919: a non-nullable collection element is passed to a converter whose model type is nullable, for example int[] numbers with numbers.Contains(x.Num.Value) and an int?-to-string converter.

Approach: I would expect conversion input/output nullability to be adapted before invoking the element lambda. This PR does so at the mapping layer by composing a CastingConverter<int, int?> with the property's converter. #3965 instead performs the adaptation inside NpgsqlArrayConverter, where the incompatible invocation originates and whose constructor already permits element/converter types that differ in nullability. Both approaches preserve the existing nullable-element handling.

Validation and coverage: I built each production patch separately on the same pre-fix main baseline and exercised a standalone console application against both. The original array query, its List<int> equivalent, and a nullable array containing null all translated successfully and produced identical SQL and converted parameters. The inherited functional-test changes in the two PRs are identical.

The difference is direct use of the converter API:

new NpgsqlArrayConverter<int[], int[], string[]>(
    new ValueConverter<int?, string>(
        v => v.HasValue ? v.Value.ToString() : null,
        v => v == null ? null : int.Parse(v)));

With this PR alone, construction still throws the original argument-type exception, because it bypasses the mapping-layer adaptation. With #3965, both array and list converters construct successfully and round-trip [1, 2] through ["1", "2"]. #3965 also adds unit coverage for these two cases.

Recommendation: Keep the converter-level approach in #3965 rather than combining the implementations. This PR is a valid narrower fix, but does not provide an additional benefit for the scenarios checked. It adds reflection and converter composition at the call site where #3965 reuses the converter's existing adaptation logic.

Posted by copilot

@roji

roji commented Oct 15, 2026

Copy link
Copy Markdown
Member

Apologies @henriquewr, I only noticed this PR after submitting and merging #3965. In any case, #3965 handles the nullability mismatch directly in NpgsqlArrayConverter, rather than compensating at the mapping layer with an additional CastingConverter. Both approaches fix the reported query, but fixing the converter itself also makes direct array/list conversions work, avoids the extra reflection and converter composition, and adds round-trip unit coverage alongside the same functional regression test.

Thanks for doing the work here and reporting the bug!

@roji roji closed this Oct 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

2 participants