Skip to content

Commit

Permalink
fix #1993 - when matching properties, prefer non-normalized property …
Browse files Browse the repository at this point in the history
…name (#1997)
  • Loading branch information
mgravell authored Nov 11, 2023
1 parent 5bb98eb commit 947b582
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 1 deletion.
18 changes: 18 additions & 0 deletions Dapper/DefaultTypeMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,24 @@ public SqlMapper.IMemberMap GetConstructorParameter(ConstructorInfo constructor,
{
// same again, minus underscore delta
name = name?.Replace("_", "");

// match normalized column name vs actual property name
foreach (var member in members)
{
if (string.Equals(name, selector(member), StringComparison.Ordinal))
{
return member;
}
}
foreach (var member in members)
{
if (string.Equals(name, selector(member), StringComparison.OrdinalIgnoreCase))
{
return member;
}
}

// match normalized column name vs normalized property name
foreach (var member in members)
{
if (string.Equals(name, selector(member)?.Replace("_", ""), StringComparison.Ordinal))
Expand Down
67 changes: 66 additions & 1 deletion tests/Dapper.Tests/ConstructorTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Data;
using System.Linq;
using Xunit;

Expand Down Expand Up @@ -241,6 +240,72 @@ public void CtorWithoutUnderscores()
Assert.Equal("def", obj.LastName);
}

[Fact]
public void Issue1993_PreferPropertyOverField() // https://github.com/DapperLib/Dapper/issues/1993
{
var oldValue = DefaultTypeMap.MatchNamesWithUnderscores;
try
{
DefaultTypeMap.MatchNamesWithUnderscores = true;

var map = new DefaultTypeMap(typeof(ShowIssue1993));
var first = map.GetMember("field_first");
Assert.NotNull(first);
Assert.Null(first.Field);
Assert.Equal(nameof(ShowIssue1993.FieldFirst), first.Property?.Name);

var last = map.GetMember("field_last");
Assert.NotNull(last);
Assert.Null(last.Field);
Assert.Equal(nameof(ShowIssue1993.FieldLast), last.Property?.Name);
}
finally
{
DefaultTypeMap.MatchNamesWithUnderscores = oldValue;
}
}

[Fact]
public void Issue1993_Query()
{
var oldValue = DefaultTypeMap.MatchNamesWithUnderscores;
try
{
DefaultTypeMap.MatchNamesWithUnderscores = true;

var obj = connection.QueryFirst<ShowIssue1993>("select 'abc' as field_first, 'def' as field_last");
Assert.Equal("abc", obj.FieldFirst);
Assert.Equal("def", obj.FieldLast);

Assert.Equal("abc", obj.AltFieldFirst);
Assert.Equal("def", obj.AltFieldLast);
}
finally
{
DefaultTypeMap.MatchNamesWithUnderscores = oldValue;
}
}

public class ShowIssue1993
{
private string _fieldFirst { get; set; } = null!; // not actually a field
public string FieldFirst
{
get => _fieldFirst;
set => _fieldFirst = AltFieldFirst = value;
}

public string FieldLast
{
get => _fieldLast;
set => _fieldLast = AltFieldLast = value;
}
private string _fieldLast { get; set; } = null!;// not actually a field

public string AltFieldFirst { get; set; } = null!;
public string AltFieldLast { get; set; } = null!;
}

class Type_ParamsWithUnderscores
{
public string FirstName { get; }
Expand Down

0 comments on commit 947b582

Please sign in to comment.