Skip to content

Commit d5fb626

Browse files
authored
Add coverage for analyzer violation BL0007 (#35161)
1 parent 99fbda6 commit d5fb626

File tree

10 files changed

+45
-8
lines changed

10 files changed

+45
-8
lines changed

aspnetcore/blazor/components/index.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,14 +1071,14 @@ For more information, see <xref:mvc/views/razor>.
10711071
> [!WARNING]
10721072
> Providing initial values for component parameters is supported, but don't create a component that writes to its own parameters after the component is rendered for the first time. For more information, see <xref:blazor/components/overwriting-parameters>.
10731073
1074-
Component parameters should be declared as *auto-properties*, meaning that they shouldn't contain custom logic in their `get` or `set` accessors. For example, the following `StartData` property is an auto-property:
1074+
Component parameters should be declared as [automatically-implemented properties (*auto properties*)](/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties), meaning that they shouldn't contain custom logic in their `get` or `set` accessors. For example, the following `StartData` property is an auto property:
10751075

10761076
```csharp
10771077
[Parameter]
10781078
public DateTime StartData { get; set; }
10791079
```
10801080

1081-
Don't place custom logic in the `get` or `set` accessor because component parameters are purely intended for use as a channel for a parent component to flow information to a child component. If a `set` accessor of a child component property contains logic that causes rerendering of the parent component, an infinite rendering loop results.
1081+
Don't place custom logic in the `get` or `set` accessor because component parameters are purely intended for use as a channel for a parent component to flow information to a child component. If a `set` accessor of a child component property contains logic that causes rerendering of the parent component, an infinite rendering loop results. Other side effects include unexpected extra renderings and parameter value overwrites.
10821082

10831083
To transform a received parameter value:
10841084

aspnetcore/diagnostics/bl0001.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "BL0001: Component parameter should have public setters"
33
description: "Learn about analysis rule BL0001: Component parameter should have public setters"
4-
author: pranavkm
4+
author: guardrex
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.date: 10/21/2021

aspnetcore/diagnostics/bl0002.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "BL0002: Component has multiple CaptureUnmatchedValues parameters"
33
description: "Learn about analysis rule BL0002: Component has multiple CaptureUnmatchedValues parameters"
4-
author: pranavkm
4+
author: guardrex
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.date: 10/21/2021

aspnetcore/diagnostics/bl0003.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "BL0003: Component parameter with CaptureUnmatchedValues has the wrong type"
33
description: "Learn about analysis rule BL0003: Component parameter with CaptureUnmatchedValues has the wrong type"
4-
author: pranavkm
4+
author: guardrex
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.date: 10/21/2021

aspnetcore/diagnostics/bl0004.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "BL0004: Component parameter should be public"
33
description: "Learn about analysis rule BL0004: Component parameter should be public"
4-
author: pranavkm
4+
author: guardrex
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.date: 10/21/2021

aspnetcore/diagnostics/bl0005.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "BL0005: Component parameter should not be set outside of its component"
33
description: "Learn about analysis rule BL0005: Component parameter should not be set outside of its component"
4-
author: pranavkm
4+
author: guardrex
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.date: 10/21/2021

aspnetcore/diagnostics/bl0006.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: "BL0006: Do not use RenderTree types"
33
description: "Learn about analysis rule BL0006: Do not use RenderTree types"
4-
author: pranavkm
4+
author: guardrex
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: riande
77
ms.date: 10/21/2021

aspnetcore/diagnostics/bl0007.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
title: "BL0007: Component parameter '{0}' should be auto property"
3+
description: "Learn about analysis rule BL0007: Component parameter '{0}' should be auto property"
4+
author: guardrex
5+
monikerRange: '>= aspnetcore-3.1'
6+
ms.author: riande
7+
ms.date: 04/07/2025
8+
uid: diagnostics/bl0007
9+
---
10+
# BL0007: Component parameter '{0}' should be auto property
11+
12+
| | Value |
13+
| - | - |
14+
| **Rule ID** | BL0007 |
15+
| **Category** | Usage |
16+
| **Fix is breaking or non-breaking** | Non-breaking |
17+
18+
## Cause
19+
20+
A [component parameter](xref:blazor/components/index#component-parameters), which is a public [C# property](/dotnet/csharp/programming-guide/classes-and-structs/properties) of a component class with the [`[Parameter]` attribute](xref:Microsoft.AspNetCore.Components.ParameterAttribute), isn't an [automatically-implemented property (*auto property*)](/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties).
21+
22+
## Rule description
23+
24+
A component parameter is a framework-managed communication channel between a parent and a child component. Developers shouldn't read or write to the parameter in a [`get` (getter) or `set` (setter) accessor](/dotnet/csharp/programming-guide/classes-and-structs/using-properties), either from inside or outside the component.
25+
26+
Possible side effects of interacting directly with a component parameter include infinite rendering loops, unexpected extra renderings, and parameter value overwrites.
27+
28+
## How to fix violations
29+
30+
Implement the component parameter as an auto property and override <xref:Microsoft.AspNetCore.Components.ComponentBase.OnParametersSet%2A> or <xref:Microsoft.AspNetCore.Components.ComponentBase.OnParametersSetAsync%2A> in the component class to read or transform the parameter's value. For more information, see the [`OnParametersSet{Async}` lifecycle method](xref:blazor/components/lifecycle#after-parameters-are-set-onparameterssetasync).
31+
32+
## When to suppress warnings
33+
34+
Do not suppress a warning from this rule.

aspnetcore/diagnostics/code-analysis.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ Diagnostic ID:
4949
* [BL0004](xref:diagnostics/bl0004)
5050
* [BL0005](xref:diagnostics/bl0005)
5151
* [BL0006](xref:diagnostics/bl0006)
52+
* [BL0007](xref:diagnostics/bl0007)
5253
* [MVC1000](xref:diagnostics/mvc1000)
5354
* [MVC1001](xref:diagnostics/mvc1001)
5455
* [MVC1002](xref:diagnostics/mvc1002)

aspnetcore/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,8 @@ items:
14611461
uid: diagnostics/bl0005
14621462
- name: BL0006
14631463
uid: diagnostics/bl0006
1464+
- name: BL0007
1465+
uid: diagnostics/bl0007
14641466
- name: MVC1000
14651467
uid: diagnostics/mvc1000
14661468
- name: MVC1001

0 commit comments

Comments
 (0)