-
Notifications
You must be signed in to change notification settings - Fork 25.1k
Add BL0008 diagnostic article #36361
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| --- | ||
| title: "BL0008: Component parameters should be auto properties" | ||
| description: "Learn about analysis rule BL0008: Component parameters should be auto properties" | ||
| author: guardrex | ||
| monikerRange: '>= aspnetcore-8.0' | ||
| ms.author: wpickett | ||
| ms.date: 11/17/2025 | ||
| uid: diagnostics/bl0008 | ||
| --- | ||
guardrex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| # BL0008: Component parameters should be auto properties | ||
|
|
||
| | | Value | | ||
| | - | - | | ||
| | **Rule ID** | BL0008 | | ||
| | **Category** | Usage | | ||
| | **Fix is breaking or non-breaking** | Non-breaking | | ||
|
|
||
| ## Cause | ||
|
|
||
| A property has the [`[SupplyParameterFromForm]` attribute](xref:Microsoft.AspNetCore.Components.SupplyParameterFromFormAttribute) and is initialized with a non-default property initializer, where the initializer can be overwritten with `null` during form submission, causing the `EditForm` to fail with the following exception: | ||
|
|
||
| > :::no-loc text="InvalidOperationException: EditForm requires either a Model parameter, or an EditContext parameter, please provide one of these."::: | ||
| ## Rule description | ||
|
|
||
| The analyzer warns developers when this pattern is detected, while safely ignoring default value initializers (`null`, `null!`, `default`, `default!`) that don't cause the same issue. | ||
|
|
||
| Consider the following `Input` form model with a property initializer: | ||
|
|
||
| ```csharp | ||
| [SupplyParameterFromForm] | ||
| public InputModel Input { get; set; } = new(); | ||
| ``` | ||
|
|
||
| The analyzer reports the following warning: | ||
|
|
||
| > :::no-loc text="Property 'MyComponent.Input' has [SupplyParameterFromForm] and a property initializer. This can be overwritten with null during form posts."::: | ||
| <span aria-hidden="true">✔️</span> Safe patterns that are ignored by analyzer: | ||
|
|
||
| ```csharp | ||
| [SupplyParameterFromForm] | ||
| public InputModel Input { get; set; } = default!; | ||
|
|
||
| [SupplyParameterFromForm] | ||
| public InputModel Input { get; set; } = null!; | ||
| ``` | ||
|
|
||
| ## How to fix violations | ||
|
|
||
| To ensure the initialized value isn't overwritten, move the initialization to one of the [`OnInitialized{Async}` lifecycle methods](xref:blazor/components/lifecycle#component-initialization-oninitializedasync). | ||
|
|
||
| Consider the following Razor component that generates a BL0008 code analysis warning because the `Input` model for the form is decorated with `[SupplyParameterFromForm]` and has a property initializer: | ||
|
|
||
| ```razor | ||
| <EditForm Model="Input" OnSubmit="Submit" FormName="Starship1"> | ||
| ... | ||
| </EditForm> | ||
| @code { | ||
| [SupplyParameterFromForm] | ||
| private InputModel Input { get; set; } = new(); | ||
| private void Submit() | ||
| { | ||
| ... | ||
| } | ||
| public class InputModel | ||
| { | ||
| public string? Id { get; set; } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| To fix the violation, the initialization code for the `Input` property is moved to the [`OnInitialized` lifecycle method](xref:blazor/components/lifecycle#component-initialization-oninitializedasync), leaving the property as an [automatically implemented property (*auto property*)](/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties). The following change also makes `Input` a [nullable reference types (NRT)](xref:migration/50-to-60#nullable-reference-types-nrts-and-net-compiler-null-state-static-analysis): | ||
guardrex marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```csharp | ||
| [SupplyParameterFromForm] | ||
| private InputModel? Input { get; set; } | ||
|
|
||
| protected override void OnInitialized() => Input ??= new(); | ||
| ``` | ||
|
|
||
| ## When to suppress warnings | ||
|
|
||
| Do not suppress a warning from this rule. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.