Skip to content

Commit

Permalink
Address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastien-marichal committed Nov 9, 2023
1 parent 0fd9881 commit a3aee08
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 193 deletions.
Original file line number Diff line number Diff line change
@@ -1,90 +1,9 @@
<p>
Component parameters supplied from the query string support the following types:
<ul>
<li>bool, DateTime, decimal, double, float, Guid, int, long, string.</li>
<li>Nullable variants of the preceding types.</li>
<li>Arrays of the preceding types, whether they're nullable or not nullable.</li>
</ul>
</p>

@code {
[Parameter]
[SupplyParameterFromQuery]
public bool BoolParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public DateTime DateTimeParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public decimal DecimalParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public double DoubleParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public float FloatParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public Guid GuidParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public int IntParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public long LongParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public string StringParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public bool? NullableBoolParamShortForm { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public Nullable<bool> NullableBoolParamLongForm { get; set; } // Compliant
@code {
[Parameter]
[SupplyParameterFromQuery]
public bool?[] ArrayOfNullableBoolParamShortForm { get; set; } // Compliant
public bool BoolParam { get; set; }

[Parameter]
[SupplyParameterFromQuery]
public Nullable<bool>[] ArrayOfNullableBoolParamLongForm { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public TimeSpan TimeSpan { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public TimeSpan? NullableTimeSpan { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public Nullable<TimeSpan> NullableTimeSpanLongForm { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public TimeSpan[] TimeSpanArrayLongForm { get; set; } // Compliant
[Parameter]
public TimeSpan TimeSpanParam { get; set; } // Compliant
[Parameter]
public TimeSpan? NullableTimeSpanParam { get; set; } // Compliant
[Parameter]
public Nullable<TimeSpan> NullableTimeSpanLongFormParam { get; set; } // Compliant
[Parameter]
public TimeSpan[] TimeSpanArrayLongFormParam { get; set; } // Compliant
}
public DateTime DateTimeParam { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ public partial class S6797_Partial : ComponentBase
{
[Parameter]
[SupplyParameterFromQuery]
public TimeSpan TimeSpan { get; set; } // Noncompliant {{Query parameter type 'TimeSpan' is not supported.}}
// ^^^^^^^^
public TimeSpan TimeSpan { get; set; }

[Parameter]
public TimeSpan TimeSpanParam { get; set; } // Compliant
public TimeSpan TimeSpanParam { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ public class S6797 : ComponentBase
{
[Parameter]
[SupplyParameterFromQuery]
public TimeSpan TimeSpan { get; set; } // Noncompliant {{Query parameter type 'TimeSpan' is not supported.}}
// ^^^^^^^^
public TimeSpan TimeSpan { get; set; }

[Parameter]
public TimeSpan TimeSpanParam { get; set; } // Compliant
public TimeSpan TimeSpanParam { get; set; }
}
Original file line number Diff line number Diff line change
@@ -1,95 +1,11 @@
@page "/query-parameters"
<p>
Component parameters supplied from the query string support the following types:
<ul>
<li>bool, DateTime, decimal, double, float, Guid, int, long, string.</li>
<li>Nullable variants of the preceding types.</li>
<li>Arrays of the preceding types, whether they're nullable or not nullable.</li>
</ul>
</p>

@code {
[Parameter]
[SupplyParameterFromQuery]
public bool BoolParam { get; set; } // Compliant
public bool BoolParam { get; set; }

[Parameter]
[SupplyParameterFromQuery]
public DateTime DateTimeParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public decimal DecimalParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public double DoubleParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public float FloatParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public Guid GuidParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public int IntParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public long LongParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public string StringParam { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public bool? NullableBoolParamShortForm { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public Nullable<bool> NullableBoolParamLongForm { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public bool?[] ArrayOfNullableBoolParamShortForm { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public Nullable<bool>[] ArrayOfNullableBoolParamLongForm { get; set; } // Compliant
[Parameter]
[SupplyParameterFromQuery]
public TimeSpan TimeSpan { get; set; } // Noncompliant {{Query parameter type 'TimeSpan' is not supported.}}
// ^^^^^^^^
[Parameter]
[SupplyParameterFromQuery]
public TimeSpan? NullableTimeSpan { get; set; } // Noncompliant {{Query parameter type 'TimeSpan' is not supported.}}
// ^^^^^^^^^
[Parameter]
[SupplyParameterFromQuery]
public Nullable<TimeSpan> NullableTimeSpanLongForm { get; set; } // Noncompliant {{Query parameter type 'TimeSpan' is not supported.}}
// ^^^^^^^^^^^^^^^^^^
[Parameter]
[SupplyParameterFromQuery]
public TimeSpan[] TimeSpanArrayLongForm { get; set; } // Noncompliant {{Query parameter type 'TimeSpan' is not supported.}}
// ^^^^^^^^^^
[Parameter]
public TimeSpan TimeSpanParam { get; set; } // Compliant
[Parameter]
public TimeSpan? NullableTimeSpanParam { get; set; } // Compliant
[Parameter]
public Nullable<TimeSpan> NullableTimeSpanLongFormParam { get; set; } // Compliant
[Parameter]
public TimeSpan[] TimeSpanArrayLongFormParam { get; set; } // Compliant
}
public DateTime DateTimeParam { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,19 @@ protected override void Initialize(SonarAnalysisContext context) =>
SymbolKind.NamedType);
});

private static string GetTypeName(TypeSyntax propertyType)
{
if (propertyType.NameIs(KnownType.System_Nullable_T.TypeName))
{
propertyType = ((GenericNameSyntax)propertyType).TypeArgumentList.Arguments[0];
}

return propertyType.GetName();
}
private static bool HasRouteAttribute(INamedTypeSymbol componentClass) =>
componentClass.GetAttributes().Any(x => KnownType.Microsoft_AspNetCore_Components_RouteAttribute.Matches(x.AttributeClass));

private static IEnumerable<TypeSyntax> GetPropertyTypeMismatches(INamedTypeSymbol componentClass) =>
componentClass
.GetMembers()
.Where(IsPropertyTypeMismatch)
.SelectMany(p => p.DeclaringSyntaxReferences.Select(d => ((PropertyDeclarationSyntax)d.GetSyntax()).Type));
.SelectMany(x => x.DeclaringSyntaxReferences.Select(r => ((PropertyDeclarationSyntax)r.GetSyntax()).Type));

private static bool IsPropertyTypeMismatch(ISymbol member) =>
member is IPropertySymbol property
&& property.HasAttribute(KnownType.Microsoft_AspNetCore_Components_SupplyParameterFromQueryAttribute)
&& !SupportedTypes.Any(t => IsSupportedType(property.Type, t));
&& !SupportedTypes.Any(x => IsSupportedType(property.Type, x));

private static bool IsSupportedType(ITypeSymbol type, KnownType supportType)
{
Expand All @@ -101,6 +94,8 @@ private static bool IsSupportedType(ITypeSymbol type, KnownType supportType)
return supportType.Matches(type);
}

private static bool HasRouteAttribute(INamedTypeSymbol componentClass) =>
componentClass.GetAttributes().Any(a => KnownType.Microsoft_AspNetCore_Components_RouteAttribute.Matches(a.AttributeClass));
private static string GetTypeName(TypeSyntax propertyType) =>
propertyType.NameIs(KnownType.System_Nullable_T.TypeName) && propertyType is GenericNameSyntax syntax
? syntax.TypeArgumentList.Arguments[0].GetName()
: propertyType.GetName();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

#if NET

using SonarAnalyzer.Rules.CSharp;

namespace SonarAnalyzer.UnitTest.Rules;
Expand Down Expand Up @@ -46,14 +47,17 @@ public void BlazorQueryParameterTypeShouldBeSupported_BlazorNoRoute() =>

[TestMethod]
public void BlazorQueryParameterTypeShouldBeSupported_Partial() =>
builder.AddPaths("BlazorQueryParameterTypeShouldBeSupported.Partial.razor", "BlazorQueryParameterTypeShouldBeSupported.Partial.razor.cs")
builder
.AddPaths("BlazorQueryParameterTypeShouldBeSupported.Partial.razor", "BlazorQueryParameterTypeShouldBeSupported.Partial.razor.cs")
.WithAdditionalFilePath(AnalysisScaffolding.CreateSonarProjectConfig(TestContext, ProjectType.Product))
.Verify();

[TestMethod]
public void BlazorQueryParameterTypeShouldBeSupported_CS() =>
builder.AddPaths("BlazorQueryParameterTypeShouldBeSupported.cs")
builder
.AddPaths("BlazorQueryParameterTypeShouldBeSupported.cs")
.AddReferences(NuGetMetadataReference.MicrosoftAspNetCoreComponents("7.0.13"))
.Verify();
}

#endif
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
using System;
using Microsoft.AspNetCore.Components;

public class Nullable
{

}

[Route("/query-parameters")]
public class BlazorQueryParameterTypeShouldBeSupported : ComponentBase
{
Expand All @@ -11,4 +16,9 @@ public class BlazorQueryParameterTypeShouldBeSupported : ComponentBase

[Parameter]
public TimeSpan TimeSpanParam { get; set; } // Compliant

[Parameter]
[SupplyParameterFromQuery]
public Nullable Nullable { get; set; } // Noncompliant {{Query parameter type 'Nullable' is not supported.}}

}

0 comments on commit a3aee08

Please sign in to comment.