-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Description
Is there an existing issue for this?
- I have searched the existing issuesTo pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
Is your feature request related to a problem? Please describe the problem.
I enable the System.Text.Json.Serialization.RespectRequiredConstructorParametersDefault
as true
for my projects. See: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/required-properties#feature-switch
It works fine before, but after updating to Preview5, I got:
AggregateException_ctor_DefaultMessage (An exception occurred executing JS interop: JsonRequiredPropertiesMissing, Microsoft.AspNetCore.Components.ResourceAsset, 'properties'. See InnerException for more details.)
at un (marshal-to-js.ts:421:18)
at rn.resolve_or_reject (marshal-to-js.ts:315:28)
at marshal-to-js.ts:364:16
at marshal-to-js.ts:341:48
at gr (invoke-js.ts:528:9)
at Mc (marshal-to-js.ts:341:5)
at dotnet.native.ulywfn9r8g.wasm:0x1f60f
at dotnet.native.ulywfn9r8g.wasm:0x1ca55
at dotnet.native.ulywfn9r8g.wasm:0xeb73
at dotnet.native.ulywfn9r8g.wasm:0x1f0f3
To disable RespectRequiredConstructorParametersDefault
will fix, this seems a break change, and I want to keep my RespectRequiredConstructorParametersDefault
as true.
My guess is Preview5 deserializes Microsoft.AspNetCore.Components.ResourceAsset
without all required properties.
Describe the solution you'd like
With System.Text.Json.Serialization.RespectRequiredConstructorParametersDefault
as true
won't break my Blazor Web application. An approach is to make sure all required properties presented in the json string. Or:
public sealed class ResourceAsset(string url, IReadOnlyList<ResourceAssetProperty>? properties);
// Make the second param optional
public sealed class ResourceAsset(string url, IReadOnlyList<ResourceAssetProperty>? properties = null);
Additional context
My .NET SDK Version: 10.0.100-preview.5.25277.114
I'm using preview5 nuget packages for all my Blazor related packages.
Activity
javiercn commentedon Jun 16, 2025
@Kumima thanks for contacting us.
This is not a breaking change on Blazor. I imagine we are not compatible with https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json/required-properties because of the reason that you stated but might as well be that other data structures in addition to
ResourceAsset
are impacted by it.If this behavior changed for you between preview4 and preview5, we would suggest you raise the issue on the runtime repo, since that might point to a false positive or it might be the case that a bug in the detection logic got fixed and is surfacing the error now.
Kumima commentedon Jun 16, 2025
@javiercn Thanks for the reply.
I don't think a runtime repo issue will help. Since I still believe
My guess is Preview5 adds some serialization of Microsoft.AspNetCore.Components.ResourceAsset without all required properties.
It is not a break change since by default
RespectRequiredConstructorParametersDefault
flag isfalse
. The thing I was hoping is Blazor can improve the serialization stuffs to fit for thetrue
value ofRespectRequiredConstructorParametersDefault
. Because this setting is more intuitive. Iftrue
value works,false
value won't be affected.This may not be a high priority task for you, but I still think this can be an improvement.
javiercn commentedon Jun 17, 2025
Feature switch
You can turn on the RespectRequiredConstructorParameters setting globally using the System.Text.Json.Serialization.RespectRequiredConstructorParametersDefault feature switch. Add the following MSBuild item to your project file (for example, .csproj file):
The RespectRequiredConstructorParametersDefault API was implemented as an opt-in flag in .NET 9 to avoid breaking existing applications. If you're writing a new application, it's highly recommended that you enable this flag in your code.
I think we want to turn this on in our E2E test projects as a first step.
Microsoft.NET.Sdk.BlazorWebassembly
Then we want to see what errors appear and then fix the issues.
There are two possible ways to three possible fixes
ResourceAsset(string url) : this(url, null)
ResourceAsset(string url, IReadOnlyList<ResourceAssetProperty>? properties = null);
public class ResourceAsset { public required string Url { get; init; } public IReadOnlyList<ResourceAssetProperty> Properties { get; init; } }
We can't make breaking changes, so 3 is only possible for internal types.
Kumima commentedon Jun 17, 2025
There is another option which is
<RuntimeHostConfigurationOption Include="System.Text.Json.Serialization.RespectNullableAnnotationsDefault" Value="true" />
, can they be handled together? Currently with this option on, I did not meet any problem.javiercn commentedon Jun 19, 2025
@Kumima thanks for the additional details.
I think we would want this to work without having to enable additional JSON settings.
#62366 should cover the fix and updating our E2E tests to validate that things work with the more strict setting.