ApiConvention Analyzer behaviour for Non-200 Success messages
See original GitHub issueIs this a Bug or Feature request?:
Bug
Steps to reproduce (preferably a link to a GitHub repo with a repro project):
- Create WebApp with 2.2 and paste the repro controller from below
- Add the analyzer package Microsoft.AspNetCore.Mvc.Api.Analyzers
- Look at the suggestions/warnings
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:
- Created 5 years ago
- Comments:8 (7 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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.