Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve S6964: Dont try to raise issue when Model and Controller are in different projects #9240

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static void ProcessControllerMethods(SonarSyntaxNodeReportingContext con
{
var modelParameterTypes = method.Parameters
.Where(x => !HasValidateNeverAttribute(x))
.SelectMany(x => RelatedTypesToExamine(x.Type))
.SelectMany(x => RelatedTypesToExamine(x.Type, method.ContainingType))
.Distinct();
foreach (var modelParameterType in modelParameterTypes)
{
Expand Down Expand Up @@ -132,13 +132,14 @@ private static void GetAllDeclaredProperties(ITypeSymbol type, ConcurrentDiction
}
}

private static IEnumerable<INamedTypeSymbol> RelatedTypesToExamine(ITypeSymbol type) =>
// We only consider Model types that are in the same assembly as the Controller, because Roslyn can't raise an issue when the location is in a different assembly than the one being analyzed.
private static IEnumerable<INamedTypeSymbol> RelatedTypesToExamine(ITypeSymbol type, ITypeSymbol controllerType) =>
type switch
{
IArrayTypeSymbol array => RelatedTypesToExamine(array.ElementType),
IArrayTypeSymbol array => RelatedTypesToExamine(array.ElementType, controllerType),
INamedTypeSymbol collection when collection.DerivesOrImplements(KnownType.System_Collections_Generic_IEnumerable_T) =>
collection.TypeArguments.SelectMany(RelatedTypesToExamine),
INamedTypeSymbol namedType => [namedType],
collection.TypeArguments.SelectMany(x => RelatedTypesToExamine(x, controllerType)),
INamedTypeSymbol namedType when type.IsInSameAssembly(controllerType) => [namedType],
_ => []
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ namespace SonarAnalyzer.Test.Rules;
[TestClass]
public class AvoidUnderPostingTest
{
private readonly VerifierBuilder builder = new VerifierBuilder<AvoidUnderPosting>()
.WithBasePath("AspNet")
.AddReferences([
private static readonly IEnumerable<MetadataReference> AspNetReferences = [
AspNetCoreMetadataReference.MicrosoftAspNetCoreMvcAbstractions,
AspNetCoreMetadataReference.MicrosoftAspNetCoreMvcCore,
AspNetCoreMetadataReference.MicrosoftAspNetCoreMvcViewFeatures,
..NuGetMetadataReference.SystemComponentModelAnnotations(Constants.NuGetLatestVersion)
]);
..NuGetMetadataReference.SystemComponentModelAnnotations(Constants.NuGetLatestVersion)];

private readonly VerifierBuilder builder = new VerifierBuilder<AvoidUnderPosting>()
.WithBasePath("AspNet")
.AddReferences(AspNetReferences);

[TestMethod]
public void AvoidUnderPosting_CSharp() =>
Expand Down Expand Up @@ -103,6 +104,43 @@ public class ControllerClass : Controller
[{{attribute}}] public IActionResult Create(Model model) => View(model);
}
""").Verify();

[TestMethod]
public void AvoidUnderPosting_ModelInDifferentProject_CSharp()
{
const string modelCode = """
namespace Models
{
public class Person
{
public int Age { get; set; } // FN - Roslyn can't raise an issue when the location is in different project than the one being analyzed
}
}
""";
const string controllerCode = """
using Microsoft.AspNetCore.Mvc;
using Models;

namespace Controllers
{
public class PersonController : Controller
{
[HttpPost] public IActionResult Post(Person person) => View(person);
}
}
""";
var solution = SolutionBuilder.Create()
.AddProject(AnalyzerLanguage.CSharp)
.AddSnippet(modelCode)
.Solution
.AddProject(AnalyzerLanguage.CSharp)
.AddProjectReference(x => x.ProjectIds[0])
.AddReferences(AspNetReferences)
.AddSnippet(controllerCode)
.Solution;
var compiledAspNetProject = solution.Compile()[1];
DiagnosticVerifier.Verify(compiledAspNetProject, new AvoidUnderPosting());
}
}

#endif
Loading