1

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."
          }
        }
      }
    }
  }
}

Swagger UI Screenshot

Sam W
  • 13
  • 1
  • 6

1 Answers1

0

Right now, you are telling Swagger you are producing a TrackerDto with the default type of 200 and in addition another response of 201. You need to make that one single matching pair.

Drop the ResponseType attribute and put the type where it belongs:

[SwaggerResponse(201, Description = "The tracker was successfully created.", typeof(TrackerDto))]

You could also try:

[ProducesResponseType(typeof(TrackerDto), 201)]

The important thing is to put the type and the status code in the same attribute so Swagger knows they belong together.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
  • Tried this and this didn't solve it either - I've got another test controller method that is stripped down to the simplest possible scenario: with no abstract base controller, no `[Authorize]` attributes, only XML comment `` tags for response types and the `[SwaggerResponseRemoveDefaults]` attribute still doesn't remove the *200 Success* response. – Sam W Feb 24 '20 at 15:13
  • Can you try to drop *everything*, all other attributes and comments (except for maybe the HTTP verb and AllowAnonymous) and just use one of the options in the answer please? – nvoigt Feb 24 '20 at 15:18
  • 1
    For some reason `SwaggerResponse` attributes aren't being picked up by Swashbuckle for me, but I can confirm that the `[ProducesResponseType(typeof(TrackerDto), 201)]` approach does remove the default *200 Success* response from the list. To add a description for the response types I'm just using XML comment docs as well and it's linking the two together. – Sam W Feb 24 '20 at 15:44