Skip to content

Commit 7bae265

Browse files
committed
Fix arrays over JsonDocument/JsonElement (#3747)
Fixes #3677 (cherry picked from commit e9528b4)
1 parent 87ead6e commit 7bae265

File tree

3 files changed

+17
-3
lines changed

3 files changed

+17
-3
lines changed

src/EFCore.PG/Storage/Internal/Mapping/NpgsqlArrayTypeMapping.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static RelationalTypeMappingParameters CreateParameters(string storeType
145145
if (elementJsonReaderWriter is not null && !typeof(TElement).UnwrapNullableType().IsAssignableTo(elementJsonReaderWriter.ValueType))
146146
{
147147
throw new InvalidOperationException(
148-
$"When building an array mapping over '{typeof(TElement).Name}', the JsonValueReaderWriter for element mapping '{elementMapping.GetType().Name}' is incorrect ('{elementJsonReaderWriter.ValueType.GetType().Name}' instead of '{typeof(TElement).UnwrapNullableType()}', the JsonValueReaderWriter is '{elementJsonReaderWriter.GetType().Name}').");
148+
$"When building an array mapping over '{typeof(TElement).Name}', the JsonValueReaderWriter for element mapping '{elementMapping.GetType().Name}' is incorrect ('{elementJsonReaderWriter.ValueType.Name}' instead of '{typeof(TElement).UnwrapNullableType()}', the JsonValueReaderWriter is '{elementJsonReaderWriter.GetType().Name}').");
149149
}
150150

151151
// If there's no JsonValueReaderWriter on the element, we also don't set one on its array (this is for rare edge cases such as

src/EFCore.PG/Storage/Internal/Mapping/NpgsqlJsonTypeMapping.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public NpgsqlJsonTypeMapping(string storeType, Type clrType, CoreTypeMapping? el
3232
storeType,
3333
clrType,
3434
storeType == "jsonb" ? NpgsqlDbType.Jsonb : NpgsqlDbType.Json,
35-
jsonValueReaderWriter: JsonStringReaderWriter.Instance,
35+
jsonValueReaderWriter: clrType == typeof(string) ? JsonStringReaderWriter.Instance : null,
3636
elementTypeMapping: elementTypeMapping)
3737
{
3838
if (storeType != "json" && storeType != "jsonb")

test/EFCore.PG.Tests/Storage/NpgsqlTypeMappingSourceTest.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System.Net;
22
using System.Net.NetworkInformation;
3+
using System.Text.Json;
34
using Microsoft.EntityFrameworkCore.Storage.Json;
45
using NetTopologySuite.Geometries;
56
using Npgsql.EntityFrameworkCore.PostgreSQL.Infrastructure;
67
using Npgsql.EntityFrameworkCore.PostgreSQL.Internal;
78
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal;
89
using Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.Mapping;
9-
using Npgsql.EntityFrameworkCore.PostgreSQL.TestUtilities;
1010

1111
namespace Npgsql.EntityFrameworkCore.PostgreSQL.Storage;
1212

@@ -313,6 +313,20 @@ public void Json_primitive_collection()
313313
Assert.Same(typeof(int), elementMapping.ClrType);
314314
}
315315

316+
[Theory]
317+
[InlineData(typeof(JsonDocument[]))]
318+
[InlineData(typeof(List<JsonDocument>))]
319+
[InlineData(typeof(JsonElement[]))]
320+
[InlineData(typeof(List<JsonElement>))]
321+
public void Array_of_json_type(Type clrType)
322+
{
323+
var mapping = CreateTypeMappingSource().FindMapping(clrType);
324+
Assert.NotNull(mapping);
325+
var arrayMapping = Assert.IsType<NpgsqlArrayTypeMapping>(mapping, exactMatch: false);
326+
Assert.Equal("jsonb[]", arrayMapping.StoreType);
327+
Assert.Same(clrType, arrayMapping.ClrType);
328+
}
329+
316330
#endregion JSON
317331

318332
#region Multirange

0 commit comments

Comments
 (0)