SonarSource/sonar-dotnet

Fix S6964 FP: Do not raise in properties with required modifier

Closed this issue · 3 comments

Description

The rule S6964 should not be active for model properties that use the required keyword. Those properties can never be under-posted because the JSON deserialization for them fails.

Repro steps

  1. Create a new ASP.NET Core Web API project.
  2. Add a model with required properties (e.g. required int) and use it in a POST controller method.
  3. Call the POST method without providing the property. See that ASP.NET Core returns an error, even though you didn't apply the [Required] attribute 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@1 task)
  • Visual Studio version: not relevant
  • MSBuild / dotnet version: 8.0.300
  • SonarScanner for .NET version: SonarScanner for MSBuild 5.15 (used by the SonarCloudAnalyze@1 task)
  • Operating System: windows-latest build agent on Azure DevOps

Hello @cremor.

The rule does take into consideration the RequiredAttribute.
Could you please provide me with a reproducer so I can investigate further?

Thanks!

My issue is about the required C# keyword. Not about the RequiredAttribute class.

@cremor I misread - my bad. Thanks a lot!

I confirm this is a false positive. We'll tackle it soon, as we plan to harden the ASP.NET rules in the following weeks.