question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ApiConvention Analyzer behaviour for Non-200 Success messages

See original GitHub issue

Is this a Bug or Feature request?:

Bug

Steps to reproduce (preferably a link to a GitHub repo with a repro project):

  1. Create WebApp with 2.2 and paste the repro controller from below
  2. Add the analyzer package Microsoft.AspNetCore.Mvc.Api.Analyzers
  3. Look at the suggestions/warnings

screenshot

Description of the problem:

The analyzer apparently does not accept 204 NoContent as a valid success status code for controller actions which do not directly create the ActionResult object in the action method. Instead it shows: API1001: API1001 Action method returns a success result without a corresponding ProducesResponseType. See Action Delete and Convention Delete

It apparently works if you create the ActionResult directly (Convention AnotherDelete) or if you add StatusCode.200OK to the API conventions (Convention MoreDelete).

Whatever you do to handle 200OK, you should do the same for 204 and probably 201 too.

Please ignore the async hints, they are not related.

Version of Microsoft.AspNetCore.Mvc or Microsoft.AspNetCore.App or Microsoft.AspNetCore.All:

2.2

Repro:

using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;

namespace ApiConventionsDeletebug.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [ApiConventionType(typeof(MyConventions))]
    public class ValuesController : ControllerBase
    {
        [HttpDelete("{id}")]
        public Task<ActionResult> Delete(int id)
        {
            return DeleteInternal(id);
        }

        private async Task<ActionResult> DeleteInternal(int id)
        {
            return NoContent();
        }


        [HttpDelete("/another/{id}")]
        public async Task<ActionResult> AnotherDelete(int id)
        {
            return NoContent();
        }

        [HttpDelete("/More/{id}")]
        public Task<ActionResult> MoreDelete(int id)
        {
            return MoreDeleteInternal(id);
        }

        private async Task<ActionResult> MoreDeleteInternal(int id)
        {
            return Ok();
        }
    }

    public static class MyConventions
    {
        [ProducesResponseType(StatusCodes.Status204NoContent)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        [ProducesDefaultResponseType]
        public static void Delete(
            [ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
            int id)
        {
        }

        [ProducesResponseType(StatusCodes.Status204NoContent)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        [ProducesDefaultResponseType]
        public static void AnotherDelete(
            [ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
            int id)
        {
        }

        [ProducesResponseType(StatusCodes.Status200OK)]
        [ProducesResponseType(StatusCodes.Status404NotFound)]
        [ProducesResponseType(StatusCodes.Status400BadRequest)]
        [ProducesDefaultResponseType]
        public static void MoreDelete(
            [ApiConventionTypeMatch(ApiConventionTypeMatchBehavior.Any)]
            int id)
        {
        }
    }
}

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
OldrichDlouhycommented, Jun 4, 2020

Why is this bug closed when it is not fixed?

I would understand giving this issue low priority, but closing it just because people do not comment on it does not make the bug report invalid.

1reaction
mkArtakMSFTcommented, May 21, 2020

Hi. Thanks for contacting us. We’re closing this issue as there was not much community interest in this ask for quite a while now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Use web API analyzers
The analyzers package notifies you of any controller action that: Returns an undeclared status code. Returns an undeclared success result.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found