Skip to content

PostgreSQL SMALLINT[] mapping fails in Dapper 2.1.86 but works in 2.1.79 #2231

Closed

Description

@Kutegz

PostgreSQL SMALLINT[] mapping fails in Dapper 2.1.86 but works in Dapper 2.1.79.

Environment

  • .NET 10
  • PostgreSQL
  • Npgsql
  • Dapper 2.1.86

Database

CREATE TABLE auto_numbers
(
    item_name TEXT NOT NULL,
    auto_column_name TEXT NOT NULL,
    separator_positions SMALLINT[] NOT NULL DEFAULT ARRAY[]::SMALLINT[],
    PRIMARY KEY (item_name, auto_column_name)
);

Example value:

INSERT INTO auto_numbers
    (item_name, auto_column_name, separator_positions)
VALUES
    ('queue_visits', 'token_no', ARRAY[1]::SMALLINT[]);

Model

public sealed record AutoNumberRequest
{
    public required string ItemName { get; init; }
    public required string AutoColumnName { get; init; }
    public required IReadOnlyList<short> SeparatorPositions { get; init; } = [];
}

Query

const string sql = """
    SELECT
        item_name "ItemName",
        auto_column_name "AutoColumnName",
        separator_positions "SeparatorPositions"
    FROM auto_numbers
    WHERE item_name = @ItemName
      AND auto_column_name = @AutoColumnName
    """;

var result = await connection.QuerySingleAsync<AutoNumberRequest>(
    sql,
    new
    {
        ItemName = "queue_visits",
        AutoColumnName = "token_no"
    });

Error with Dapper 2.1.86

System.Data.DataException:
Error parsing column 2 (SeparatorPositions=System.Int16[] - Int16[])

System.InvalidCastException:
Object must implement IConvertible.

   at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider)
   ...

Npgsql returns the PostgreSQL SMALLINT[] value as System.Int16[].

Version comparison

With Dapper 2.1.79:

<PackageReference Include="Dapper" Version="[2.1.79]" />

the same query and model work correctly.

With Dapper 2.1.86:

<PackageReference Include="Dapper" Version="[2.1.86]" />

the query fails with the exception above.

I also tested changing the property from:

IReadOnlyList<short>

to:

short[]

and short[] also fails with Dapper 2.1.86.

No other application or database changes were made between the tests.

This appears to be a regression in Dapper 2.1.86 when reading PostgreSQL SMALLINT[] values through Npgsql.

Dapper 2.1.79 continues to work correctly with the same PostgreSQL database, Npgsql setup, query, and model.

Activity

  1. zotov-y-tg commented on Nov 12, 2026

    @zotov-y-tg

    We hit the same regression in production after upgrading Dapper from 2.0.123 to 2.1.89. PostgreSQL integer[] columns mapped to IReadOnlyCollection<int> properties now throw:

    System.Data.DataException: Error parsing column 16 (RequiredProviders=System.Int32[] - Int32[])
     ---> System.InvalidCastException: Object must implement IConvertible.
    

    Setup: .NET 10, Npgsql 9.0.5. Bisected: 2.1.79 works; 2.1.86 and 2.1.89 fail. We have pinned 2.1.79 for now.

    #2239 and #2241 both fix this. Could one of them be merged and released?

  2. mgravell commented on Nov 20, 2026

    @mgravell
    Member

    Fixed by #2271, merged to main. The fix restores the runtime-checked cast for array columns into interface members, and covers both Npgsql GetFieldType shapes: the concrete array type (Npgsql 9.0.0–9.0.4) and typeof(Array) (Npgsql 7, 8 and 10) — the latter is what the production reports here were hitting, and is now pinned by tests that run in CI against real PostgreSQL under Npgsql 10. This will ship in the next release; the 2.1.79 pin can be lifted then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions