Skip to content

Commit 44a0106

Browse files
committed
ASP001 handle parameter with type.
1 parent a9e1750 commit 44a0106

7 files changed

Lines changed: 130 additions & 7 deletions

File tree

AspNetCoreAnalyzers.Tests/ASP001ParameterNameTests/ValidCode.cs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,118 @@ public async Task<IActionResult> GetOrder(int id)
6565
AnalyzerAssert.Valid(Analyzer, order, db, code);
6666
}
6767

68+
[Test]
69+
public void ImplicitOptionalFromRoute()
70+
{
71+
var order = @"
72+
namespace ValidCode
73+
{
74+
public class Order
75+
{
76+
public int Id { get; set; }
77+
}
78+
}";
79+
80+
var db = @"
81+
namespace ValidCode
82+
{
83+
using Microsoft.EntityFrameworkCore;
84+
85+
public class Db : DbContext
86+
{
87+
public DbSet<Order> Orders { get; set; }
88+
}
89+
}";
90+
var code = @"
91+
namespace ValidCode
92+
{
93+
using System.Threading.Tasks;
94+
using Microsoft.AspNetCore.Mvc;
95+
using Microsoft.EntityFrameworkCore;
96+
97+
[ApiController]
98+
public class OrdersController : Controller
99+
{
100+
private readonly Db db;
101+
102+
public OrdersController(Db db)
103+
{
104+
this.db = db;
105+
}
106+
107+
[HttpGet(""api/orders/{id?}"")]
108+
public async Task<IActionResult> GetOrder(int? id)
109+
{
110+
var match = id == null
111+
? await this.db.Orders.FirstOrDefaultAsync()
112+
: await this.db.Orders.FirstOrDefaultAsync(x => x.Id == id);
113+
if (match == null)
114+
{
115+
return this.NotFound();
116+
}
117+
118+
return this.Ok(match);
119+
}
120+
}
121+
}";
122+
AnalyzerAssert.Valid(Analyzer, order, db, code);
123+
}
124+
125+
[Test]
126+
public void ImplicitTypedFromRoute()
127+
{
128+
var order = @"
129+
namespace ValidCode
130+
{
131+
public class Order
132+
{
133+
public int Id { get; set; }
134+
}
135+
}";
136+
137+
var db = @"
138+
namespace ValidCode
139+
{
140+
using Microsoft.EntityFrameworkCore;
141+
142+
public class Db : DbContext
143+
{
144+
public DbSet<Order> Orders { get; set; }
145+
}
146+
}";
147+
var code = @"
148+
namespace ValidCode
149+
{
150+
using System.Threading.Tasks;
151+
using Microsoft.AspNetCore.Mvc;
152+
using Microsoft.EntityFrameworkCore;
153+
154+
[ApiController]
155+
public class OrdersController : Controller
156+
{
157+
private readonly Db db;
158+
159+
public OrdersController(Db db)
160+
{
161+
this.db = db;
162+
}
163+
164+
[HttpGet(""api/orders/{id:int}"")]
165+
public async Task<IActionResult> GetOrder(int id)
166+
{
167+
var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == id);
168+
if (match == null)
169+
{
170+
return this.NotFound();
171+
}
172+
173+
return this.Ok(match);
174+
}
175+
}
176+
}";
177+
AnalyzerAssert.Valid(Analyzer, order, db, code);
178+
}
179+
68180
[Test]
69181
public void ExplicitFromRoute()
70182
{

AspNetCoreAnalyzers/ASP001ParameterName.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal static class ASP001ParameterName
1010
id: DiagnosticId,
1111
title: "The parameter name does not match the url parameter.",
1212
messageFormat: "The parameter name does not match the url parameter.",
13-
category: AnalyzerCategory.Correctness,
13+
category: AnalyzerCategory.Routing,
1414
defaultSeverity: DiagnosticSeverity.Hidden,
1515
isEnabledByDefault: true,
1616
description: "The parameter name does not match the url parameter.",

AspNetCoreAnalyzers/ASP002MissingParameter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ internal static class ASP002MissingParameter
1010
id: DiagnosticId,
1111
title: "The method has no corresponding parameter.",
1212
messageFormat: "The method has no corresponding parameter.",
13-
category: AnalyzerCategory.Correctness,
13+
category: AnalyzerCategory.Routing,
1414
defaultSeverity: DiagnosticSeverity.Hidden,
1515
isEnabledByDefault: true,
1616
description: "The method has no corresponding parameter.",

AspNetCoreAnalyzers/AnalyzerCategory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ namespace AspNetCoreAnalyzers
22
{
33
internal static class AnalyzerCategory
44
{
5-
internal const string Correctness = "AspNetCoreAnalyzers.Correctness";
5+
internal const string Routing = "AspNetCoreAnalyzers.Routing";
66
}
77
}

AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,17 @@ private static bool TryGetParameterName(string text, out string name)
8484
end--;
8585
}
8686

87+
for (var i = start; i < end; i++)
88+
{
89+
switch (text[i])
90+
{
91+
case '?':
92+
case ':':
93+
end = i;
94+
break;
95+
}
96+
}
97+
8798
name = text.Substring(start, end - start);
8899
return true;
89100
}

documentation/ASP001.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</tr>
1818
<tr>
1919
<td>Category</td>
20-
<td>AspNetCoreAnalyzers.Correctness</td>
20+
<td>AspNetCoreAnalyzers.Routing</td>
2121
</tr>
2222
<tr>
2323
<td>Code</td>
@@ -88,7 +88,7 @@ Or put this at the top of the file to disable all instances.
8888
### Via attribute `[SuppressMessage]`.
8989

9090
```C#
91-
[System.Diagnostics.CodeAnalysis.SuppressMessage("AspNetCoreAnalyzers.Correctness",
91+
[System.Diagnostics.CodeAnalysis.SuppressMessage("AspNetCoreAnalyzers.Routing",
9292
"ASP001:The parameter name does not match the url parameter.",
9393
Justification = "Reason...")]
9494
```

documentation/ASP002.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</tr>
1818
<tr>
1919
<td>Category</td>
20-
<td>AspNetCoreAnalyzers.Correctness</td>
20+
<td>AspNetCoreAnalyzers.Routing</td>
2121
</tr>
2222
<tr>
2323
<td>Code</td>
@@ -74,7 +74,7 @@ Or put this at the top of the file to disable all instances.
7474
### Via attribute `[SuppressMessage]`.
7575

7676
```C#
77-
[System.Diagnostics.CodeAnalysis.SuppressMessage("AspNetCoreAnalyzers.Correctness",
77+
[System.Diagnostics.CodeAnalysis.SuppressMessage("AspNetCoreAnalyzers.Routing",
7878
"ASP002:The method has no corresponding parameter.",
7979
Justification = "Reason...")]
8080
```

0 commit comments

Comments
 (0)