DapperLib / DapperLib/Dapper

Mapping '-infinity' to DateTimeOffset fails when used with Npgsql

Open
#1,791 2 comments 0 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

The following code

var result = connection.QuerySingleOrDefault<Dto>("SELECT '-infinity'::timestamptz AS Value");

public class Dto
{
    public DateTimeOffset Value { get; set; }
}

throws a DataException: Error parsing column 0 (value=1/1/0001 12:00:00 AM - DateTime) with the stack trace:

   at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in /_/Dapper/SqlMapper.cs:line 3706
   at Deserialized3c1b0f9-a4d0-4ee4-9b2d-42e7fb1f3c42(IDataReader )
   at Dapper.SqlMapper.QueryRowImpl[T](IDbConnection cnn, Row row, CommandDefinition& command, Type effectiveType) in /_/Dapper/SqlMapper.cs:line 1198
   at Dapper.SqlMapper.QuerySingleOrDefault[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in /_/Dapper/SqlMapper.cs:line 814
   at UserQuery.Main(), line 3

Expected result is no exception thrown, DateTimeOffset.MinValue returned in Dto.Value

I'm using Dapper v2.0.123 and Npgsql v6.0.5

This issue might be related to the discussion here #1716 and the fact that Npgsql returns -infinity as DateTime.MinValue, which throws when casted to DateTimeOffset if local timezone is UTC+N. Could it be somehow treated as a special case then?

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 with the conversion path in SqlMapper.cs around the reported line 3706 and reproduce the sample using Npgsql's timestamptz '-infinity' value. Trace the DateTime-to-DateTimeOffset handling and add a regression test if the project has coverage for this path; done means the query returns DateTimeOffset.MinValue without throwing.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, postgresql
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.