aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

Check Web APIs returning ActionResult with status code 2xx

Open sshanzel opened this issue 6 years ago • 9 comments

Describe the problem you are trying to solve

ActionResult accepts a generic parameter but doesn't enforce strict-typing (related issue). But as what we have discussed here, it is ideal to improve the Analyzers instead of making the changes with the ControllerBase.

Describe suggestions on how to achieve the rule

I am literally new with Analyzers (like a few hours ago) but it would be awesome if we will be able to check the MethodDeclarationSyntax of a ReturnStatementSyntax to see if the content type of the returned OkObjectResult matches the identified generic parameter from the method itself (please do correct me if I'm wrong on what I have initially thought for the solution).

I suggest displaying red curly underlines to notify the developer of the mismatch.

Additional context

Even though I literally haven't tried making a single rule, I did a few moments ago but failed miserably (link to the repo - actual rule). Hence, went here to seek help.

sshanzel avatar Nov 27 '20 19:11 sshanzel

I'm not really sure yet how can I infer the type from OkObjectResult returned value, being unable to have the correct type checking at: https://github.com/sshanzel/RoslynAnalyzer-Proposal/blob/a9b45300a504e7878b644cc5a08d33eddd1b6e40/ProducesResponseTypeAnalyzer/ProducesResponseTypeAnalyzer/ProducesResponseTypeAnalyzerAnalyzer.cs#L71

sshanzel avatar Nov 27 '20 21:11 sshanzel

It would go here btw https://github.com/dotnet/aspnetcore/tree/master/src/Analyzers/Analyzers/src

davidfowl avatar Nov 28 '20 19:11 davidfowl

It would go here btw https://github.com/dotnet/aspnetcore/tree/master/src/Analyzers/Analyzers/src

Thanks a lot for letting me know, I'd be sure to check this one out!

sshanzel avatar Nov 29 '20 03:11 sshanzel

Does it mean this ticket should be moved across to aspnetcore repo? If not I can have a look at the implementation but with my poor web experience I'd be happy to have someone providing "test" cases of when to report and when not to.

Evangelink avatar Dec 02 '20 10:12 Evangelink

Ping @mavasani and @davidfowl

Evangelink avatar Dec 11 '20 15:12 Evangelink

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Dec 11 '20 17:12 ghost

I see that the project already has a controllers methods analyzer for which type of objects they returns and with which http status code. It can be found in Microsoft.AspNetCore.Mvc.Api.Analyzers (see https://github.com/dotnet/aspnetcore/blob/9d91021a8af619ad212e315a3a6d441ea8ac13f8/src/Mvc/Mvc.Api.Analyzers/src/ActualApiResponseMetadataFactory.cs ). This analyzer is designed to find controller methods without the [ProducesResponseType()] attribute and create them if needed. For the most part, this analyzer does the same general work that is required for a new analyzer in this issue.

If I understand correctly, the analyzers in the Microsoft.AspNetCore.Mvc.Api.Analyzers package are obsolete.

Should the new analyzer be placed in Microsoft.AspNetCore.Analyzers? And what can be done with the code from Microsoft.AspNetCore.Mvc.Api.Analyzers that fits for the new analyzer? Copy-paste, since all code from Microsoft.AspNetCore.Mvc.Api.Analyzers will be deleted in the future at all?

fvoronin avatar Apr 22 '21 18:04 fvoronin

@davidfowl @sshanzel I have gone through the code and below is the proposed solution.

Step 1: To get the method Return type.

image

Step 2 : To get return statement return type

image

Step3: Compare step1 and step2 . If not equal then show the red bar

singh733 avatar Jun 28 '21 02:06 singh733

Hi, I’d like to take a look at this if it’s still available.

From the discussion, my understanding is that the analyzer should detect controller actions returning ActionResult<T> where a 2xx result, such as Ok(...), returns a value whose type does not match the declared T.

Before opening a PR, I wanted to confirm the expected scope. Should the initial implementation focus on common 2xx helper methods like Ok(...) and add analyzer tests for matching/mismatching ActionResult<T> return values?

If that direction sounds right, I can start with a small PR.

Wezylnia avatar May 29 '26 18:05 Wezylnia

Hi @javiercn , if possible, is this issue available to work on?

BryanAndy avatar Aug 05 '26 22:08 BryanAndy

Hi! I’m interested in working on this issue as my first ASP.NET Core contribution. I see that it is currently unassigned and has no linked pull request. Is it still available for a community contribution?

dapalamac avatar Aug 20 '26 15:08 dapalamac