Skip to content

Commit 1d93236

Browse files
authored
STJ 9.0 alternative approach (#5941)
1 parent 94de20f commit 1d93236

File tree

5 files changed

+38
-6
lines changed

5 files changed

+38
-6
lines changed

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
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="9.0.0" />
8+
<PackageVersion Include="System.Text.Json" Version="8.0.5" />
99
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="8.0.1" />
1010
<PackageVersion Include="System.Runtime.CompilerServices.Unsafe" Version="6.0.0" />
1111

@@ -44,4 +44,4 @@
4444
<!-- NativeAOT -->
4545
<PackageVersion Include="Mono.Cecil" Version="0.11.5" />
4646
</ItemGroup>
47-
</Project>
47+
</Project>

src/Npgsql/Internal/ResolverFactories/JsonDynamicTypeInfoResolverFactory.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ JsonSerializerOptions SerializerOptions
5353
readonly Type[] _jsonClrTypes = jsonClrTypes ?? [];
5454
TypeInfoMappingCollection? _mappings;
5555

56+
#if NET9_0_OR_GREATER
57+
static Func<JsonSerializerOptions, bool> AllowOutOfOrderMetadataProperties { get; } = options => options.AllowOutOfOrderMetadataProperties;
58+
#else
59+
static Func<JsonSerializerOptions, bool> AllowOutOfOrderMetadataProperties { get; } =
60+
typeof(JsonSerializerOptions).GetProperty("AllowOutOfOrderMetadataProperties") is { } prop && prop.GetGetMethod() is { } getProp
61+
? getProp.CreateDelegate<Func<JsonSerializerOptions, bool>>()
62+
: _ => false;
63+
#endif
5664
protected TypeInfoMappingCollection Mappings => _mappings ??= AddMappings(new(), _jsonbClrTypes, _jsonClrTypes, SerializerOptions);
5765

5866
public new PgTypeInfo? GetTypeInfo(Type? type, DataTypeName? dataTypeName, PgSerializerOptions options)
@@ -98,7 +106,7 @@ void AddUserMappings(bool jsonb, Type[] clrTypes)
98106
// For jsonb we can't properly support polymorphic serialization unless the SerializerOptions.AllowOutOfOrderMetadataProperties is `true`.
99107
// If `jsonb` AND `AllowOutOfOrderMetadataProperties` is `false`, use `derived.DerivedType` as the base type for the converter,
100108
// this causes STJ to stop serializing the "$type" field; essentially disabling the feature.
101-
var baseType = jsonb && !serializerOptions.AllowOutOfOrderMetadataProperties ? derived.DerivedType : jsonType;
109+
var baseType = jsonb && !AllowOutOfOrderMetadataProperties(serializerOptions) ? derived.DerivedType : jsonType;
102110
dynamicMappings.AddMapping(derived.DerivedType, dataTypeName,
103111
factory: (options, mapping, _) => mapping.CreateInfo(options,
104112
CreateSystemTextJsonConverter(mapping.Type, jsonb, options.TextEncoding, serializerOptions, baseType)));
@@ -125,7 +133,7 @@ void AddUserMappings(bool jsonb, Type[] clrTypes)
125133
// For jsonb we can't properly support polymorphic serialization unless the SerializerOptions.AllowOutOfOrderMetadataProperties is `true`.
126134
// If `jsonb` AND `AllowOutOfOrderMetadataProperties` is `false`, use `mapping.Type` as the base type for the converter,
127135
// this causes STJ to stop serializing the "$type" field; essentially disabling the feature.
128-
var baseType = jsonb && !SerializerOptions.AllowOutOfOrderMetadataProperties ? mapping.Type : typeof(object);
136+
var baseType = jsonb && !AllowOutOfOrderMetadataProperties(SerializerOptions) ? mapping.Type : typeof(object);
129137

130138
return mapping.CreateInfo(options,
131139
CreateSystemTextJsonConverter(mapping.Type, jsonb, options.TextEncoding, SerializerOptions, baseType));

src/Npgsql/Npgsql.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222

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

2827
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' ">

test/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<Import Project="../Directory.Build.props" />
33

44
<PropertyGroup>
5-
<TargetFramework>net8.0</TargetFramework>
5+
<TargetFrameworks>net8.0;net9.0</TargetFrameworks>
66
<IsPackable>false</IsPackable>
77

88
<!-- Suppress warnings for [RequiresPreviewFeatures] (<EnablePreviewFeatures> doesn't seem to work across <ProjectReference>) -->

test/Npgsql.Tests/Types/JsonDynamicTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,17 @@ await AssertType(
220220
[Test]
221221
public async Task Poco_polymorphic_mapping()
222222
{
223+
#if !NET9_0_OR_GREATER
224+
if (IsJsonb)
225+
return;
226+
#endif
223227
await using var dataSource = CreateDataSource(builder =>
224228
{
225229
var types = new[] {typeof(WeatherForecast)};
226230
builder
231+
#if NET9_0_OR_GREATER
227232
.ConfigureJsonOptions(new() { AllowOutOfOrderMetadataProperties = true })
233+
#endif
228234
.EnableDynamicJson(jsonClrTypes: IsJsonb ? [] : types, jsonbClrTypes: !IsJsonb ? [] : types);
229235
});
230236

@@ -249,11 +255,17 @@ public async Task Poco_polymorphic_mapping()
249255
[Test]
250256
public async Task Poco_polymorphic_mapping_read_parents()
251257
{
258+
#if !NET9_0_OR_GREATER
259+
if (IsJsonb)
260+
return;
261+
#endif
252262
await using var dataSource = CreateDataSource(builder =>
253263
{
254264
var types = new[] {typeof(WeatherForecast)};
255265
builder
266+
#if NET9_0_OR_GREATER
256267
.ConfigureJsonOptions(new() { AllowOutOfOrderMetadataProperties = true })
268+
#endif
257269
.EnableDynamicJson(jsonClrTypes: IsJsonb ? [] : types, jsonbClrTypes: !IsJsonb ? [] : types);
258270
});
259271

@@ -293,7 +305,9 @@ public async Task Poco_exact_polymorphic_mapping()
293305
{
294306
var types = new[] {typeof(ExtendedDerivedWeatherForecast)};
295307
builder
308+
#if NET9_0_OR_GREATER
296309
.ConfigureJsonOptions(new() { AllowOutOfOrderMetadataProperties = true })
310+
#endif
297311
.EnableDynamicJson(jsonClrTypes: IsJsonb ? [] : types, jsonbClrTypes: !IsJsonb ? [] : types);
298312
});
299313

@@ -318,10 +332,17 @@ public async Task Poco_exact_polymorphic_mapping()
318332
[Test]
319333
public async Task Poco_unspecified_polymorphic_mapping()
320334
{
335+
#if !NET9_0_OR_GREATER
336+
if (IsJsonb)
337+
return;
338+
#endif
339+
321340
await using var dataSource = CreateDataSource(builder =>
322341
{
323342
builder
343+
#if NET9_0_OR_GREATER
324344
.ConfigureJsonOptions(new() { AllowOutOfOrderMetadataProperties = true })
345+
#endif
325346
.EnableDynamicJson();
326347
});
327348

@@ -360,7 +381,9 @@ public async Task Poco_polymorphic_mapping_without_AllowOutOfOrderMetadataProper
360381
{
361382
var types = new[] {typeof(WeatherForecast)};
362383
builder
384+
#if NET9_0_OR_GREATER
363385
.ConfigureJsonOptions(new() { AllowOutOfOrderMetadataProperties = false })
386+
#endif
364387
.EnableDynamicJson(jsonClrTypes: IsJsonb ? [] : types, jsonbClrTypes: !IsJsonb ? [] : types);
365388
});
366389

@@ -413,7 +436,9 @@ public async Task Poco_unspecified_polymorphic_mapping_without_AllowOutOfOrderMe
413436
await using var dataSource = CreateDataSource(builder =>
414437
{
415438
builder
439+
#if NET9_0_OR_GREATER
416440
.ConfigureJsonOptions(new() { AllowOutOfOrderMetadataProperties = false })
441+
#endif
417442
.EnableDynamicJson();
418443
});
419444

0 commit comments

Comments
 (0)