Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define controller descriptions in ASP.NET Core Swagger (Swashbuckle.AspNetCore)?

I am trying Swagger in ASP.NET Core WebApi project and everything works fine - except controller descriptions.

For instance, I have UredskoPoslovanjeController and description in Swagger UI is UredskoPoslovanje and I can not find a way to change it.

Only solution I found is listed here However, I think this is in conflict with API versions since versioning uses exact same attribute [ApiExplorerSettings(GroupName="v2")]

Here is swagger.json for this part: UredskoPoslovanje part in swagger.json

And my controlle is defined like this:

   /// <summary>
    /// Uredsko poslovanje API
    /// </summary>
    [Authorize]
    [Route("api/[controller]")]
    public class UredskoPoslovanjeController : Controller
    {
        private LinkDbContext ctx;

        public UredskoPoslovanjeController(LinkDbContext ctx)
        {
            this.ctx = ctx;
        }

        /// <summary>
        /// Vraća broj pismena za zadani OIB
        /// </summary>
        /// <param name="OIB">OIB korisnika za koji se traži broj pismena</param>
        /// <returns>Vraća broj pronađenih pismena</returns>
        /// <response code="200">Vraća broj pismena za traženi OIB</response>
        /// <response code="400">OIB ne postoji</response>        
        /// <response code="401">Nemate pristup metodi (neispravna autorizacija)</response>        
        [HttpGet("BrojPismena/{oib}")]
        public ActionResult<BrojPismenaModel> DajBrojPismena(string OIB)
        {
            if (string.IsNullOrWhiteSpace(OIB)) return BadRequest("OIB ne smije biti prazan");
            else
            {
                var osoba = ctx.Osoba.FirstOrDefault(x => x.Oib == OIB);
                if (osoba == null) return BadRequest($"Osoba s OIB-om '{OIB}' ne postoji!");
                else
                {
                    return Ok(new BrojPismenaModel() { OIB = OIB, BrojPismena = ctx.UpPismeno.Count() });
                }
            }            
        }
    }

I would expect "Uredsko poslovanje API" as controller description, but that does not happen - swagger ui screenshot

Any idea how to properly set controller description?

Thanks, Mario

like image 754
mariob Avatar asked May 24 '18 13:05

mariob


1 Answers

By default the comments for the controller are not included. The IncludeXmlComments method has a version that takes a boolean to indicate whether the controller XML comments should be used. The code below is from my Startup : ConfigureServices method.

Original:

var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath);

New:

var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
c.IncludeXmlComments(xmlPath, true);  // <== Added the true here, to show the controller description
like image 156
Brad Bruce Avatar answered Oct 31 '22 03:10

Brad Bruce