-
Notifications
You must be signed in to change notification settings - Fork 237
Closed
Closed
Copy link
Labels
False PositiveRule IS triggered when it shouldn't be.Rule IS triggered when it shouldn't be.
Milestone
Description
Description
The rule S6964 should not be active for model properties in a nullable aware context (where nullable reference types are enabled). This is because of:
By default, MVC will treat a non-nullable reference type parameters and properties as-if
[Required]has been applied, resulting in validation errors when no value was bound.
Even if a project sets this property to true, the rule should not be reported in my opinion. Because then it was an explicit decision made by the developer (e.g. because the project uses a different validation framework like FluentValidation).
Repro steps
- Create a new ASP.NET Core Web API project (nullable reference types are enabled by default in current .NET versions)
- Add a model with not-nullable reference type properties (e.g.
string) and use it in a POST controller method. - Call the POST method without providing the property. See that ASP.NET Core returns an validation error, even though you didn't apply the
Requiredattribute manually.
Expected behavior
Rule should not be reported.
Actual behavior
Rule is reported.
Known workarounds
I had to create a new quality profile in SonarCloud to disable the rule completely.
Related information
- C#/VB.NET Plugins version: 9.25.0.90414 (used by the
SonarCloudPrepare@1task) - Visual Studio version: not relevant
- MSBuild / dotnet version: 8.0.300
- SonarScanner for .NET version: SonarScanner for MSBuild 5.15 (used by the
SonarCloudAnalyze@1task) - Operating System:
windows-latestbuild agent on Azure DevOps
Metadata
Metadata
Assignees
Labels
False PositiveRule IS triggered when it shouldn't be.Rule IS triggered when it shouldn't be.