Skip to content

Commit 76ada6b

Browse files
authored
Sync well known text types to actual mapping list (#5617)
Fixes #5599
1 parent 3ceb33f commit 76ada6b

5 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/Npgsql.Json.NET/Internal/JsonNetPocoTypeInfoResolverFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ static void AddUserMappings(TypeInfoMappingCollection mappings, bool jsonb, Type
7373
protected override DynamicMappingCollection? GetMappings(Type? type, DataTypeName dataTypeName, PgSerializerOptions options)
7474
{
7575
// Match all types except null, object and text types as long as DataTypeName (json/jsonb) is present.
76-
if (type is null || type == typeof(object) || Array.IndexOf(PgSerializerOptions.WellKnownTextTypes, type) != -1
76+
if (type is null || type == typeof(object) || PgSerializerOptions.IsWellKnownTextType(type)
7777
|| dataTypeName != JsonbDataTypeName && dataTypeName != JsonDataTypeName)
7878
return null;
7979

src/Npgsql/Internal/PgSerializerOptions.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Runtime.CompilerServices;
34
using System.Text;
45
using Npgsql.Internal.Postgres;
@@ -57,11 +58,15 @@ public IPgTypeInfoResolver TypeInfoResolver
5758
public ArrayNullabilityMode ArrayNullabilityMode { get; init; } = ArrayNullabilityMode.Never;
5859
public INpgsqlNameTranslator DefaultNameTranslator { get; init; } = NpgsqlSnakeCaseNameTranslator.Instance;
5960

60-
public static Type[] WellKnownTextTypes { get; } = {
61-
typeof(string), typeof(char[]), typeof(byte[]),
62-
typeof(ArraySegment<char>), typeof(ArraySegment<char>?),
63-
typeof(char), typeof(char?)
64-
};
61+
public static bool IsWellKnownTextType(Type type)
62+
{
63+
type = type.IsValueType ? Nullable.GetUnderlyingType(type) ?? type : type;
64+
return Array.IndexOf([
65+
typeof(string), typeof(char),
66+
typeof(char[]), typeof(ReadOnlyMemory<char>), typeof(ArraySegment<char>),
67+
typeof(byte[]), typeof(ReadOnlyMemory<byte>)
68+
], type) != -1 || typeof(Stream).IsAssignableFrom(type);
69+
}
6570

6671
internal bool RangesEnabled => _resolverChain.RangesEnabled;
6772
internal bool MultirangesEnabled => _resolverChain.MultirangesEnabled;

src/Npgsql/Internal/ResolverFactories/AdoTypeInfoResolverFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ static TypeInfoMappingCollection AddMappings(TypeInfoMappingCollection mappings)
7575
static (options, mapping, _) => mapping.CreateInfo(options, new MoneyConverter<decimal>()), MatchRequirement.DataTypeName);
7676

7777
// Text
78+
// Update PgSerializerOptions.IsWellKnownTextType(Type) after any changes to this list.
7879
mappings.AddType<string>(DataTypeNames.Text,
7980
static (options, mapping, _) => mapping.CreateInfo(options, new StringTextConverter(options.TextEncoding), preferredFormat: DataFormat.Text), isDefault: true);
8081
mappings.AddStructType<char>(DataTypeNames.Text,

src/Npgsql/Internal/ResolverFactories/ExtraConversionsTypeInfoResolverFactory.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ static TypeInfoMappingCollection AddInfos(TypeInfoMappingCollection mappings)
104104
static (options, mapping, _) => mapping.CreateInfo(options, new StringBitStringConverter()));
105105

106106
// Text
107+
// Update PgSerializerOptions.IsWellKnownTextType(Type) after any changes to this list.
107108
mappings.AddType<char[]>(DataTypeNames.Text,
108109
static (options, mapping, _) => mapping.CreateInfo(options, new CharArrayTextConverter(options.TextEncoding), preferredFormat: DataFormat.Text));
109110
mappings.AddStructType<ReadOnlyMemory<char>>(DataTypeNames.Text,

src/Npgsql/Internal/ResolverFactories/JsonDynamicTypeInfoResolverFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ void AddUserMappings(bool jsonb, Type[] clrTypes)
121121
protected override DynamicMappingCollection? GetMappings(Type? type, DataTypeName dataTypeName, PgSerializerOptions options)
122122
{
123123
// Match all types except null, object and text types as long as DataTypeName (json/jsonb) is present.
124-
if (type is null || type == typeof(object) || Array.IndexOf(PgSerializerOptions.WellKnownTextTypes, type) != -1
124+
if (type is null || type == typeof(object) || PgSerializerOptions.IsWellKnownTextType(type)
125125
|| dataTypeName != DataTypeNames.Jsonb && dataTypeName != DataTypeNames.Json)
126126
return null;
127127

0 commit comments

Comments
 (0)