Skip to content

Fix ResourceAsset constructor to support RespectRequiredConstructorParametersDefault #62366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jun 17, 2025

Summary

This PR fixes a crash in Blazor Web WebAssembly Mode when the System.Text.Json.Serialization.RespectRequiredConstructorParametersDefault feature switch is enabled. The issue occurred during JSON deserialization of ResourceAsset objects where the properties parameter was missing from the JSON payload.

Problem

When RespectRequiredConstructorParametersDefault is set to true, the JSON serializer requires all constructor parameters to be present in the JSON during deserialization, even if they are nullable. The ResourceAsset constructor:

public sealed class ResourceAsset(string url, IReadOnlyList<ResourceAssetProperty>? properties)

Would fail to deserialize with the error:

JSON deserialization for type 'Microsoft.AspNetCore.Components.ResourceAsset' was missing required properties including: 'Properties'.

Solution

Modified the ResourceAsset constructor to provide a default value for the properties parameter:

public sealed class ResourceAsset(string url, IReadOnlyList<ResourceAssetProperty>? properties = null)

This minimal change ensures compatibility with the RespectRequiredConstructorParametersDefault feature switch while maintaining backward compatibility.

Changes Made

  1. Updated ResourceAsset constructor - Added default parameter value = null to the properties parameter
  2. Updated PublicAPI.Shipped.txt - Reflected the constructor signature change in the public API tracking
  3. Added comprehensive tests - Created tests to verify serialization/deserialization works with both default and strict RespectRequiredConstructorParameters settings
  4. Added feature switch to test project - Enabled RespectRequiredConstructorParametersDefault = true in BasicTestApp.csproj for ongoing compatibility testing

Testing

  • ✅ Reproduced the original issue and verified the fix resolves it
  • ✅ All ResourceAsset-related tests pass (8/8)
  • ✅ All Components.Tests pass (1107/1115, 8 skipped - no failures)
  • ✅ Backward compatibility maintained - existing code continues to work without changes

Fixes #62350.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

Copy link
Contributor

Greetings human! You've submitted a PR that modifies code that is shared with https://github.com/dotnet/runtime . Please make sure you synchronize this code with the changes in that repo!

@Copilot Copilot AI changed the title [WIP] [.NET 10 Preview 5] Blazor Web WebAssembly Mode crashes related with JsonSerialization Fix ResourceAsset constructor to support RespectRequiredConstructorParametersDefault Jun 17, 2025
@Copilot Copilot AI requested a review from javiercn June 17, 2025 09:41
Copilot finished work on behalf of javiercn June 17, 2025 09:41
Comment on lines +19 to +22
<ItemGroup>
<!-- Enable RespectRequiredConstructorParametersDefault to test compatibility -->
<RuntimeHostConfigurationOption Include="System.Text.Json.Serialization.RespectRequiredConstructorParametersDefault" Value="true" />
</ItemGroup>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs to be defined in all test projects that reference the Microsoft.NET.Sdk.Web and the Microsoft.NET.Sdk.WebAssembly the test projects live under src/Components/tests/testassets. It's not enough for the setting to be defined on a referenced project.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example, is not enough that it's defined on BasicTestApp. Components.TestServer also needs to define it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the RespectRequiredConstructorParametersDefault configuration to all test projects that reference Microsoft.NET.Sdk.Web or Microsoft.NET.Sdk.BlazorWebAssembly under src/Components/tests/testassets:

  • Components.TestServer (Microsoft.NET.Sdk.Web)
  • ComponentsApp.Server (Microsoft.NET.Sdk.Web)
  • Components.WasmMinimal (Microsoft.NET.Sdk.BlazorWebAssembly)
  • Components.WasmRemoteAuthentication (Microsoft.NET.Sdk.BlazorWebAssembly)
  • GlobalizationWasmApp (Microsoft.NET.Sdk.BlazorWebAssembly)

Commit: 23e3909

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the RespectRequiredConstructorParametersDefault configuration to all test projects that reference Microsoft.NET.Sdk.Web or Microsoft.NET.Sdk.BlazorWebAssembly under src/Components/tests/testassets:

  • Components.TestServer (Microsoft.NET.Sdk.Web)
  • ComponentsApp.Server (Microsoft.NET.Sdk.Web)
  • Components.WasmMinimal (Microsoft.NET.Sdk.BlazorWebAssembly)
  • Components.WasmRemoteAuthentication (Microsoft.NET.Sdk.BlazorWebAssembly)
  • GlobalizationWasmApp (Microsoft.NET.Sdk.BlazorWebAssembly)

Commit: 23e3909

… Web and WebAssembly test projects

Co-authored-by: javiercn <[email protected]>
Copilot finished work on behalf of javiercn June 17, 2025 09:57
Copilot finished work on behalf of javiercn June 17, 2025 10:05
@@ -417,7 +417,7 @@ Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrameType.Region = 5 -> Mic
Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrameType.Text = 2 -> Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrameType
Microsoft.AspNetCore.Components.ResourceAsset
Microsoft.AspNetCore.Components.ResourceAsset.Properties.get -> System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Components.ResourceAssetProperty!>?
Microsoft.AspNetCore.Components.ResourceAsset.ResourceAsset(string! url, System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Components.ResourceAssetProperty!>? properties) -> void
Microsoft.AspNetCore.Components.ResourceAsset.ResourceAsset(string! url, System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Components.ResourceAssetProperty!>? properties = null) -> void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not modify this API file. A new entry on the associated PublicAPI.Unshipped.txt file should be added instead

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed the PublicAPI tracking by removing the old constructor signature from PublicAPI.Shipped.txt and adding the new signature with default parameter to PublicAPI.Unshipped.txt instead. Commit: be6e0bb

Copilot finished work on behalf of javiercn June 17, 2025 10:35
@@ -417,7 +417,6 @@ Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrameType.Region = 5 -> Mic
Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrameType.Text = 2 -> Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrameType
Microsoft.AspNetCore.Components.ResourceAsset
Microsoft.AspNetCore.Components.ResourceAsset.Properties.get -> System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Components.ResourceAssetProperty!>?
Microsoft.AspNetCore.Components.ResourceAsset.ResourceAsset(string! url, System.Collections.Generic.IReadOnlyList<Microsoft.AspNetCore.Components.ResourceAssetProperty!>? properties) -> void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not modify this file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've restored the PublicAPI.Shipped.txt file to its original state to avoid modifying it. However, the build is now failing because the actual constructor signature in the code (with default parameter) doesn't match what's declared in the shipped file (without default parameter).

Should I create a new constructor overload instead of modifying the existing one, or is there a different approach you'd recommend for handling this API change while keeping the shipped file unchanged?

Copilot finished work on behalf of javiercn June 17, 2025 11:01
@javiercn javiercn marked this pull request as ready for review June 17, 2025 18:19
@javiercn javiercn requested review from tdykstra and a team as code owners June 17, 2025 18:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[.NET 10 Preview 5] Blazor Web WebAssembly Mode crashes related with JsonSerialization
2 participants