DotNetAnalyzers / AspNetCoreAnalyzers

MIT License
62 stars 4 forks source link

ASP001 handle route on class #55

Open JohanLarsson opened 5 years ago

JohanLarsson commented 5 years ago
        [Test]
        public void WhenRouteOnClass()
        {
            var code = @"
namespace AspBox
{
    using Microsoft.AspNetCore.Mvc;

    [Route(""api/values/{id}"")]
    [ApiController]
    public class OrdersController : Controller
    {
        [HttpGet]
        public IActionResult GetValue(int ↓wrong)
        {
            return this.Ok(wrong);
        }
    }
}";

            var fixedCode = @"
namespace AspBox
{
    using Microsoft.AspNetCore.Mvc;

    [Route(""api/values/{id}"")]
    [ApiController]
    public class OrdersController : Controller
    {
        [HttpGet]
        public IActionResult GetValue(int id)
        {
            return this.Ok(id);
        }
    }
}";

            AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode);
        }
JohanLarsson commented 5 years ago

Maybe ASP002

        [Test]
        public void WhenRouteOnClass()
        {
            var code = @"
namespace AspBox
{
    using Microsoft.AspNetCore.Mvc;

    [Route(""api/values/{↓wrong}"")]
    [ApiController]
    public class OrdersController : Controller
    {
        [HttpGet]
        public IActionResult GetValue(int id)
        {
            return this.Ok(id);
        }
    }
}";

            var fixedCode = @"
namespace AspBox
{
    using Microsoft.AspNetCore.Mvc;

    [Route(""api/values/{id}"")]
    [ApiController]
    public class OrdersController : Controller
    {
        [HttpGet]
        public IActionResult GetValue(int id)
        {
            return this.Ok(id);
        }
    }
}";

            AnalyzerAssert.CodeFix(Analyzer, Fix, ExpectedDiagnostic, code, fixedCode);
        }
JohanLarsson commented 5 years ago

And ASP007


        [Test]
        public void WhenRouteOnClass()
        {
            var code = @"
namespace AspBox
{
    using Microsoft.AspNetCore.Mvc;

    [Route(""api/values/{↓id}"")]
    [ApiController]
    public class OrdersController : Controller
    {
        [HttpGet]
        public IActionResult GetValue()
        {
            return this.Ok();
        }
    }
}";

            AnalyzerAssert.Diagnostics(Analyzer, ExpectedDiagnostic, code);
        }
Ekkeir commented 4 years ago

I'm not sure if this issue is relevant . Please, let me know if I should create a new one or comment on another one.

The problem

The warning below is emitted when building my project where route is defined on class. CSC : warning AD0001: Analyzer 'AspNetCoreAnalyzers.AttributeAnalyzer' threw an exception of type 'System.IndexOutOfRangeException' with message 'Index was outside the bounds of the array.'.

To Reproduce

Create new empty ASP Net Core Web Application Add the following class

using Microsoft.AspNetCore.Mvc;

namespace AnalyzerBugRepro
{
    [ApiController]
    [Route("api/")]
    public class GrantStubController : ControllerBase
    {
    }
}

Build the project Observe the error in build output.

JohanLarsson commented 4 years ago

Ah, that looks like a separate/new issue.

Never hesitate to create issues, especially beautiful issues with repro code like you provided. Makes adding a test and then fixing fast and easy.

Thanks for reporting!

Ekkeir commented 4 years ago

Done And happy to help!