25

I am trying to use Autorest and Swagger documentation created by the help of Swashbuckle.AspNetCore (3.0.0) in order to generate a REST API client.

The swagger documentation generated seems to be correct except that the operation name are not really nice.

"/api/Addresses/{id}": {
      "get": {
        "tags": [ "Address" ],
        "operationId": "ApiAddressesByIdGet",
        "consumes": [],
        "produces": [],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "type": "string",
            "format": "uuid"
          }
        ],
        "responses": { "200": { "description": "Success" } }
      },

I saw in many articles and also on the official documentation of SwashBuckle.AspNetCore that I can use an attribute to decorate my controller method like this:

[HttpGet]
[Produces("application/json")]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType(typeof(List<AddressDto>), (int)HttpStatusCode.OK)]
[SwaggerOperation("GetAllAdresses")]
public async Task<IActionResult> GetAllAsync()
{
   ....
}

Unfortunately, I got an error:

SwaggerOperationAttribute could not be found

I verified the NuGet packages installed and they are:

  • SwashBuckle.AspNetCore.Swagger (3.0.0)
  • SwashBuckle.AspNetCore.SwaggerGen (3.0.0)
  • SwashBuckle.AspNetCore.SwaggerUI (3.0.0)

How can I solve the problem?

Pang
  • 9,564
  • 146
  • 81
  • 122
Sébastien Krejci
  • 431
  • 1
  • 4
  • 9

2 Answers2

58

I ran across this today. I needed to add the following nuget package that was just added for V3.0.0:

Swashbuckle.AspNetCore.Annotations

Breaking changes are described here

Please also note that you need to add the following to Startup.cs or your Swagger extension:

AddSwaggerGen(c => { ... c.EnableAnnotations(); })
Keith Hodo
  • 123
  • 1
  • 8
ScottG
  • 10,711
  • 25
  • 82
  • 111
  • 6
    Thanks that helped me out. Also good to comment, at first it did not work for me I had to enable Annotations in Startup.cs services.AddSwaggerGen(c => { ... c.EnableAnnotations(); }); More info: https://github.com/domaindrivendev/Swashbuckle.AspNetCore#swashbuckleaspnetcoreannotations – Guido Kersten Aug 23 '18 at 19:39
  • Thank you so very much! I needed to override the operationid and I was able to do so! See [Github](https://github.com/domaindrivendev/Swashbuckle.AspNetCore#swashbuckleaspnetcoreannotations) for everything you can do with annotations! – Keith Hodo Dec 11 '18 at 02:51
  • @ScottG are you able to help with this? https://stackoverflow.com/questions/72853488/display-different-controller-name-in-swagger – David Klempfner Jul 04 '22 at 08:04
4

Another option is to set Name property for your HttpGet filter like this:

[HttpGet(Name = "GetAllAdresses")]
[Produces("application/json")]
[ProducesResponseType((int)HttpStatusCode.NotFound)]
[ProducesResponseType(typeof(List<AddressDto>), (int)HttpStatusCode.OK)]
[SwaggerOperation("GetAllAdresses")]
public async Task<IActionResult> GetAllAsync()
{
   ....
}
Pang
  • 9,564
  • 146
  • 81
  • 122
CoOl
  • 2,637
  • 21
  • 24