Skip to content
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

[Blazor] Supplying a null parameter value to an interactive root component causes a NullReferenceException on enhanced navigation #52434

Closed
MackinnonBuck opened this issue Nov 28, 2023 · 3 comments · Fixed by #53317
Assignees
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug.
Milestone

Comments

@MackinnonBuck
Copy link
Member

Summary

If a null value is supplied as a parameter to a render mode boundary component during static SSR, and the SSR'd component is matched up with an existing interactive root component, a NullReferenceException gets thrown by the framework.

Steps to reproduce

  1. Create a Blazor Web App with Server interactivity
  2. Define a component (MyComponent.razor):
    @inject NavigationManager NavigationManager
    
    <button @onclick="@(() => NavigationManager.Refresh())">Refresh</button>
    
    @code {
        [Parameter]
        public string? Value { get; set; }
    }
  3. Render the component from Home.razor and supply the value null for the Value parameter:
    <MyComponent Value="@null" @rendermode="InteractiveServer" />
  4. Run the app
  5. Click the button labeled "Refresh"
  6. Observe that an exception gets thrown and the circuit gets terminated

Relevant output

Stack trace:

Microsoft.AspNetCore.Components.Server.dll!Microsoft.AspNetCore.Components.WebRootComponentParameters.DefinitelyEquals(Microsoft.AspNetCore.Components.WebRootComponentParameters other) Line 43
	at Microsoft.AspNetCore.Components\WebRootComponentParameters.cs(43)
Microsoft.AspNetCore.Components.Server.dll!Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer.WebRootComponentManager.WebRootComponent.UpdateAsync(Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer renderer, System.Type newComponentType, Microsoft.AspNetCore.Components.ComponentMarkerKey? newKey, Microsoft.AspNetCore.Components.WebRootComponentParameters newParameters) Line 245
	at Microsoft.AspNetCore.Components.Server.Circuits\RemoteRenderer.cs(245)
.
.
.

Proposed fix

Update WebRootComponentParameters to account for the possibility that the parameter values are null rather than being valid JsonElements.

@MackinnonBuck MackinnonBuck added bug This issue describes a behavior which is not expected - a bug. area-blazor Includes: Blazor, Razor Components labels Nov 28, 2023
@javiercn
Copy link
Member

@MackinnonBuck good catch.

Do you think this has been the case since the beginning or is this a new .NET 8.0 behavior.

@MackinnonBuck
Copy link
Member Author

@javiercn WebRootComponentParameters was a type we introduced in .NET 8, so it only affects .NET 8 (specifically Blazor Web apps; pure Server or WebAssembly apps shouldn't be affected)

@javiercn javiercn added this to the 8.0.x milestone Nov 30, 2023
@javiercn
Copy link
Member

@MackinnonBuck @mkArtakMSFT parking this in 8.0.x as I think it is patchable, feel free to move it otherwise

mkArtakMSFT pushed a commit that referenced this issue Jan 17, 2024
…components via enhanced page update (#53317) (#53342)

# Allow `null` parameter values to be supplied to interactive components via enhanced page update

Backport of #53317

Fixes an issue where a `NullRefrenceException` would be thrown if an enhanced page update supplied a `null` parameter to an interactive root component.

## Description

In .NET 8, SSR'd components can supply updated parameters to existing interactive root components. If one of those updated parameters is `null`, an exception currently gets thrown from within the framework. This causes the circuit to crash when using Server interactivity, and it would causes an error to be logged in the browser console when using WebAssembly interactivity.

This PR fixes the problem by treating `null` as a valid value for a serialized parameter that gets supplied to an interactive root component.

Fixes #52434

## Customer Impact

Without this fix, customers may encounter the unfriendly exception and have a hard time figuring out the underlying cause. We have not yet seen customer reports of the issue, but it's possible that customers have this bug in their apps without knowing it, especially since it only occurs when supplying updated parameters to existing components (not when supplying the initial set of parameters).

One workaround would be to use a different value than `null` to specify an empty parameter value, but this may not be possible in cases where the parameter gets supplied by the framework (e.g., via route value), or if the interactive root component's implementation is not under the developer's control.

## Regression?

- [ ] Yes
- [X] No

Only applicable to new scenarios in .NET 8.

## Risk

- [ ] High
- [ ] Medium
- [X] Low

The fix is straightforward and well-tested.

## Verification

- [x] Manual (required)
- [x] Automated

## Packaging changes reviewed?

- [ ] Yes
- [ ] No
- [X] N/A
@mkArtakMSFT mkArtakMSFT modified the milestones: 8.0.x, 8.0.2 Jan 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components bug This issue describes a behavior which is not expected - a bug.
Projects
Status: Done
4 participants
@javiercn @MackinnonBuck @mkArtakMSFT and others