Repository navigation
Fix hstore SQL literal generation for null and escaped values - #3780
Conversation
There was a problem hiding this comment.
Pull request overview
Fixes SQL literal generation for PostgreSQL hstore in NpgsqlHstoreTypeMapping to correctly handle null values, empty dictionaries, and escaping of special characters, with unit tests covering the corrected behavior.
Changes:
- Reworked
hstoreSQL literal generation to emit separators correctly and support empty dictionaries. - Added proper escaping for
\,"and'when generatinghstoreSQL literals. - Added unit tests for null values, escaping, and empty dictionaries.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/EFCore.PG/Storage/Internal/Mapping/NpgsqlHstoreTypeMapping.cs |
Fixes hstore SQL literal formatting/escaping and handles empty dictionaries safely. |
test/EFCore.PG.Tests/Storage/NpgsqlTypeMappingTest.cs |
Adds unit test coverage for the fixed hstore literal generation edge cases. |
|
Here is a simple Program.cs that exercises the changes: using var dbContext = new ExampleDbContext();
dbContext.Database.EnsureDeleted();
dbContext.Database.EnsureCreated();
dbContext.Blogs.Add(new Blog { Name = "ABC"});
dbContext.SaveChanges();
var blog = dbContext.Blogs.Single(b => b.Name == "ABC");
foreach (var item in blog.Features!)
{
Console.WriteLine($"{item.Key}: {item.Value ?? "<NULL>"}");
}
public class ExampleDbContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.HasPostgresExtension("hstore");
modelBuilder.Entity<Blog>()
.Property(x => x.Features)
.HasDefaultValue(new Dictionary<string, string?>
{
["k1"] = null,
["k\"2\""] = "v\"2\"",
["k'3'"] = "v'3'",
["k\\4"] = "v\\4"
});
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseNpgsql("Host=localhost;Username=postgres;Password=postgres;Database=ExampleDb")
.LogTo(Console.WriteLine, LogLevel.Information)
.EnableSensitiveDataLogging();
}
}
public class Blog
{
public int Id { get; set; }
public required string Name { get; set; }
public Dictionary<string, string?>? Features { get; set; }
}Logged table creation: CREATE TABLE "Blogs" (
"Id" integer GENERATED BY DEFAULT AS IDENTITY,
"Name" text NOT NULL,
"Features" hstore DEFAULT HSTORE '"k1"=>NULL,"k\"2\""=>"v\"2\"","k''3''"=>"v''3''","k\\4"=>"v\\4"',
CONSTRAINT "PK_Blogs" PRIMARY KEY ("Id")
);Output: For contrast, running this test program without the PR results in a syntax error because of lack of escape handling: |
Co-authored-by: Copilot App <[email protected]>
There was a problem hiding this comment.
Thanks @brianpursley, LGTM and sorry it took so long to get this reviewed. Rebased, pushed a tiny commit to improve test coverage, and will merge and backport.
If you want, post a comment on #4103 and I'll assign it to you for credit.
…#4107) (cherry picked from commit 157ae7f) Co-authored-by: Brian Pursley <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Copilot App <[email protected]>
…#4105) (cherry picked from commit 157ae7f) Co-authored-by: Brian Pursley <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Copilot App <[email protected]>
…#4106) (cherry picked from commit 157ae7f) Co-authored-by: Brian Pursley <[email protected]> Co-authored-by: Copilot <[email protected]> Co-authored-by: Copilot App <[email protected]>
Summary
Fix hstore SQL literal generation bugs in NpgsqlHstoreTypeMapping.
Fixes #4103.
Details
The previous implementation produced invalid or corrupted literals in several cases:
This PR updates hstore literal generation to:
\,"and'appropriately for use inside the generated SQL literalUnit tests were added for each of these fixed cases.