Repository navigation
Description
Problem
NpgsqlHstoreTypeMapping.GenerateNonNullSqlLiteral produces invalid SQL or silently changes dictionary contents when an hstore literal contains null values, backslashes, or quotes, or when the dictionary is empty. This affects SQL literals used by, for example, .HasDefaultValue(...) in generated table DDL.
These cases were reproduced against the current-main implementation at 150011d, using PostgreSQL 18.4 with standard_conforming_strings=on.
Minimal reproductions and actual results
The following calls exercise the literal generator directly:
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;
var mapping = NpgsqlHstoreTypeMapping.Default;Null values can silently become strings
mapping.GenerateSqlLiteral(new Dictionary<string, string?> { ["k"] = null });Actual generated SQL:
HSTORE '"k"=>NUL'SELECT of that literal succeeds, but returns {"k":"NUL"} rather than {"k":null}: the last character of NULL is removed.
A null value before another entry also corrupts the result:
mapping.GenerateSqlLiteral(new Dictionary<string, string?>
{
["k1"] = null,
["k2"] = "v2"
});Actual generated SQL:
HSTORE '"k1"=>NULL"k2"=>"v2"'PostgreSQL accepts this as a single entry whose k1 value is the string NULL"k2"=>"v2"; the k2 entry is lost.
An empty dictionary produces unterminated SQL
mapping.GenerateSqlLiteral(new Dictionary<string, string?>());Actual output is HSTORE ' (only one apostrophe), so executing it fails with SQLSTATE 42601. Expected: HSTORE ''.
Backslashes are lost
mapping.GenerateSqlLiteral(new Dictionary<string, string?> { ["k\\2"] = "v\\2" });Actual generated SQL:
HSTORE '"k\2"=>"v\2"'PostgreSQL returns {"k2":"v2"} instead of preserving the backslashes in both the key and value.
Quotes cause syntax errors
mapping.GenerateSqlLiteral(new Dictionary<string, string?> { ["k\"1\""] = "v\"1\"" });
// HSTORE '"k"1""=>"v"1""'
mapping.GenerateSqlLiteral(new Dictionary<string, string?> { ["k'3'"] = "v'3'" });
// HSTORE '"k'3'"=>"v'3'"'Both generated literals fail with SQLSTATE 42601 when executed.
Expected behavior
Generated SQL should preserve every dictionary entry, distinguish null from strings, support empty dictionaries, and correctly escape keys and values at both layers: hstore text syntax and the enclosing SQL string literal.
A normal application encounters the same path through a default value such as:
modelBuilder.HasPostgresExtension("hstore");
modelBuilder.Entity<Blog>()
.Property(b => b.Features) // Dictionary<string, string?>
.HasDefaultValue(new Dictionary<string, string?> { ["k"] = null });The resulting column default must contain HSTORE '"k"=>NULL', not HSTORE '"k"=>NUL'.
Related work
Fix: #3780.
This is SQL-literal generation, separate from the C# code-literal generation/scaffolding issue in #1318.
Activity