Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add method description in Swagger UI in WebAPI Application

I am using Swagger as my API tooling framework and it is working out great so far. I just came across this page https://petstore.swagger.io/

and saw how each method has a description. For example,

POST: pet/ is described by add a new Pet to the store. I thought adding something like [Description("Description text")] should do it but it just does not. How can I achieve this?

like image 921
Lost Avatar asked Oct 18 '18 22:10

Lost


People also ask

How do I add method description in Swagger UI in web API application?

1 - Open the Properties dialog for your project, click the "Build" tab and ensure that "XML documentation file" is checked. This will produce a file containing all XML comments at build-time. At this point, any classes or methods that are NOT annotated with XML comments will trigger a build warning.

How do I add comments in Swagger API?

You can add and view comments on your API and Domain spec using the new Comment Bar. Click on the + button on the left of each line of the Swagger spec to add your comment from the Comment Bar. The Comment Bar houses all the comments, both resolved and unresolved, on the API spec, sorted by line.


2 Answers

This is well documented in the project: https://github.com/domaindrivendev/Swashbuckle.AspNetCore#include-descriptions-from-xml-comments


Include Descriptions from XML Comments

To enhance the generated docs with human-friendly descriptions, you can annotate controller actions and models with Xml Comments and configure Swashbuckle to incorporate those comments into the outputted Swagger JSON:

1 - Open the Properties dialog for your project, click the "Build" tab and ensure that "XML documentation file" is checked. This will produce a file containing all XML comments at build-time.

At this point, any classes or methods that are NOT annotated with XML comments will trigger a build warning. To suppress this, enter the warning code "1591" into the "Suppress warnings" field in the properties dialog.

2 - Configure Swashbuckle to incorporate the XML comments on file into the generated Swagger JSON:

services.AddSwaggerGen(c => {     c.SwaggerDoc("v1",         new Info         {             Title = "My API - V1",             Version = "v1"         }      );       var filePath = Path.Combine(System.AppContext.BaseDirectory, "MyApi.xml");      c.IncludeXmlComments(filePath); } 

3 - Annotate your actions with summary, remarks and response tags:

/// <summary> /// Retrieves a specific product by unique id /// </summary> /// <remarks>Awesomeness!</remarks> /// <response code="200">Product created</response> /// <response code="400">Product has missing/invalid values</response> /// <response code="500">Oops! Can't create your product right now</response> [HttpGet("{id}")] [ProducesResponseType(typeof(Product), 200)] [ProducesResponseType(typeof(IDictionary<string, string>), 400)] [ProducesResponseType(500)] public Product GetById(int id) 

4 - You can also annotate types with summary and example tags:

public class Product {     /// <summary>     /// The name of the product     /// </summary>     /// <example>Men's basketball shoes</example>     public string Name { get; set; }      /// <summary>     /// Quantity left in stock     /// </summary>     /// <example>10</example>     public int AvailableStock { get; set; } } 

5 - Rebuild your project to update the XML Comments file and navigate to the Swagger JSON endpoint. Note how the descriptions are mapped onto corresponding Swagger fields.

NOTE: You can also provide Swagger Schema descriptions by annotating your API models and their properties with summary tags. If you have multiple XML comments files (e.g. separate libraries for controllers and models), you can invoke the IncludeXmlComments method multiple times and they will all be merged into the outputted Swagger JSON.


Following the instructions carefully you should get something that looks like:
https://swashbucklenetcore.azurewebsites.net/swagger/

like image 50
Helder Sepulveda Avatar answered Sep 29 '22 01:09

Helder Sepulveda


For ASP.Net Core projects:

  1. install nuget package Swashbuckle.AspNetCore.Annotations

  2. Use SwaggerOperation attribute for a methods like [SwaggerOperation(Summary = "Write your summary here")]

  3. Enable annotations in Startup method ConfigureServices like the following:

    services.AddSwaggerGen(c => {     c.EnableAnnotations();     c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" }); }); 
  4. To exclude public method from appearing in swagger ui use attribute [ApiExplorerSettings(IgnoreApi = true)]. It is useful cause these methods can break swagger for some reason.

Launch project, go to localhost:[port number]/swagger and enjoy.

like image 45
alanextar Avatar answered Sep 29 '22 03:09

alanextar