Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ASP001 handle route on class #55

Open
JohanLarsson opened this issue Jan 16, 2019 · 5 comments
Open

ASP001 handle route on class #55

JohanLarsson opened this issue Jan 16, 2019 · 5 comments

Comments

@JohanLarsson
Copy link
Collaborator

JohanLarsson commented Jan 16, 2019

        [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
Copy link
Collaborator Author

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
Copy link
Collaborator Author

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
Copy link

Ekkeir commented Jun 26, 2020

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
Copy link
Collaborator Author

JohanLarsson commented Jun 26, 2020

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
Copy link

Ekkeir commented Jun 26, 2020

Done
And happy to help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants