Skip to content

Commit 0b03e5d

Browse files
committed
Don't use reserved words as route parameter names. close #37
1 parent e9396b7 commit 0b03e5d

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

AspNetCoreAnalyzers.Tests/ASP008ValidRouteParameterNameTests/CodeFix.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,31 @@ public IActionResult GetId(string id)
4949
}".AssertReplace("\"api/orders/{id}\"", after);
5050
AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode);
5151
}
52+
53+
[TestCase("\"api/orders/{↓action}\"")]
54+
[TestCase("\"api/orders/{↓area}\"")]
55+
[TestCase("\"api/orders/{↓controller}\"")]
56+
[TestCase("\"api/orders/{↓handler}\"")]
57+
[TestCase("\"api/orders/{↓page}\"")]
58+
public void NoFixWhen(string before)
59+
{
60+
var code = @"
61+
namespace ValidCode
62+
{
63+
using Microsoft.AspNetCore.Mvc;
64+
65+
[ApiController]
66+
public class OrdersController : Controller
67+
{
68+
[HttpGet(""api/orders/{id}"")]
69+
public IActionResult GetId(string id)
70+
{
71+
return this.Ok(id);
72+
}
73+
}
74+
}".AssertReplace("\"api/orders/{id}\"", before);
75+
76+
AnalyzerAssert.NoFix(Analyzer, Fix, ExpectedDiagnostic, code);
77+
}
5278
}
5379
}

AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,17 @@ private static bool HasInvalidName(PathSegment segment, out Location location, o
504504
correctName = parameter.Name.ToString().Trim();
505505
return true;
506506
}
507+
508+
if (parameter.Name.Equals("action", StringComparison.OrdinalIgnoreCase) ||
509+
parameter.Name.Equals("area", StringComparison.OrdinalIgnoreCase) ||
510+
parameter.Name.Equals("controller", StringComparison.OrdinalIgnoreCase) ||
511+
parameter.Name.Equals("handler", StringComparison.OrdinalIgnoreCase) ||
512+
parameter.Name.Equals("page", StringComparison.OrdinalIgnoreCase))
513+
{
514+
location = parameter.Name.GetLocation();
515+
correctName = null;
516+
return true;
517+
}
507518
}
508519

509520
location = null;

0 commit comments

Comments
 (0)