Skip to content

Commit 35c4f33

Browse files
committed
More PR feedback
1 parent 75a71f6 commit 35c4f33

File tree

11 files changed

+67
-76
lines changed

11 files changed

+67
-76
lines changed

src/Components/Server/src/Circuits/RemoteJSRuntime.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public RemoteJSRuntime(
4848
DefaultAsyncTimeout = _options.JSInteropDefaultCallTimeout;
4949
ElementReferenceContext = new WebElementReferenceContext(this);
5050
JsonSerializerOptions.Converters.Add(new ElementReferenceJsonConverter(ElementReferenceContext));
51-
JsonSerializerOptions.MakeReadOnly();
51+
JsonSerializerOptions.MakeReadOnly(populateMissingResolver: JsonSerializer.IsReflectionEnabledByDefault);
5252
}
5353

5454
internal void Initialize(CircuitClientProxy clientProxy)

src/Components/Web/src/WebRenderer.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ public WebRenderer(
4444

4545
var jsRuntime = serviceProvider.GetRequiredService<IJSRuntime>();
4646
var jsRuntimeJsonSerializerOptions = new JsonSerializerOptions(jsRuntime.JsonSerializerOptions);
47-
jsRuntimeJsonSerializerOptions.TypeInfoResolverChain.Insert(0, JsonConverterFactoryTypeInfoResolver<DotNetObjectReference<WebRendererInteropMethods>>.Instance);
48-
jsRuntimeJsonSerializerOptions.TypeInfoResolverChain.Insert(0, WebRendererSerializerContext.Default);
47+
jsRuntimeJsonSerializerOptions.TypeInfoResolverChain.Clear();
48+
jsRuntimeJsonSerializerOptions.TypeInfoResolverChain.Add(WebRendererSerializerContext.Default);
49+
jsRuntimeJsonSerializerOptions.TypeInfoResolverChain.Add(JsonConverterFactoryTypeInfoResolver<DotNetObjectReference<WebRendererInteropMethods>>.Instance);
4950

5051
jsRuntime.InvokeVoidAsync(
5152
"Blazor._internal.attachWebRendererInterop",

src/Components/WebAssembly/WebAssembly.Authentication/test/WebAssemblyAuthenticationServiceCollectionExtensionsTests.cs

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,10 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Authentication;
1212

1313
public class WebAssemblyAuthenticationServiceCollectionExtensionsTests
1414
{
15-
private static readonly JsonSerializerOptions JsonOptions = new();
16-
1715
[Fact]
1816
public void CanResolve_AccessTokenProvider()
1917
{
20-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
18+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
2119
builder.Services.AddApiAuthorization();
2220
var host = builder.Build();
2321

@@ -27,7 +25,7 @@ public void CanResolve_AccessTokenProvider()
2725
[Fact]
2826
public void CanResolve_IRemoteAuthenticationService()
2927
{
30-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
28+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
3129
builder.Services.AddApiAuthorization();
3230
var host = builder.Build();
3331

@@ -37,7 +35,7 @@ public void CanResolve_IRemoteAuthenticationService()
3735
[Fact]
3836
public void ApiAuthorizationOptions_ConfigurationDefaultsGetApplied()
3937
{
40-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
38+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
4139
builder.Services.AddApiAuthorization();
4240
var host = builder.Build();
4341

@@ -71,7 +69,7 @@ public void ApiAuthorizationOptions_ConfigurationDefaultsGetApplied()
7169
[Fact]
7270
public void ApiAuthorizationOptionsConfigurationCallback_GetsCalledOnce()
7371
{
74-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
72+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
7573
var calls = 0;
7674
builder.Services.AddApiAuthorization(options =>
7775
{
@@ -98,7 +96,7 @@ public void ApiAuthorizationOptionsConfigurationCallback_GetsCalledOnce()
9896
[Fact]
9997
public void ApiAuthorizationTestAuthenticationState_SetsUpConfiguration()
10098
{
101-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
99+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
102100
var calls = 0;
103101
builder.Services.AddApiAuthorization<TestAuthenticationState>(options => calls++);
104102

@@ -124,7 +122,7 @@ public void ApiAuthorizationTestAuthenticationState_SetsUpConfiguration()
124122
[Fact]
125123
public void ApiAuthorizationTestAuthenticationState_NoCallback_SetsUpConfiguration()
126124
{
127-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
125+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
128126
builder.Services.AddApiAuthorization<TestAuthenticationState>();
129127

130128
var host = builder.Build();
@@ -147,7 +145,7 @@ public void ApiAuthorizationTestAuthenticationState_NoCallback_SetsUpConfigurati
147145
[Fact]
148146
public void ApiAuthorizationCustomAuthenticationStateAndAccount_SetsUpConfiguration()
149147
{
150-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
148+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
151149
var calls = 0;
152150
builder.Services.AddApiAuthorization<TestAuthenticationState, TestAccount>(options => calls++);
153151

@@ -173,7 +171,7 @@ public void ApiAuthorizationCustomAuthenticationStateAndAccount_SetsUpConfigurat
173171
[Fact]
174172
public void ApiAuthorizationTestAuthenticationStateAndAccount_NoCallback_SetsUpConfiguration()
175173
{
176-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
174+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
177175
builder.Services.AddApiAuthorization<TestAuthenticationState, TestAccount>();
178176

179177
var host = builder.Build();
@@ -196,7 +194,7 @@ public void ApiAuthorizationTestAuthenticationStateAndAccount_NoCallback_SetsUpC
196194
[Fact]
197195
public void ApiAuthorizationOptions_DefaultsCanBeOverriden()
198196
{
199-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
197+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
200198
builder.Services.AddApiAuthorization(options =>
201199
{
202200
options.AuthenticationPaths.LogInPath = "a";
@@ -247,7 +245,7 @@ public void ApiAuthorizationOptions_DefaultsCanBeOverriden()
247245
[Fact]
248246
public void OidcOptions_ConfigurationDefaultsGetApplied()
249247
{
250-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
248+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
251249
builder.Services.Replace(ServiceDescriptor.Singleton<NavigationManager, TestNavigationManager>());
252250
builder.Services.AddOidcAuthentication(options => { });
253251
var host = builder.Build();
@@ -286,7 +284,7 @@ public void OidcOptions_ConfigurationDefaultsGetApplied()
286284
[Fact]
287285
public void OidcOptions_DefaultsCanBeOverriden()
288286
{
289-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
287+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
290288
builder.Services.AddOidcAuthentication(options =>
291289
{
292290
options.AuthenticationPaths.LogInPath = "a";
@@ -348,7 +346,7 @@ public void OidcOptions_DefaultsCanBeOverriden()
348346
[Fact]
349347
public void AddOidc_ConfigurationGetsCalledOnce()
350348
{
351-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
349+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
352350
var calls = 0;
353351

354352
builder.Services.AddOidcAuthentication(options => calls++);
@@ -365,7 +363,7 @@ public void AddOidc_ConfigurationGetsCalledOnce()
365363
[Fact]
366364
public void AddOidc_CustomState_SetsUpConfiguration()
367365
{
368-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
366+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
369367
var calls = 0;
370368

371369
builder.Services.AddOidcAuthentication<TestAuthenticationState>(options => options.ProviderOptions.Authority = (++calls).ToString(CultureInfo.InvariantCulture));
@@ -387,7 +385,7 @@ public void AddOidc_CustomState_SetsUpConfiguration()
387385
[Fact]
388386
public void AddOidc_CustomStateAndAccount_SetsUpConfiguration()
389387
{
390-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
388+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
391389
var calls = 0;
392390

393391
builder.Services.AddOidcAuthentication<TestAuthenticationState, TestAccount>(options => options.ProviderOptions.Authority = (++calls).ToString(CultureInfo.InvariantCulture));
@@ -409,7 +407,7 @@ public void AddOidc_CustomStateAndAccount_SetsUpConfiguration()
409407
[Fact]
410408
public void OidcProviderOptionsAndDependencies_NotResolvedFromRootScope()
411409
{
412-
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods(), JsonOptions);
410+
var builder = new WebAssemblyHostBuilder(new TestInternalJSImportMethods());
413411

414412
var calls = 0;
415413

src/Components/WebAssembly/WebAssembly/src/Hosting/WebAssemblyHostBuilder.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Diagnostics.CodeAnalysis;
55
using System.Globalization;
66
using System.Reflection;
7-
using System.Text.Json;
87
using Microsoft.AspNetCore.Components.Forms;
98
using Microsoft.AspNetCore.Components.Infrastructure;
109
using Microsoft.AspNetCore.Components.RenderTree;
@@ -27,7 +26,6 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Hosting;
2726
/// </summary>
2827
public sealed class WebAssemblyHostBuilder
2928
{
30-
private readonly JsonSerializerOptions _jsonOptions;
3129
private readonly IInternalJSImportMethods _jsMethods;
3230
private Func<IServiceProvider> _createServiceProvider;
3331
private RootComponentTypeCache? _rootComponentCache;
@@ -49,9 +47,7 @@ public static WebAssemblyHostBuilder CreateDefault(string[]? args = default)
4947
{
5048
// We don't use the args for anything right now, but we want to accept them
5149
// here so that it shows up this way in the project templates.
52-
var builder = new WebAssemblyHostBuilder(
53-
InternalJSImportMethods.Instance,
54-
DefaultWebAssemblyJSRuntime.Instance.ReadJsonSerializerOptions());
50+
var builder = new WebAssemblyHostBuilder(InternalJSImportMethods.Instance);
5551

5652
WebAssemblyCultureProvider.Initialize();
5753

@@ -65,14 +61,11 @@ public static WebAssemblyHostBuilder CreateDefault(string[]? args = default)
6561
/// <summary>
6662
/// Creates an instance of <see cref="WebAssemblyHostBuilder"/> with the minimal configuration.
6763
/// </summary>
68-
internal WebAssemblyHostBuilder(
69-
IInternalJSImportMethods jsMethods,
70-
JsonSerializerOptions jsonOptions)
64+
internal WebAssemblyHostBuilder(IInternalJSImportMethods jsMethods)
7165
{
7266
// Private right now because we don't have much reason to expose it. This can be exposed
7367
// in the future if we want to give people a choice between CreateDefault and something
7468
// less opinionated.
75-
_jsonOptions = jsonOptions;
7669
_jsMethods = jsMethods;
7770
Configuration = new WebAssemblyHostConfiguration();
7871
RootComponents = new RootComponentMappingCollection();
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace Microsoft.AspNetCore.Components.WebAssembly.Infrastructure;
8+
9+
[JsonSourceGenerationOptions(
10+
PropertyNamingPolicy = JsonKnownNamingPolicy.CamelCase,
11+
PropertyNameCaseInsensitive = true,
12+
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
13+
14+
// Required for WebAssemblyComponentParameterDeserializer
15+
[JsonSerializable(typeof(ComponentParameter[]))]
16+
[JsonSerializable(typeof(JsonElement))]
17+
[JsonSerializable(typeof(IList<object>))]
18+
19+
// Required for DefaultWebAssemblyJSRuntime
20+
[JsonSerializable(typeof(RootComponentOperationBatch))]
21+
internal sealed partial class WebAssemblyJsonSerializerContext : JsonSerializerContext;

src/Components/WebAssembly/WebAssembly/src/Prerendering/WebAssemblyComponentParameterDeserializer.cs

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,13 @@
33

44
using System.Diagnostics.CodeAnalysis;
55
using System.Text.Json;
6-
using System.Text.Json.Serialization;
6+
using Microsoft.AspNetCore.Components.WebAssembly.Infrastructure;
77
using static Microsoft.AspNetCore.Internal.LinkerFlags;
88

99
namespace Microsoft.AspNetCore.Components;
1010

1111
internal sealed class WebAssemblyComponentParameterDeserializer
1212
{
13-
private static readonly JsonSerializerOptions _jsonSerializerOptions = new(WebAssemblyComponentSerializationSettings.JsonSerializationOptions)
14-
{
15-
TypeInfoResolver = WebAssemblyComponentParameterDeserializerSerializerContext.Default,
16-
};
17-
1813
private readonly ComponentParametersTypeCache _parametersCache;
1914

2015
public WebAssemblyComponentParameterDeserializer(
@@ -65,7 +60,7 @@ public ParameterView DeserializeParameters(IList<ComponentParameter> parametersD
6560
var parameterValue = JsonSerializer.Deserialize(
6661
value.GetRawText(),
6762
parameterType,
68-
WebAssemblyComponentSerializationSettings.JsonSerializationOptions);
63+
WebAssemblyJsonSerializerContext.Default);
6964

7065
parametersDictionary[definition.Name] = parameterValue;
7166
}
@@ -83,17 +78,12 @@ public ParameterView DeserializeParameters(IList<ComponentParameter> parametersD
8378
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode", Justification = "The correct members will be preserved by the above DynamicDependency.")]
8479
public static ComponentParameter[] GetParameterDefinitions(string parametersDefinitions)
8580
{
86-
return JsonSerializer.Deserialize<ComponentParameter[]>(parametersDefinitions, _jsonSerializerOptions)!;
81+
return JsonSerializer.Deserialize(parametersDefinitions, WebAssemblyJsonSerializerContext.Default.ComponentParameterArray)!;
8782
}
8883

8984
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "We expect application code is configured to preserve component parameter types.")]
9085
public static IList<object> GetParameterValues(string parameterValues)
9186
{
92-
return JsonSerializer.Deserialize<IList<object>>(parameterValues, _jsonSerializerOptions)!;
87+
return JsonSerializer.Deserialize(parameterValues, WebAssemblyJsonSerializerContext.Default.IListObject)!;
9388
}
9489
}
95-
96-
[JsonSerializable(typeof(ComponentParameter[]))]
97-
[JsonSerializable(typeof(JsonElement))]
98-
[JsonSerializable(typeof(IList<object>))]
99-
internal sealed partial class WebAssemblyComponentParameterDeserializerSerializerContext : JsonSerializerContext;

src/Components/WebAssembly/WebAssembly/src/Services/DefaultWebAssemblyJSRuntime.cs

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
using System.Runtime.InteropServices.JavaScript;
77
using System.Runtime.Versioning;
88
using System.Text.Json;
9-
using System.Text.Json.Serialization;
109
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
10+
using Microsoft.AspNetCore.Components.WebAssembly.Infrastructure;
1111
using Microsoft.JSInterop;
1212
using Microsoft.JSInterop.Infrastructure;
1313
using Microsoft.JSInterop.WebAssembly;
@@ -17,11 +17,6 @@ namespace Microsoft.AspNetCore.Components.WebAssembly.Services;
1717

1818
internal sealed partial class DefaultWebAssemblyJSRuntime : WebAssemblyJSRuntime
1919
{
20-
private static readonly JsonSerializerOptions _rootComponentSerializerOptions = new(WebAssemblyComponentSerializationSettings.JsonSerializationOptions)
21-
{
22-
TypeInfoResolver = DefaultWebAssemblyJSRuntimeJsonSerializerContext.Default,
23-
};
24-
2520
public static readonly DefaultWebAssemblyJSRuntime Instance = new();
2621

2722
private readonly RootComponentTypeCache _rootComponentCache = new();
@@ -35,11 +30,12 @@ internal sealed partial class DefaultWebAssemblyJSRuntime : WebAssemblyJSRuntime
3530
[DynamicDependency(nameof(BeginInvokeDotNet))]
3631
[DynamicDependency(nameof(ReceiveByteArrayFromJS))]
3732
[DynamicDependency(nameof(UpdateRootComponentsCore))]
33+
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = $"The call to {nameof(JsonSerializerOptions.MakeReadOnly)} only populates the missing resolver when reflection is enabled")]
3834
private DefaultWebAssemblyJSRuntime()
3935
{
4036
ElementReferenceContext = new WebElementReferenceContext(this);
4137
JsonSerializerOptions.Converters.Add(new ElementReferenceJsonConverter(ElementReferenceContext));
42-
JsonSerializerOptions.MakeReadOnly();
38+
JsonSerializerOptions.MakeReadOnly(populateMissingResolver: JsonSerializer.IsReflectionEnabledByDefault);
4339
}
4440

4541
[JSExport]
@@ -116,9 +112,9 @@ public static void UpdateRootComponentsCore(string operationsJson)
116112
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "The correct members will be preserved by the above DynamicDependency")]
117113
internal static RootComponentOperationBatch DeserializeOperations(string operationsJson)
118114
{
119-
var deserialized = JsonSerializer.Deserialize<RootComponentOperationBatch>(
115+
var deserialized = JsonSerializer.Deserialize(
120116
operationsJson,
121-
_rootComponentSerializerOptions)!;
117+
WebAssemblyJsonSerializerContext.Default.RootComponentOperationBatch)!;
122118

123119
for (var i = 0; i < deserialized.Operations.Length; i++)
124120
{
@@ -169,6 +165,3 @@ protected override Task TransmitStreamAsync(long streamId, DotNetStreamReference
169165
return TransmitDataStreamToJS.TransmitStreamAsync(this, "Blazor._internal.receiveWebAssemblyDotNetDataStream", streamId, dotNetStreamReference);
170166
}
171167
}
172-
173-
[JsonSerializable(typeof(RootComponentOperationBatch))]
174-
internal sealed partial class DefaultWebAssemblyJSRuntimeJsonSerializerContext : JsonSerializerContext;

0 commit comments

Comments
 (0)