Skip to content

Honor TypeHandler.SetValue for known types and list parameters (#2266, #1649) - #2273

Open
lqkhanh295 wants to merge 1 commit into
DapperLib:mainfrom
lqkhanh295:fix/typehandler-known-types-setvalue
Open

lqkhanh295 wants to merge 1 commit into
DapperLib:mainfrom
lqkhanh295:fix/typehandler-known-types-setvalue

Conversation

@lqkhanh295

Copy link
Copy Markdown

Fixes #2266. Also addresses #1649 and #1972.

Context

When someone registers a custom TypeHandler<T> for a type that Dapper already recognizes (like DateOnly, Guid, or string), Parse runs as expected when querying, but SetValue gets bypassed when binding parameters on commands.

In SqlMapper.LookupDbType, the lookup in typeMap was placed before typeHandlers. Because the type was found in typeMap, handler remained null, which prevented parameter generation (both the IL generator and DynamicParameters) from calling handler.SetValue.

Additionally, in PackListParameters, LookupDbType was called for the first item, but the returned handler wasn't stored or invoked for individual list items when expanding collection parameters.

Changes

  • In LookupDbType, check typeHandlers before typeMap. If a custom handler is registered for a type, it takes precedence and gets returned.
  • In PackListParameters, keep the looked-up handler and call handler.SetValue for each parameter in the expanded collection.
  • Added tests in SqliteTests.cs verifying SetValue and Parse for DateOnly, Guid, and string across anonymous objects, nullable parameters, DynamicParameters, and collection expansions.

Copilot AI balanced review requested due to automatic review settings November 21, 2026 18:16

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

Development

Successfully merging this pull request may close these issues.

TypeHandler is not fully used for known types

2 participants