Skip to content

Fix 2.1.86 unbox-gate regressions: array columns to interface members, sql_variant to primitives - #2271

Merged
mgravell merged 1 commit into
mainfrom
fix/object-typecode-unbox-regression
Nov 20, 2026
Merged

mgravell merged 1 commit into
mainfrom
fix/object-typecode-unbox-regression

Conversation

@mgravell

Copy link
Copy Markdown
Member

Fixes #2231, fixes #2234.

Both regressions trace to the direct-unbox gate rewritten in 6d48ef6 (#2228, shipped in 2.1.86):

  • PostgreSQL SMALLINT[] mapping fails in Dapper 2.1.86 but works in 2.1.79 #2231 — the new dataTypeCode == unboxTypeCode && dataTypeCode != TypeCode.Object test excluded Object/Object pairs entirely, removing the runtime-checked castclass that let array columns land in interface members (IReadOnlyList<short> etc). The gate now admits reference-typed targets under that pairing. Crucially, this also covers the shape where the provider's GetFieldType is too weak to prove the match statically: Npgsql 7, 8 and 10 report array columns as typeof(Array) (only 9.0.0–9.0.4 report the concrete array type), so an IsAssignableFrom-style compile-time check cannot work there — only the runtime castclass can, which is what pre-2.1.86 emitted.
  • SQL Server sql_variant mapping regression in 2.1.86: SERVERPROPERTY Int32 to bool throws InvalidCastException #2234 — colType == typeof(object) was added to the direct-unbox branch, so a sql_variant holding a boxed Int32 read into a bool member blind-unboxed and threw InvalidCastException instead of coercing via the flexible-convert path. object columns now direct-unbox only into TypeCode.Object targets (Guid, Guid? — where the runtime box is the only truth available, and keeping Re-enable DateOnly/TimeOnly support, fixing the defects that got it disabled #2228's fast path for those), and otherwise coerce. This also fixes the unreported sibling case of variant Int32 → string member.

Value-type Object/Object pairs (TimeSpan column vs TimeOnly? member, say) stay on the flexible-convert path, so the DateOnly/TimeOnly fixes from #2228 are preserved — the new tests pin those guards too.

Tests, all red on main and green here, run against real servers locally (docker Postgres + SQL Server 2019; full suite 794 passed / 0 failed on net8.0 and net10.0):

  • DB-free mock-reader tests pinning every column-type shape exactly, including the typeof(Array) shape that provider-typed tests can miss
  • SQL Server sql_variant integration tests (cast + the original SERVERPROPERTY repro)
  • Postgres integration tests reading smallint[]/int[] columns into IReadOnlyList<short>/IEnumerable<int>/array members — these run under the Npgsql 10 typeof(Array) shape in CI

Credit where due: this builds on the diagnosis and partial fixes in #2267 (@lqkhanh295 — closest to complete, but its IsAssignableFrom check cannot reach the Npgsql 7/8/10 typeof(Array) shape), #2244 (@Arthur031221 — the cleanest sql_variant half), #2246 (@SENESO) and #2239/#2241 (@leandrommedeiros, @SENESO). Those PRs are superseded by this one.

…, sql_variant to primitives

The direct-unbox gate rewritten in 6d48ef6 broke two shapes that worked in
2.1.79:

- array columns into interface members (IReadOnlyList<T> etc): the TypeCode
  tests excluded Object/Object pairs entirely, removing the runtime-checked
  castclass those relied on. The gate now admits reference-typed targets
  under that pairing, which also covers Npgsql 7/8/10 reporting array
  columns as typeof(Array) - a shape no static assignability test can prove
  (#2231)
- sql_variant columns into primitive members: colType == typeof(object) was
  added to the direct-unbox branch, so a boxed Int32 into a bool member
  blind-unboxed and threw instead of coercing via flexible-convert. object
  columns now direct-unbox only into TypeCode.Object targets (Guid etc),
  where the runtime box is the only truth available (#2234)

Value-type Object/Object pairs (TimeSpan vs TimeOnly?) stay on the
flexible-convert path, preserving the DateOnly/TimeOnly fixes from #2228.

Fixes #2231, fixes #2234
@mgravell
mgravell merged commit db2670c into main Nov 20, 2026
2 checks passed
@mgravell
mgravell deleted the fix/object-typecode-unbox-regression branch November 20, 2026 10:18
This was referenced Nov 30, 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

1 participant