Skip to content

Commit aae475e

Browse files
committed
Fix tests.
1 parent 74f3952 commit aae475e

3 files changed

Lines changed: 100 additions & 3 deletions

File tree

AspNetCoreAnalyzers.Tests/ASP001ParameterNameTests/CodeFix.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,5 +406,71 @@ public async Task<IActionResult> GetOrder([FromRoute]int id)
406406
}";
407407
AnalyzerAssert.CodeFix(Analyzer, RenameParameterFix, ExpectedDiagnostic, new[] { order, db, before }, after);
408408
}
409+
410+
[Test]
411+
public void BothParameters()
412+
{
413+
var orderItem = @"
414+
namespace ValidCode
415+
{
416+
public class OrderItem
417+
{
418+
public int Id { get; set; }
419+
}
420+
}";
421+
var order = @"
422+
namespace ValidCode
423+
{
424+
public class Order
425+
{
426+
public int Id { get; set; }
427+
428+
public IEnumerable<OrderItem> Items { get; set; }
429+
}
430+
}";
431+
432+
var db = @"
433+
namespace ValidCode
434+
{
435+
using Microsoft.EntityFrameworkCore;
436+
437+
public class Db : DbContext
438+
{
439+
public DbSet<Order> Orders { get; set; }
440+
}
441+
}";
442+
var before = @"
443+
namespace ValidCode
444+
{
445+
using System.Threading.Tasks;
446+
using Microsoft.AspNetCore.Mvc;
447+
using Microsoft.EntityFrameworkCore;
448+
449+
[ApiController]
450+
public class OrdersController : Controller
451+
{
452+
private readonly Db db;
453+
454+
public OrdersController(Db db)
455+
{
456+
this.db = db;
457+
}
458+
459+
[HttpGet(""api/orders/{orderId}/items/{itemId}"")]
460+
public async Task<IActionResult> GetOrder↓(int wrong1, int wrong2)
461+
{
462+
var match = await this.db.Orders.FirstOrDefaultAsync(x => x.Id == wrong1)?.FirstOrDefaultAsync(x => x.Id == wrong2);
463+
if (match == null)
464+
{
465+
return this.NotFound();
466+
}
467+
468+
return this.Ok(match);
469+
}
470+
}
471+
}";
472+
473+
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, orderItem, order, db, before);
474+
}
409475
}
410476
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace AspNetCoreAnalyzers
2+
{
3+
using Microsoft.CodeAnalysis;
4+
5+
internal static class ASP003ParameterType
6+
{
7+
public const string DiagnosticId = "ASP003";
8+
9+
internal static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor(
10+
id: DiagnosticId,
11+
title: "Parameter type does not match template.",
12+
messageFormat: "Parameter type does not match template.",
13+
category: AnalyzerCategory.Routing,
14+
defaultSeverity: DiagnosticSeverity.Hidden,
15+
isEnabledByDefault: true,
16+
description: "Parameter type does not match template.",
17+
helpLinkUri: HelpLink.ForId(DiagnosticId));
18+
}
19+
}

AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,25 @@ context.ContainingSymbol is IMethodSymbol method &&
3131
using (var pairs = GetPairs(template, method))
3232
{
3333
if (pairs.TrySingle(x => x.Template == null, out var withMethodParameter) &&
34-
pairs.TrySingle(x => x.Method == null, out var withTemplateParameter))
34+
methodDeclaration.TryFindParameter(withMethodParameter.Method.Name, out var parameterSyntax) &&
35+
pairs.TrySingle(x => x.Method == null, out var withTemplateParameter) &&
36+
withTemplateParameter.Template is TemplateParameter templateParameter)
3537
{
3638
context.ReportDiagnostic(
3739
Diagnostic.Create(
3840
ASP001ParameterName.Descriptor,
39-
withMethodParameter.Method.Locations.Single(),
40-
ImmutableDictionary<string, string>.Empty.Add(nameof(NameSyntax), withTemplateParameter.Template.Value.Name.Text)));
41+
parameterSyntax.Identifier.GetLocation(),
42+
ImmutableDictionary<string, string>.Empty.Add(
43+
nameof(NameSyntax),
44+
templateParameter.Name.Text)));
45+
}
46+
else if (pairs.Count(x => x.Template == null) > 1 &&
47+
pairs.Count(x => x.Method == null) > 1)
48+
{
49+
context.ReportDiagnostic(
50+
Diagnostic.Create(
51+
ASP001ParameterName.Descriptor,
52+
methodDeclaration.ParameterList.GetLocation()));
4153
}
4254

4355
if (pairs.TrySingle(x => x.Template != null, out _) &&

0 commit comments

Comments
 (0)