Skip to content

Commit 99b4059

Browse files
committed
Check argument for min, mac, minlength & maxlength. #26
1 parent 6c1a6a4 commit 99b4059

3 files changed

Lines changed: 37 additions & 1 deletion

File tree

AspNetCoreAnalyzers.Tests/ASP004ParameterSyntaxTests/Diagnostics.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ public class Diagnostics
1212
[TestCase("api/orders/{id:↓wrong}")]
1313
[TestCase("api/orders/{id:min1)}")]
1414
[TestCase("api/orders/{id:max1)}")]
15+
[TestCase("api/orders/{id:min(↓wrong))}")]
16+
[TestCase("api/orders/{id:max(↓wrong))}")]
17+
[TestCase("api/orders/{id:minlength(↓wrong))}")]
18+
[TestCase("api/orders/{id:maxlength(↓wrong))}")]
1519
[TestCase("api/orders/{id:↓:int)}")]
1620
[TestCase("api/orders/{id:int:↓)}")]
1721
[TestCase("api/orders/{id:int:↓:)}")]

AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,15 @@ private static bool HasWrongSyntax(PathSegment segment, out Location location, o
241241
}
242242
}
243243

244+
if (HasWrongIntArgumentSyntax(constraint, "min", out location) ||
245+
HasWrongIntArgumentSyntax(constraint, "max", out location) ||
246+
HasWrongIntArgumentSyntax(constraint, "minlength", out location) ||
247+
HasWrongIntArgumentSyntax(constraint, "maxlength", out location))
248+
{
249+
correctSyntax = null;
250+
return true;
251+
}
252+
244253
if (!text.Equals("?", StringComparison.OrdinalIgnoreCase) &&
245254
!text.Equals("int", StringComparison.OrdinalIgnoreCase) &&
246255
!text.Equals("bool", StringComparison.OrdinalIgnoreCase) &&
@@ -286,10 +295,31 @@ private static bool HasWrongSyntax(PathSegment segment, out Location location, o
286295
}
287296
}
288297

289-
290298
location = null;
291299
correctSyntax = null;
292300
return false;
301+
302+
bool HasWrongIntArgumentSyntax(RouteConstraint constraint, string methodName, out Location result)
303+
{
304+
var text = constraint.Span.Text;
305+
if (text.Length > methodName.Length + 2 &&
306+
text.StartsWith(methodName, StringComparison.OrdinalIgnoreCase) &&
307+
text[methodName.Length] == '(' &&
308+
text[text.Length - 1] == ')')
309+
{
310+
for (var i = methodName.Length + 1; i < text.Length - 2; i++)
311+
{
312+
if (!char.IsDigit(text[i]))
313+
{
314+
result = constraint.Span.GetLocation(i);
315+
return true;
316+
}
317+
}
318+
}
319+
320+
result = null;
321+
return false;
322+
}
293323
}
294324
}
295325
}

AspNetCoreAnalyzers/Helpers/Span.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ public override int GetHashCode()
5555

5656
public Location GetLocation() => Location.Create(this.literal.SyntaxTree, new TextSpan(this.literal.SpanStart + this.TextSpan.Start + 1, this.TextSpan.Length));
5757

58+
public Location GetLocation(int start) => Location.Create(this.literal.SyntaxTree, new TextSpan(this.literal.SpanStart + this.TextSpan.Start + start + 1, this.TextSpan.Length));
59+
5860
internal Span Slice(int start, int end)
5961
{
6062
if (start > end)

0 commit comments

Comments
 (0)