Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swashbuckle parameter descriptions

Tags:

I'm using SwaggerResponse attributes to decorate my api controller actions, this all works fine, however when I look at the generated documentation the description field for parameters is empty.

Is a there an attribute based approach to describe action parameters (rather than XML comments)?

like image 753
Slicc Avatar asked Jun 07 '16 13:06

Slicc


People also ask

How do I give a parameter description in Swagger?

To describe a parameter, you specify its name , location ( in ), data type (defined by either schema or content ) and other attributes, such as description or required . Here is an example: paths: /users/{userId}:

How do you pass multiple parameters in Swagger?

If you are trying to send a body with multiple parameters, add an object model in the definitions section and refer it in your body parameter, see below (works with editor.swagger.io):

What is AddSwaggerGen?

AddSwaggerGen is an extension method to add swagger services to the collection. To configure Swagger, you invoke the method SwaggerDoc. Passing an Info object, you can define the title, description, contact information, and more in code file Startup. cs.

What is the use of swashbuckle AspNetCore?

Swashbuckle is an open source project for generating Swagger documents for Web APIs that are built with ASP.NET Core. There are three core components: AspNetCore. SwaggerGen - provides the functionality to generate JSON Swagger documents that describe the objects, methods, return types, etc.


2 Answers

With the latest Swashbuckle, or better said at least the Swashbuckle.AspNetCore variant which I'm using, the Description field for parameters can now be displayed correctly as output.

It does require the following conditions to be met:

  • XML comments must be enabled and configured with Swagger
  • Parameters should be explicitly decorated with either [FromRoute], [FromQuery], [FromBody] or [FromUri]
  • The same for the method type (get/post/put etc.), which should be decorated with [Http...]
  • Describe the parameter as usual with a <param ...> xml comment

A full sample looks like this:

/// <summary> /// Short, descriptive title of the operation /// </summary> /// <remarks> /// More elaborate description /// </remarks> /// <param name="id">Here is the description for ID.</param> [ProducesResponseType(typeof(Bar), (int)HttpStatusCode.OK)] [HttpGet, Route("{id}", Name = "GetFoo")] public async Task<IActionResult> Foo([FromRoute] long id) {     var response = new Bar();     return Ok(response); } 

Which produces the following output:

Swagger output with parameter description

like image 146
Juliën Avatar answered Sep 18 '22 20:09

Juliën


You should confirm you are allowing Swagger to use XML comments

httpConfig.EnableSwagger(c => {                 if (GetXmlCommentsPath() != null) {                     c.IncludeXmlComments(GetXmlCommentsPath());                 } ... ... );  protected static string GetXmlCommentsPath() {     var path = HostingEnvironment.MapPath("path to your xml doc file");     return path; } 

You should also check you are generating XML doc for your desired project. Under your desired project Properties (Alt + Enter on top of the project or Right Click -> Properties) -> Build -> Check XML documentation file

like image 39
Alfredo A. Avatar answered Sep 17 '22 20:09

Alfredo A.