Swashbuckle.AspNetCore icon indicating copy to clipboard operation
Swashbuckle.AspNetCore copied to clipboard

Response type is ignored when add Filters

Open Merynek opened this issue 3 years ago • 3 comments

In startup.cs I add filter for all Error response, bcs I return own error object (ErrorResponse) by throw exception and in middleware I return this code in response. But these errors are not as a return type in controllers methods so I have to add filter to all responses. It works and in swagger.json are these responses but without response type (200) there are only error responses.

services.AddMvc(options =>  {
                options.EnableEndpointRouting = false;
                options.Filters.Add(new Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute(typeof(ErrorResponse), (int)HttpStatusCode.BadRequest));
                options.Filters.Add(new Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute(typeof(ErrorResponse), (int)HttpStatusCode.Unauthorized));
                options.Filters.Add(new Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute(typeof(ErrorResponse), (int)HttpStatusCode.NotFound));
                options.Filters.Add(new Microsoft.AspNetCore.Mvc.ProducesResponseTypeAttribute(typeof(ErrorResponse), (int)HttpStatusCode.InternalServerError));
            })

image

Here is example one of my method with return type but it doesnt display in swagger.

public async Task<ActionResult<HorseEntity>> GetHorseById(int id) 

Does anybody know how can I preserve default return type from controllers methods? Thank you. :-)

Merynek avatar Nov 11 '21 08:11 Merynek