Skip to content

Commit

Permalink
ASP007: test when multiple [Route] on class.
Browse files Browse the repository at this point in the history
  • Loading branch information
JohanLarsson committed Jan 19, 2019
1 parent 955e79a commit 1a6122c
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public IActionResult Get()
}
}
}";
var message = "The route template has parameter id that does not have a corresponding method parameter.";
var message = "The route template has parameter 'id' that does not have a corresponding method parameter.";
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage(message), code);
}

Expand Down Expand Up @@ -83,7 +83,7 @@ public IActionResult Get(int orderId)
}
}
}";
var message = "The route template has parameter itemId that does not have a corresponding method parameter.";
var message = "The route template has parameter 'itemId' that does not have a corresponding method parameter.";
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage(message), order, db, code);
}

Expand Down Expand Up @@ -195,8 +195,8 @@ namespace AspBox
{
using Microsoft.AspNetCore.Mvc;
[Route(""api/values/{↓valueId}/items"")]
[Route(""api/values/{↓valueId}/items/{itemId}"")]
[Route(""api/values"")]
[Route(""api/values/{↓id}"")]
[ApiController]
public class OrdersController : Controller
{
Expand All @@ -207,6 +207,30 @@ public IActionResult Get()
}
}
}";
var message = "The route template has parameter 'id' that does not have a corresponding method parameter.";
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic.WithMessage(message), code);
}

[Test]
public void WhenMultipleRouteAttributesMissingActionWithParameter()
{
var code = @"
namespace AspBox
{
using Microsoft.AspNetCore.Mvc;
[Route(""api/values"")]
[Route(""api/values/{↓id}"")]
[ApiController]
public class ValuesController : Controller
{
[HttpGet]
public IActionResult Get()
{
return this.Ok();
}
}
}";

AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,16 @@ namespace AspBox
public class OrdersController : Controller
{
[HttpGet]
public IActionResult GetValue()
public IActionResult Get()
{
return this.Ok();
}
[HttpGet]
public IActionResult Get(int id)
{
return this.Ok(id);
}
}
}";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AspNetCoreAnalyzers.Tests.ASP011RouteParameterNotUniqueTests
namespace AspNetCoreAnalyzers.Tests.ASP011RouteParameterNameMustBeUniqueTests
{
using Gu.Roslyn.Asserts;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AspNetCoreAnalyzers.Tests.ASP011RouteParameterNotUniqueTests
namespace AspNetCoreAnalyzers.Tests.ASP011RouteParameterNameMustBeUniqueTests
{
using Gu.Roslyn.Asserts;
using Microsoft.CodeAnalysis.Diagnostics;
Expand Down
2 changes: 1 addition & 1 deletion AspNetCoreAnalyzers/ASP007MissingParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ internal static class ASP007MissingParameter
internal static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor(
id: DiagnosticId,
title: "The method has no corresponding parameter.",
messageFormat: "The route template has parameter {0} that does not have a corresponding method parameter.",
messageFormat: "The route template has parameter '{0}' that does not have a corresponding method parameter.",
category: AnalyzerCategory.Routing,
defaultSeverity: DiagnosticSeverity.Warning,
isEnabledByDefault: true,
Expand Down
45 changes: 12 additions & 33 deletions AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ namespace AspNetCoreAnalyzers
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Gu.Roslyn.AnalyzerExtensions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
Expand Down Expand Up @@ -583,41 +582,21 @@ private static bool HasMissingMethodParameter(PathSegment segment, SyntaxNodeAna
context.Node.TryFirstAncestor(out ClassDeclarationSyntax classDeclaration) &&
!classDeclaration.Modifiers.Any(SyntaxKind.PartialKeyword))
{
foreach (var attributeList in classDeclaration.AttributeLists)
{
foreach (var attribute in attributeList.Attributes)
{
if (UrlAttribute.TryCreate(attribute, context, out var urlAttribute))
{
if (urlAttribute.UrlTemplate == null)
{
location = null;
name = null;
return false;
}

if (urlAttribute.UrlTemplate is UrlTemplate template &&
!template.Path.Any(x => x.Parameter?.Name.TextEquals(templateParameter.Name) == true))
{
location = null;
name = null;
return false;
}
}
}
}

foreach (var member in classDeclaration.Members)
{
if (member is MethodDeclarationSyntax candidate &&
HasHttpVerbAttribute(candidate, context) &&
!TryFindParameter(templateParameter, candidate, out _))
TryFindParameter(templateParameter, candidate, out _))
{
location = templateParameter.Name.GetLocation();
name = templateParameter.Name.ToString();
return true;
location = null;
name = null;
return false;
}
}

location = templateParameter.Name.GetLocation();
name = templateParameter.Name.ToString();
return true;
}
}

Expand Down Expand Up @@ -885,13 +864,13 @@ StringBuilderPool.PooledStringBuilder ClassName()
return builder.Append("Controller");
}

bool Equals(StringBuilderPool.PooledStringBuilder builder, string text)
bool Equals(StringBuilderPool.PooledStringBuilder x, string y)
{
if (builder.Length == containingType.Name.Length)
if (x.Length == y.Length)
{
for (var i = 0; i < builder.Length; i++)
for (var i = 0; i < x.Length; i++)
{
if (builder[i] != containingType.Name[i])
if (x[i] != y[i])
{
return false;
}
Expand Down

0 comments on commit 1a6122c

Please sign in to comment.