Skip to content

API analyzers should support undeclared 200 status codes #15385

Open
@jawn

Description

@jawn

Describe the bug

API analyzers should flag an undeclared 200 status code, and provide an option to fix it.

To Reproduce

Steps to reproduce the behavior:

  1. Using ASP.NET Core 3.0 with analyzers enabled
<PropertyGroup>
 <IncludeOpenAPIAnalyzers>true</IncludeOpenAPIAnalyzers>
</PropertyGroup>
  1. Run this code (adapted from this sample with the ProducesResponseType attribute removed)
// GET api/contacts/{guid}
[HttpGet("{id}", Name = "GetById")]
public IActionResult Get(string id)
{
    var contact = _contacts.Get(id);

    if (contact == null)
    {
        return NotFound();
    }

    return Ok(contact);
}

  1. The API analyzer flags the NotFound using a warning (this is expected).
  2. The API analyzer does not flag the return Ok(...); (not expected)

Expected behavior

  1. The API analyzer does flag the return Ok(...) using a warning.
  2. An option to fix this warning is provided, choosing it adds the following attribute
    [ProducesResponseType(typeof(Contact), StatusCodes.Status200OK)]

Why I think this is a bug

From the docs:

The analyzers package notifies you of any controller action that:

  • Returns an undeclared status code.
  • Returns an undeclared success result.

Additional context

Follow up to dotnet/AspNetCore.Docs#15269 (comment)

.NET Core SDK: 3.0.100
.NET Core runtimes installed:
Microsoft.NETCore.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.WindowsDesktop.App 3.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority:3Work that is nice to haveaffected-fewThis issue impacts only small number of customersanalyzerIndicates an issue which is related to analyzer experiencearea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesenhancementThis issue represents an ask for new feature or an enhancement to an existing onefeature-openapiseverity-blockingThis label is used by an internal tool

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions