Swashbuckle.AspNetCore
Swashbuckle.AspNetCore copied to clipboard
Response type is ignored when add Filters
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));
})
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. :-)