Skip to content

Commit

Permalink
Fix FP: property marked with JsonRequired attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-kolbay-sonarsource committed May 21, 2024
1 parent 0371dfa commit 16c07cd
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ private static void CheckInvalidProperties(INamedTypeSymbol parameterType, Sonar
var declaredProperties = new List<IPropertySymbol>();
GetAllDeclaredProperties(parameterType, examinedTypes, declaredProperties);
var invalidProperties = declaredProperties.Where(x => !CanBeNull(x.Type)
&& !x.HasAttribute(KnownType.System_ComponentModel_DataAnnotations_RequiredAttribute));
&& !x.HasAttribute(KnownType.System_ComponentModel_DataAnnotations_RequiredAttribute)
&& !x.HasAttribute(KnownType.System_Text_Json_Serialization_JsonRequiredAttribute));
foreach (var property in invalidProperties)
{
var propertySyntax = property.DeclaringSyntaxReferences[0].GetSyntax();
Expand Down
1 change: 1 addition & 0 deletions analyzers/src/SonarAnalyzer.Common/Helpers/KnownType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ public sealed partial class KnownType
public static readonly KnownType System_StringComparison = new("System.StringComparison");
public static readonly KnownType System_SystemException = new("System.SystemException");
public static readonly KnownType System_Text_Encoding = new("System.Text.Encoding");
public static readonly KnownType System_Text_Json_Serialization_JsonRequiredAttribute = new("System.Text.Json.Serialization.JsonRequiredAttribute");
public static readonly KnownType System_Text_RegularExpressions_Regex = new("System.Text.RegularExpressions.Regex");
public static readonly KnownType System_Text_RegularExpressions_RegexOptions = new("System.Text.RegularExpressions.RegexOptions");
public static readonly KnownType System_Text_StringBuilder = new("System.Text.StringBuilder");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ namespace Repro9275
// Repro https://github.com/SonarSource/sonar-dotnet/issues/9275
public class Model
{
public int ValueProperty { get; set; } // Noncompliant
public int ValueProperty { get; set; } // Noncompliant

[Custom] // Noncompliant repro for https://github.com/SonarSource/sonar-dotnet/issues/9282
[Custom] // Noncompliant repro for https://github.com/SonarSource/sonar-dotnet/issues/9282
public int ValuePropertyAnnotatedWithCustomAttribute { get; set; }

[JsonRequired] // Noncompliant - FP because the property is annotated with JsonRequiredAttribute
[JsonRequired] // Compliant - the property is annotated with JsonRequiredAttribute
public int AnotherValueProperty { get; set; }

public required int RequiredProperty { get; set; } // Noncompliant - FP because the property has the required modifier
public required int RequiredProperty { get; set; } // Noncompliant - FP because the property has the required modifier
}

public class DerivedFromController : Controller
Expand Down

0 comments on commit 16c07cd

Please sign in to comment.