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 issue 222: MultiMap does not return null object #1932

Open
wants to merge 2 commits 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
20 changes: 20 additions & 0 deletions Dapper/SqlMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3547,6 +3547,26 @@ private static void GenerateDeserializerFromMap(Type type, DbDataReader reader,

il.MarkLabel(finishLabel);
}
else
{
if (first && returnNullIfFirstMissing)
{
Label finishLabel = il.DefineLabel();

il.Emit(OpCodes.Ldarg_0); // stack is now [target][reader]
EmitInt32(il, index); // stack is now [target][reader][index]
il.Emit(OpCodes.Callvirt, isDbNull); // stack is now [target][bool]
il.Emit(OpCodes.Brfalse_S, finishLabel);

il.Emit(OpCodes.Pop);
il.Emit(OpCodes.Ldnull); // stack is now [null]
il.Emit(OpCodes.Stloc, returnValueLocal);
il.Emit(OpCodes.Br, allDone);

il.MarkLabel(finishLabel); // stack is now [target]
}
}

first = false;
index++;
}
Expand Down
17 changes: 16 additions & 1 deletion tests/Dapper.Tests/MultiMapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ public void TestMultiMapWithSplitWithNullValue() // https://stackoverflow.com/q/
}

[Fact]
public void TestMultiMapWithSplitWithNullValueAndSpoofColumn() // https://stackoverflow.com/q/10744728/449906
public void TestMultiMapWithSplitWithNotNullValueInSpoofColumn() // https://stackoverflow.com/q/10744728/449906
{
const string sql = "select 1 as id, 'abc' as name, 1 as spoof, NULL as description, 'def' as name";
var product = connection.Query<Product, Category, Product>(sql, (prod, cat) =>
Expand All @@ -285,6 +285,21 @@ public void TestMultiMapWithSplitWithNullValueAndSpoofColumn() // https://stacko
Assert.Null(product.Category.Description);
}

[Fact]
public void TestMultiMapWithSplitWithNullValueInSpoofColumn()
{
const string sql = "select 1 as id, 'abc' as name, NULL as spoof, NULL as description, 'def' as name";
var product = connection.Query<Product, Category, Product>(sql, (prod, cat) =>
{
prod.Category = cat;
return prod;
}, splitOn: "spoof").First();
// assertions
Assert.Equal(1, product.Id);
Assert.Equal("abc", product.Name);
Assert.Null(product.Category);
}

[Fact]
public void TestMultiMappingVariations()
{
Expand Down