Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ protected override string GenerateNonNullSqlLiteral(object value)
}
case string s:
return $"'{EscapeSqlLiteral(s)}'";
case byte[] bytes:
return $"'{EscapeSqlLiteral(Encoding.UTF8.GetString(bytes))}'";
default: // User POCO
return $"'{EscapeSqlLiteral(JsonSerializer.Serialize(value))}'";
}
Expand Down
41 changes: 41 additions & 0 deletions test/EFCore.PG.FunctionalTests/Query/JsonStringQueryTest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
using System.Text.Json;
using Npgsql.EntityFrameworkCore.PostgreSQL.TestUtilities;

Expand Down Expand Up @@ -92,6 +93,33 @@ LIMIT 2
""");
}

[Theory]
[InlineData(false)]
[InlineData(true)]
public async Task Byte_array_mapped_to_json_column_in_predicate(bool async)
{
using var ctx = CreateContext();

var json = Encoding.UTF8.GetBytes("""{"Foo":"O'Reilly"}""");
var parameterQuery = ctx.JsonByteArrayEntities.Where(e => e.Json == json);
var parameterResult = async ? await parameterQuery.SingleAsync() : parameterQuery.Single();
Assert.Equal(1, parameterResult.Id);

Fixture.TestSqlLoggerFactory.Clear();

var constantQuery = ctx.JsonByteArrayEntities.Where(e => e.Json == EF.Constant(json));
var constantResult = async ? await constantQuery.SingleAsync() : constantQuery.Single();
Assert.Equal(1, constantResult.Id);

AssertSql(
"""
SELECT j."Id", j."Json"
FROM "JsonByteArrayEntities" AS j
WHERE j."Json" = '{"Foo":"O''Reilly"}'
LIMIT 2
""");
}

#region Functions

[Fact]
Expand Down Expand Up @@ -251,6 +279,8 @@ public class JsonStringQueryContext : PoolableDbContext
{
public DbSet<JsonEntity> JsonEntities { get; set; }

public DbSet<JsonByteArrayEntity> JsonByteArrayEntities { get; set; }

public JsonStringQueryContext(DbContextOptions options)
: base(options)
{
Expand Down Expand Up @@ -336,6 +366,9 @@ public static void Seed(JsonStringQueryContext context)
CustomerJsonb = array,
CustomerJson = array
});
context.JsonByteArrayEntities.AddRange(
new JsonByteArrayEntity { Id = 1, Json = """{"Foo":"O'Reilly"}"""u8.ToArray() },
new JsonByteArrayEntity { Id = 2, Json = """{"Foo":"Bar"}"""u8.ToArray() });
context.SaveChanges();
}
}
Expand All @@ -353,6 +386,14 @@ public class JsonEntity
public string SomeString { get; set; }
}

public class JsonByteArrayEntity
{
public int Id { get; set; }

[Column(TypeName = "jsonb")]
public byte[] Json { get; set; }
}

public class JsonStringQueryFixture : SharedStoreFixtureBase<JsonStringQueryContext>
{
protected override string StoreName
Expand Down
15 changes: 15 additions & 0 deletions test/EFCore.PG.Tests/Storage/NpgsqlTypeMappingTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,6 +1012,21 @@ public void GenerateSqlLiteral_returns_jsonb_string_literal()
public void GenerateSqlLiteral_returns_json_string_literal()
=> Assert.Equal("""'{"a":1}'""", GetMapping("json").GenerateSqlLiteral("""{"a":1}"""));

[Theory]
[InlineData("json")]
[InlineData("jsonb")]
public void GenerateSqlLiteral_returns_json_byte_array_literal(string storeType)
{
var mapping = GetMapping(typeof(byte[]), storeType);

Assert.Equal("""'{"a":1}'""", mapping.GenerateSqlLiteral("""{"a":1}"""u8.ToArray()));
Assert.Equal(
"'{\"Name\":\"O''Reilly \u00e9\"}'",
mapping.GenerateSqlLiteral(Encoding.UTF8.GetBytes("{\"Name\":\"O'Reilly \u00e9\"}")));
Assert.Equal("'[1,2]'", mapping.GenerateSqlLiteral("[1,2]"u8.ToArray()));
Assert.Equal("'null'", mapping.GenerateSqlLiteral("null"u8.ToArray()));
}

[Fact]
public void GenerateSqlLiteral_returns_jsonb_object_literal()
{
Expand Down
Loading