-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ASP010 Unexpected character in url. #19
- Loading branch information
1 parent
2de0850
commit 9b6fccd
Showing
9 changed files
with
239 additions
and
7 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
AspNetCoreAnalyzers.Tests/ASP010UrlSyntaxTests/Diagnostics.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
namespace AspNetCoreAnalyzers.Tests.ASP010UrlSyntaxTests | ||
{ | ||
using Gu.Roslyn.Asserts; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NUnit.Framework; | ||
|
||
public class Diagnostics | ||
{ | ||
private static readonly DiagnosticAnalyzer Analyzer = new AttributeAnalyzer(); | ||
private static readonly ExpectedDiagnostic ExpectedDiagnostic = ExpectedDiagnostic.Create(ASP010UrlSyntax.Descriptor); | ||
|
||
[TestCase("\"api/a↓?b/{id}\"")] | ||
public void WhenMethodAttribute(string before) | ||
{ | ||
var code = @" | ||
namespace AspBox | ||
{ | ||
using Microsoft.AspNetCore.Mvc; | ||
[ApiController] | ||
public class OrdersController : Controller | ||
{ | ||
[HttpGet(""api/a↓?b/{id}"")] | ||
public IActionResult GetId(string id) | ||
{ | ||
return this.Ok(id); | ||
} | ||
} | ||
}".AssertReplace("\"api/a↓?b/{id}\"", before); | ||
|
||
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code); | ||
} | ||
|
||
[TestCase("\"api/a↓?b\"")] | ||
public void WhenRouteAttribute(string before) | ||
{ | ||
var code = @" | ||
namespace AspBox | ||
{ | ||
using Microsoft.AspNetCore.Mvc; | ||
[Route(""api/a↓?b"")] | ||
[ApiController] | ||
public class OrdersController : Controller | ||
{ | ||
[HttpGet(""{id}"")] | ||
public IActionResult GetId(string id) | ||
{ | ||
return this.Ok(id); | ||
} | ||
} | ||
}".AssertReplace("\"api/a↓?b\"", before); | ||
|
||
AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
AspNetCoreAnalyzers.Tests/ASP010UrlSyntaxTests/ValidCode.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace AspNetCoreAnalyzers.Tests.ASP010UrlSyntaxTests | ||
{ | ||
using Gu.Roslyn.Asserts; | ||
using Microsoft.CodeAnalysis.Diagnostics; | ||
using NUnit.Framework; | ||
|
||
public class ValidCode | ||
{ | ||
private static readonly DiagnosticAnalyzer Analyzer = new AttributeAnalyzer(); | ||
|
||
[TestCase("\"{value}\"")] | ||
[TestCase("\"api/orders/{value}\"")] | ||
[TestCase("\"api/two-words/{value}\"")] | ||
public void WithParameter(string parameter) | ||
{ | ||
var code = @" | ||
namespace AspBox | ||
{ | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
[ApiController] | ||
public class OrdersController : Controller | ||
{ | ||
[HttpGet(""api/{value}"")] | ||
public IActionResult GetValue(string value) | ||
{ | ||
return this.Ok(value); | ||
} | ||
} | ||
}".AssertReplace("\"api/{value}\"", parameter); | ||
AnalyzerAssert.Valid(Analyzer, code); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
namespace AspNetCoreAnalyzers | ||
{ | ||
using Microsoft.CodeAnalysis; | ||
|
||
internal static class ASP010UrlSyntax | ||
{ | ||
public const string DiagnosticId = "ASP010"; | ||
|
||
internal static readonly DiagnosticDescriptor Descriptor = new DiagnosticDescriptor( | ||
id: DiagnosticId, | ||
title: "Unexpected character in url.", | ||
messageFormat: "Literal sections cannot contain the '{0}' character", | ||
category: AnalyzerCategory.Routing, | ||
defaultSeverity: DiagnosticSeverity.Warning, | ||
isEnabledByDefault: true, | ||
description: "Unexpected character in url.", | ||
helpLinkUri: HelpLink.ForId(DiagnosticId)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# ASP010 | ||
## Unexpected character in url. | ||
|
||
<!-- start generated table --> | ||
<table> | ||
<tr> | ||
<td>CheckId</td> | ||
<td>ASP010</td> | ||
</tr> | ||
<tr> | ||
<td>Severity</td> | ||
<td>Warning</td> | ||
</tr> | ||
<tr> | ||
<td>Enabled</td> | ||
<td>True</td> | ||
</tr> | ||
<tr> | ||
<td>Category</td> | ||
<td>AspNetCoreAnalyzers.Routing</td> | ||
</tr> | ||
<tr> | ||
<td>Code</td> | ||
<td><a href="https://github.com/DotNetAnalyzers/AspNetCoreAnalyzers/blob/master/AspNetCoreAnalyzers/Analyzers/AttributeAnalyzer.cs">AttributeAnalyzer</a></td> | ||
</tr> | ||
</table> | ||
<!-- end generated table --> | ||
|
||
## Description | ||
|
||
Unexpected character in url. | ||
|
||
## Motivation | ||
|
||
```cs | ||
[HttpGet(""api/a?b"")] | ||
public IActionResult GetId(string id) | ||
{ | ||
... | ||
} | ||
``` | ||
|
||
Throws an exception at runtime. | ||
|
||
## How to fix violations | ||
|
||
Fix the url template. | ||
|
||
<!-- start generated config severity --> | ||
## Configure severity | ||
|
||
### Via ruleset file. | ||
|
||
Configure the severity per project, for more info see [MSDN](https://msdn.microsoft.com/en-us/library/dd264949.aspx). | ||
|
||
### Via #pragma directive. | ||
```C# | ||
#pragma warning disable ASP010 // Unexpected character in url. | ||
Code violating the rule here | ||
#pragma warning restore ASP010 // Unexpected character in url. | ||
``` | ||
|
||
Or put this at the top of the file to disable all instances. | ||
```C# | ||
#pragma warning disable ASP010 // Unexpected character in url. | ||
``` | ||
|
||
### Via attribute `[SuppressMessage]`. | ||
|
||
```C# | ||
[System.Diagnostics.CodeAnalysis.SuppressMessage("AspNetCoreAnalyzers.Routing", | ||
"ASP010:Unexpected character in url.", | ||
Justification = "Reason...")] | ||
``` | ||
<!-- end generated config severity --> |