Skip to content

Preserve Size set by type handlers for string parameters - #2221

Open
FirmaSpring wants to merge 1 commit into
DapperLib:mainfrom
FirmaSpring:fix/typehandler-string-size-overwrite
Open

Preserve Size set by type handlers for string parameters#2221
FirmaSpring wants to merge 1 commit into
DapperLib:mainfrom
FirmaSpring:fix/typehandler-string-size-overwrite

Conversation

@FirmaSpring

Copy link
Copy Markdown

Problem

When a TypeHandler is registered for string and a query is executed with an anonymous object parameter (the IL generator path in CreateParamInfoGenerator), the generated code invokes the handler's SetValue and then unconditionally sets the parameter's Size based on the string length heuristics (DbString.DefaultLength / -1), overwriting whatever Size the handler configured.

Reported in #2141. The DynamicParameters path already leaves the Size to the handler; only the IL path diverges.

Reproduction

SqlMapper.RemoveTypeMap(typeof(string));
SqlMapper.AddTypeHandler(new StringTypeHandler()); // sets parameter.Size = 12345

connection.Execute("select @p", new { p = "hello" });
// parameter.Size is 4000 on main, not 12345

Fix

Move the string Size emission into the handler is null branch, so when a type handler is registered it fully owns parameter configuration, consistent with the DynamicParameters behavior.

Testing

Added TypeHandlerSetValue_StringParameterSizeIsNotOverwritten_Issue2141 to SqliteTypeHandlerTests (in-memory SQLite, no external database required). Verified it fails on current main and passes with this change; the existing SqliteTypeHandlerTests suite passes.

Fixes #2141

When an anonymous object parameter goes through the IL generator
path, the generated code set the parameter Size after invoking the
registered type handler's SetValue, overwriting whatever Size the
handler had configured (typically resetting it to 4000 or -1).

Move the string Size emission into the non-handler branch so the
handler owns the parameter configuration, matching what
DynamicParameters already does.

Fixes DapperLib#2141

Co-authored-by: FirmamentalSpring <287222957+FirmaSpring@users.noreply.github.com>
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.

Parameter size overwritten after custom TypeHandler

1 participant