I'm using Swashbuckle and Swagger UI in a .NET Core 3.1 project, with my XML comments imported into Swagger. I have a POST request on a controller that I want to register a number of response statuses (201, 401, 403, 404) for in Swagger. The problem is that I'm also seeing a 200 Success response listed alongside my explicitly specified status code responses in the swagger.json
file and the Swagger UI interface.
As suggested in multiple different places I'm using the [SwaggerResponseRemoveDefaults]
attribute to try and prevent this, however everything I try still results in the default 200 response being listed.
I've tried:
- adding the attribute to the method,
- adding the attribute to the controller,
- adding the attribute to the abstract base controller,
and all combinations of the above. I've also tried these in combination with:
- specifying desired response types with XML
<response code="XXX"></response>
comment tags, and - specifying desired response types with
[SwaggerResponse(XXX)]
endpoints.
Nothing results in the removal of the 200 Success result from my Swagger UI and swagger.json
.
TrackerController.cs
/// <summary>...</summary>
/// <response code="401">User is not authenticated.</response>
/// <response code="404">Tracker not found.</response>
[Authorize]
[ApiController]
[Route("[controller]")]
[SwaggerResponseRemoveDefaults]
public partial class TrackersController : AbstractController
{
...
/// <summary>...</summary>
/// <param name="tracker">The details of the tracker to be created.</param>
/// <response code="201">The tracker was successfully created.</response>
/// <response code="403">User is not authorized to modify this resource.</response>
[HttpPost]
[SwaggerResponseRemoveDefaults]
[ResponseType(typeof(TrackerDto))]
[SwaggerResponse(201, Description = "The tracker was successfully created.")]
public async Task<IActionResult> CreateTracker([FromBody] TrackerDto tracker)
{
...
}
...
}
swagger.json
{
"openapi": "3.0.1",
"info": {
"title": "My API",
"version": "v1"
},
"paths": {
"/Trackers": {
"post": {
"tags": [
"Trackers"
],
"summary": "Create a new tracker.",
"requestBody": {
"description": "The details of the tracker to be created.",
"content": {
"application/json-patch+json": {
"schema": {
"$ref": "#/components/schemas/TrackerDto"
}
},
"application/json": {
"schema": {
"$ref": "#/components/schemas/TrackerDto"
}
},
"text/json": {
"schema": {
"$ref": "#/components/schemas/TrackerDto"
}
},
"application/*+json": {
"schema": {
"$ref": "#/components/schemas/TrackerDto"
}
}
}
},
"responses": {
"200": {
"description": "Success"
},
"201": {
"description": "The tracker was successfully created."
},
"401": {
"description": "User is not authenticated."
},
"403": {
"description": "User is not authorized to modify this resource."
},
"404": {
"description": "Tracker not found."
}
}
}
}
}
}