DapperLib / DapperLib/Dapper

Int32 to Bool mapping behaves differently for positional constructor than for property

Open
#1,966 3 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C#
Stars
18.4k
Forks
3.7k
Avg merge
5h 8m
Merged PRs (30d)
1

Description

This may or may not be a bug but the behavior seemed interesting, so I thought I'd ask.

I isolated this test case, where two identical query strings fill objects with the same shape but different constructors. The one mapped by constructor does not work, but the one mapped by property does work.

private record InConstructor(bool B);

[Fact] // This one will fail
public void TestFillInConstructor()
{
    InConstructor ctor = connection.Query<InConstructor>("select 1 B").First();
    Assert.True(ctor.B);
}

private record InProperty
{
    public bool B { get; init; }
}

[Fact] // This one will succeed
public void TestFillInProperty()
{
    InProperty prop = connection.Query<InProperty>("select 1 B").First();
    Assert.True(prop.B);
}

It looks like the constructor version is looking for an Int32 in the position where Bool exists, since the returned type from the SQL is an int

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the two record-mapping tests from the issue against the Dapper query path, comparing positional constructor binding with property binding for select 1 B. Trace where the returned Int32 is matched to the constructor's Bool parameter; done means the two equivalent object shapes map consistently, with regression coverage for both cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sql
Domain
backend-api-design, databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.