Skip to content

Fix tsvector metadata cast for IReadOnlyList - #3984

Merged
roji merged 1 commit into
npgsql:mainfrom
DaniilSaushkin:fix/3892-tsvector-properties
Oct 17, 2026
Merged

roji merged 1 commit into
npgsql:mainfrom
DaniilSaushkin:fix/3892-tsvector-properties

Conversation

@DaniilSaushkin

Copy link
Copy Markdown
Contributor

Summary

Fixes #3892.

GetTsVectorProperties() previously cast the stored annotation to string[], which caused an InvalidCastException when SetTsVectorProperties() received a different implementation of IReadOnlyList<string>.

This change uses the declared interface type instead of assuming a specific collection implementation.

Changes

  • Updated GetTsVectorProperties() to cast the annotation to IReadOnlyList<string>.
  • Added a regression test covering a C# collection expression.

Tests

  • Added regression test Can_get_ts_vector_properties_from_read_only_list.
  • Verified that the test fails before the fix and passes afterward.
  • Ran all 23 tests in NpgsqlMetadataExtensionsTest — all passed.

@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.

Problem. #3892 is a genuine public metadata contract bug: both setters accept IReadOnlyList<string>, but the getter assumes string[]. For example:

property.SetTsVectorProperties(["Title"]);
property.GetTsVectorProperties(); // InvalidCastException before this fix

new List<string> { "Title" } fails in the same way; an actual array works. This also breaks Database.GenerateCreateScript(), so it is not just an isolated metadata-getter problem.

Expected approach. Cast to the declared interface in the shared getter and add a focused non-array regression test. No setter-side copying, new abstraction, API change, or documentation update is needed. The relational annotation provider already materializes an array of column names, preserving the downstream SQL generator's array assumption.

Assessment. The PR matches that approach exactly: one production-line change and a targeted regression test. The collection expression really does produce a non-array implementation with the repository's compiler; I confirmed that the added test fails on the original getter. The change is necessary, sufficient, and appropriately scoped.

Gaps. No blocking correctness or scope findings. I would take this change as written, without expanding it into broader metadata cleanup.

Manual validation. I applied a patch identical to this PR at ebfff5e7e14b0a6545d36751b52b52eda99c33fd and exercised a separate console application against the provider API, not only the unit tests. GenerateCreateScript() fails before the fix and succeeds afterward for collection expressions, List<string>, read-only collections, and arrays, using both mutable and convention setters. All eight fixed combinations produce the expected stored to_tsvector('english', "Title") generated column, without needing a running database. All 23 tests in NpgsqlMetadataExtensionsTest also pass. Version checks reproduce the original failure as far back as 5.0.0, so this is longstanding rather than a recent regression.

Posted by copilot

@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.

Thanks!

@roji
roji enabled auto-merge (squash) October 17, 2026 09:29
@roji
roji merged commit e420326 into npgsql:main Oct 17, 2026
10 checks passed
roji added a commit that referenced this pull request Oct 17, 2026
Backport of #3984 for #3892.

(cherry picked from commit ebfff5e)

Co-authored-by: Daniil Saushkin <[email protected]>
Co-authored-by: Copilot App <[email protected]>
Copilot-Session: e169ea65-4dce-4e7b-aeb7-8d0be894be49
roji added a commit that referenced this pull request Oct 17, 2026
Backport #3984 for #3892.

(cherry picked from commit ebfff5e)

Co-authored-by: Copilot App <[email protected]>
Copilot-Session: 7b444553-37cd-4668-bf8f-a930eaabdb4d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SetTsVectorProperties throws InvalidCastException when passed a non-array IReadOnlyList<string>

2 participants