Skip to content

Commit 604c00c

Browse files
onionhammerNinoFlorisroji
authored
If 'out of order properties' enabled, allow type discriminator on JSONB types (#5932)
Fixes #5937 Co-authored-by: Nino Floris <mail@ninofloris.com> Co-authored-by: Shay Rojansky <roji@roji.org>
1 parent 7a54da0 commit 604c00c

4 files changed

Lines changed: 212 additions & 84 deletions

File tree

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<PackageVersion Include="OpenTelemetry.API" Version="1.7.0" />
66

77
<!-- Compatibility -->
8-
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
8+
<PackageVersion Include="System.Text.Json" Version="9.0.0" />
99
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="8.0.1" />
1010
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
1111

src/Npgsql/Internal/ResolverFactories/JsonDynamicTypeInfoResolverFactory.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,15 @@ void AddUserMappings(bool jsonb, Type[] clrTypes)
9494
if (!jsonType.IsValueType && jsonTypeInfo.PolymorphismOptions is not null)
9595
{
9696
foreach (var derived in jsonTypeInfo.PolymorphismOptions.DerivedTypes)
97+
{
98+
// For jsonb we can't properly support polymorphic serialization unless the SerializerOptions.AllowOutOfOrderMetadataProperties is `true`.
99+
// If `jsonb` AND `AllowOutOfOrderMetadataProperties` is `false`, use `derived.DerivedType` as the base type for the converter,
100+
// this causes STJ to stop serializing the "$type" field; essentially disabling the feature.
101+
var baseType = jsonb && !serializerOptions.AllowOutOfOrderMetadataProperties ? derived.DerivedType : jsonType;
97102
dynamicMappings.AddMapping(derived.DerivedType, dataTypeName,
98103
factory: (options, mapping, _) => mapping.CreateInfo(options,
99-
CreateSystemTextJsonConverter(mapping.Type, jsonb, options.TextEncoding, serializerOptions, jsonType)));
104+
CreateSystemTextJsonConverter(mapping.Type, jsonb, options.TextEncoding, serializerOptions, baseType)));
105+
}
100106
}
101107
}
102108
mappings.AddRange(dynamicMappings.ToTypeInfoMappingCollection());
@@ -116,9 +122,10 @@ void AddUserMappings(bool jsonb, Type[] clrTypes)
116122
{
117123
var jsonb = dataTypeName == DataTypeNames.Jsonb;
118124

119-
// For jsonb we can't properly support polymorphic serialization unless we do quite some additional work
120-
// so we default to mapping.Type instead (exact types will never serialize their "$type" fields, essentially disabling the feature).
121-
var baseType = jsonb ? mapping.Type : typeof(object);
125+
// For jsonb we can't properly support polymorphic serialization unless the SerializerOptions.AllowOutOfOrderMetadataProperties is `true`.
126+
// If `jsonb` AND `AllowOutOfOrderMetadataProperties` is `false`, use `mapping.Type` as the base type for the converter,
127+
// this causes STJ to stop serializing the "$type" field; essentially disabling the feature.
128+
var baseType = jsonb && !SerializerOptions.AllowOutOfOrderMetadataProperties ? mapping.Type : typeof(object);
122129

123130
return mapping.CreateInfo(options,
124131
CreateSystemTextJsonConverter(mapping.Type, jsonb, options.TextEncoding, SerializerOptions, baseType));

src/Npgsql/Npgsql.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222

2323
<ItemGroup Condition=" '$(TargetFramework)' == 'net8.0' ">
2424
<PackageReference Include="Microsoft.CodeAnalysis.PublicApiAnalyzers" PrivateAssets="All" />
25+
<PackageReference Include="System.Text.Json" /> <!-- For AllowOutOfOrderMetadataProperties -->
2526
</ItemGroup>
2627

27-
2828
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">
2929
<PackageReference Include="System.Text.Json" />
3030
<PackageReference Include="System.Runtime.CompilerServices.Unsafe" />

0 commit comments

Comments
 (0)