Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(TypeHandler): use the custom type handler when packing a list parameter #2068

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2168,6 +2168,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
if (list is not null && !viaSplit)
{
object? lastValue = null;
SqlMapper.ITypeHandler? handler = null;
foreach (var item in list)
{
if (++count == 1) // first item: fetch some type info
Expand All @@ -2178,7 +2179,7 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
}
if (!isDbString)
{
dbType = LookupDbType(item.GetType(), "", true, out var handler);
dbType = LookupDbType(item.GetType(), "", true, out handler);
}
}
var nextName = namePrefix + count.ToString();
Expand All @@ -2199,14 +2200,23 @@ public static void PackListParameters(IDbCommand command, string namePrefix, obj
}
}

var tmp = listParam.Value = SanitizeParameterValue(item);
if (tmp is not null && tmp is not DBNull)
lastValue = tmp; // only interested in non-trivial values for padding
if (handler is null)
{
var tmp = listParam.Value = SanitizeParameterValue(item);
if (tmp is not null && tmp is not DBNull)
lastValue = tmp; // only interested in non-trivial values for padding

if (DynamicParameters.ShouldSetDbType(dbType) && listParam.DbType != dbType.GetValueOrDefault())
if (DynamicParameters.ShouldSetDbType(dbType) && listParam.DbType != dbType.GetValueOrDefault())
{
listParam.DbType = dbType.GetValueOrDefault();
}
}
else
{
listParam.DbType = dbType.GetValueOrDefault();
if (DynamicParameters.ShouldSetDbType(dbType)) listParam.DbType = dbType.GetValueOrDefault();
handler.SetValue(listParam, item ?? DBNull.Value);
}

command.Parameters.Add(listParam);
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Dapper.Tests/TypeHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,24 @@ public void SO24740733_TestCustomValueSingleColumn()
Assert.Equal(200, foo.Value);
}

[Fact]
public void Issue2067_TestCustomValueCollection()
{
SqlMapper.AddTypeHandler(RatingValueHandler.Default);

var parameters = new
{
ListOfRatingValues = new List<RatingValue>
{
new() { Value = 1 },
new() { Value = 2 },
}
};

var result = connection.Query<int>("SELECT 1 WHERE 1 IN @ListOfRatingValues", parameters).Single();
Assert.Equal(1, result);
}

public class StringListTypeHandler : SqlMapper.TypeHandler<List<string>>
{
private StringListTypeHandler()
Expand Down