Skip to content

2.2 Adding ProducesResponseType overwrites default HTTP OK response. #3513

Closed
@TrieBr

Description

@TrieBr

I just upgraded my ASP 2.1 project to 2.2.0-preview. I upgraded solely on "API Controller conventions" outlined in the 2.2 Roadmap to ease documentation generation.

When I have an action on a controller without a ProducesRespponseType attribute, a default HTTP status 200 is output in ApiExplorer inferring from the ActionResult type.

        [HttpPost("register")]
        [AllowAnonymous]
        public async Task<ActionResult<ApiResponse<ApiResonseNoData>>> Register([FromBody] RegisterViewModel viewModel)
        {
            ValidateModel();
            var u = new User { UserName = viewModel.Username, Email = viewModel.Username, FirstName = "", LastName = "" };
            var create = await _userManager.CreateAsync(u, viewModel.Password);
            return ApiResponse("Success");
        }

By inspecting an instance of ApiExplorer (IApiDescriptionGroupCollectionProvider) In Visual Studio, I can see there is a single entry in SupportedResponseTypes for the HTTP 200.

However, When I add an HTTP 422 status like the following:

    [HttpPost("register")]
        [AllowAnonymous]
        [ProducesResponseType(typeof(ApiResponse<ValidationMessageSet>), (int)HttpStatusCode.UnprocessableEntity)]
        public async Task<ActionResult<ApiResponse<ApiResonseNoData>>> Register([FromBody] RegisterViewModel viewModel)
        {
            ValidateModel();
            var u = new User { UserName = viewModel.Username, Email = viewModel.Username, FirstName = "", LastName = "" };
            var create = await _userManager.CreateAsync(u, viewModel.Password);
            return ApiResponse("Success");
        }

Suddenly this becomes the only ResoponseType in ApiExplorer. If the intention is to get rid of "Attribute Soup", any ProducesRespponseType attributes added to an action should be added in addition to the default HTTP 200 response based on the ActionResult in my opinion. Eg If I add a ProducesResponseType of HttpStatusCode.UnprocessableEntity, then Api Explorer should have a 422 response and a 200 response.

On an semi-related note, since ApiControllers Provide Automatic 400 responses for validation errors, it would also make sense for ASP Core to automatically provide this response in one of the valid response types. With the above bug (feature?), I now have to add a HTTP 400 (or 422 in my case) response for validation errors and a HTTP 200 response which again results in Attribute Soup. Maybe I'm doing something wrong or misunderstanding something?

.NET Core SDK (reflecting any global.json):
Version: 2.2.100-preview1-009349
Commit: 3f45457548

Runtime Environment:
OS Name: Windows
OS Version: 10.0.17134
OS Platform: Windows
RID: win10-x64
Base Path: C:\Program Files\dotnet\sdk\2.2.100-preview1-009349\

Host (useful for support):
Version: 2.2.0-preview-26820-02
Commit: b4726350b3

.NET Core SDKs installed:
2.1.202 [C:\Program Files\dotnet\sdk]
2.1.302 [C:\Program Files\dotnet\sdk]
2.2.100-preview1-009349 [C:\Program Files\dotnet\sdk]

.NET Core runtimes installed:
Microsoft.AspNetCore.All 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.All 2.2.0-preview1-35029 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
Microsoft.AspNetCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.AspNetCore.App 2.2.0-preview1-35029 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
Microsoft.NETCore.App 2.0.9 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.1.2 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
Microsoft.NETCore.App 2.2.0-preview-26820-02 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions