Skip to content

Fix type mapping regression for sql_variant and array interface targets (#2234, #2231) - #2267

Open
lqkhanh295 wants to merge 1 commit into
DapperLib:mainfrom
lqkhanh295:fix/type-mapping-variant-and-interfaces
Open

lqkhanh295 wants to merge 1 commit into
DapperLib:mainfrom
lqkhanh295:fix/type-mapping-variant-and-interfaces

Conversation

@lqkhanh295

Copy link
Copy Markdown

Summary

Fixes two type mapping regressions introduced in commit 6d48ef6:

  1. Fixes SQL Server sql_variant mapping regression in 2.1.86: SERVERPROPERTY Int32 to bool throws InvalidCastException #2234: Reading SQL Server sql_variant (reported by ADO.NET with colType = typeof(object)) into a bool or primitive member threw InvalidCastException: Specified cast is not valid (or unable to cast Int32 to Boolean).
  2. Fixes PostgreSQL SMALLINT[] mapping fails in Dapper 2.1.86 but works in 2.1.79 #2231: Reading PostgreSQL SMALLINT[] (reported by Npgsql as short[]) into an interface property such as IReadOnlyList<short> threw InvalidCastException: Object must implement IConvertible at Convert.ChangeType.

Root Cause

In commit 6d48ef6, the condition in LoadReaderValueOrBranchToDBNullLabel was modified:

  • colType == typeof(object) was added to the direct unbox branch under the assumption that sql_variant can directly unbox to the target type. However, if sql_variant contains an Int32 and the target member is bool, emitting OpCodes.Unbox_Any, typeof(bool) violates CLR unbox rules and throws InvalidCastException.
  • dataTypeCode == unboxTypeCode was constrained to != TypeCode.Object. For arrays implementing interfaces (e.g. short[] implementing IReadOnlyList<short>), both have TypeCode.Object. Excluding TypeCode.Object bypassed the direct castclass/unbox path and routed the array to FlexibleConvertBoxedFromHeadOfStack, which called Convert.ChangeType on a non-IConvertible array.

Changes

  • Replaced colType == typeof(object) with unboxType.IsAssignableFrom(colType).
  • When unboxType.IsAssignableFrom(colType) is true (e.g. short[] to IReadOnlyList<short>), OpCodes.Unbox_Any correctly performs a castclass on the reference/interface type.
  • Columns reported as typeof(object) (such as sql_variant) now correctly route to FlexibleConvertBoxedFromHeadOfStack, where Convert.ChangeType coerces the boxed runtime value (e.g. int 1 to bool true).

Testing

  • Added regression tests in TypeMappingRegressionTests.cs covering:
    • sql_variant (object column type containing int) mapped to bool, bool?, and long.
    • Array types (short[], int[]) mapped to IReadOnlyList<short> and IEnumerable<int>.
  • Verified entire Dapper.Tests suite passes (800 tests executed, 0 failures).

Copilot AI balanced review requested due to automatic review settings November 16, 2026 13:09

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

This branch has not been deployed

No deployments
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