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
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>

<VersionPrefix>1.0.0</VersionPrefix>
<VersionSuffix>preview.2</VersionSuffix>
<VersionSuffix>preview.3</VersionSuffix>
</PropertyGroup>

<PropertyGroup>
Expand Down
16 changes: 16 additions & 0 deletions src/Ling.RemoteServices.AspNetCore/RemoteServiceServerRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Ling.RemoteServices.Exceptions;
Expand Down Expand Up @@ -106,6 +107,11 @@ public static JsonTypeInfo<T> GetJsonTypeInfo<T>(JsonSerializerOptions options)
{
ArgumentNullException.ThrowIfNull(options);

if (options.TypeInfoResolver is null && JsonSerializer.IsReflectionEnabledByDefault)
{
options.TypeInfoResolver = CreateReflectionJsonTypeInfoResolver();
}

try
{
if (options.GetTypeInfo(typeof(T)) is JsonTypeInfo<T> typeInfo)
Expand Down Expand Up @@ -146,6 +152,16 @@ private static InvalidOperationException CreateMissingJsonMetadataException<T>(
+ "with ConfigureHttpJsonOptions when using Native AOT.",
innerException);
}

[UnconditionalSuppressMessage(
"Trimming",
"IL2026",
Justification = "This fallback is reachable only when JSON reflection is enabled.")]
[UnconditionalSuppressMessage(
"AOT",
"IL3050",
Justification = "This fallback is reachable only when JSON reflection is enabled.")]
private static DefaultJsonTypeInfoResolver CreateReflectionJsonTypeInfoResolver() => new();
}

/// <summary>
Expand Down
16 changes: 16 additions & 0 deletions src/Ling.RemoteServices.Client/RemoteServiceClientRuntime.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Net.Http.Json;
using System.Text;
Expand Down Expand Up @@ -116,6 +117,11 @@ public static JsonTypeInfo<T> GetJsonTypeInfo<T>(JsonSerializerOptions options)
{
ArgumentNullException.ThrowIfNull(options);

if (options.TypeInfoResolver is null && JsonSerializer.IsReflectionEnabledByDefault)
{
options.TypeInfoResolver = CreateReflectionJsonTypeInfoResolver();
}

try
{
if (options.GetTypeInfo(typeof(T)) is JsonTypeInfo<T> typeInfo)
Expand Down Expand Up @@ -195,4 +201,14 @@ private static InvalidOperationException CreateMissingJsonMetadataException<T>(
+ "with RemoteServiceClientOptions.AddJsonSerializerContext when using Native AOT.",
innerException);
}

[UnconditionalSuppressMessage(
"Trimming",
"IL2026",
Justification = "This fallback is reachable only when JSON reflection is enabled.")]
[UnconditionalSuppressMessage(
"AOT",
"IL3050",
Justification = "This fallback is reachable only when JSON reflection is enabled.")]
private static DefaultJsonTypeInfoResolver CreateReflectionJsonTypeInfoResolver() => new();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Text.Json;
using System.Text.Json.Serialization.Metadata;
using Ling.RemoteServices.AspNetCore;

namespace Ling.RemoteServices.AspNetCore.Tests;

public class RemoteServiceServerRuntimeTests
{
[Fact]
public void GetJsonTypeInfo_uses_reflection_metadata_by_default_on_jit_hosts()
{
var options = new JsonSerializerOptions(JsonSerializerDefaults.Web);

var typeInfo = RemoteServiceServerRuntime.GetJsonTypeInfo<TestPayload>(options);

Assert.Equal(typeof(TestPayload), typeInfo.Type);
Assert.IsType<DefaultJsonTypeInfoResolver>(options.TypeInfoResolver);
}

private sealed record TestPayload(string Value);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@ namespace Ling.RemoteServices.Client.Tests;

public class RemoteServiceRuntimeTests
{
[Fact]
public void GetJsonTypeInfo_uses_reflection_metadata_by_default_on_jit_hosts()
{
var options = new RemoteServiceClientOptions();

var typeInfo = RemoteServiceClientRuntime.GetJsonTypeInfo<ClientTestPayload>(
options.JsonSerializerOptions);

Assert.Equal(typeof(ClientTestPayload), typeInfo.Type);
Assert.IsType<System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver>(
options.JsonSerializerOptions.TypeInfoResolver);
}

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