API Part 7 – Swagger Comments

In the previous post, I introduced Swagger and showed how to set up your project for Swagger. In this post I will show how to add Swagger comments to annotate your program.

In earlier posts we looked at the database of cars and the Get method that retrieves the entire list. That can be quite a lot of data going over the wire. What we want instead is to send pages. I’ll show that briefly and then we’ll annotate that code.

In the CarController we’ll have a Get method that takes three parameters: showDeleted, pageNumber and pageSize. The first we’ve seen before, it determines whether the list returned to the caller will include our deleted records. The second, pageNumber, will designate which page of data we want to return (zero based). The third parameter, pageSize, will designate how many records to return per page.

Thus, if we were to write

Get(false,3,4)

we would expect to get back records 17, 18, 19 and 20, because page 0 would have records 1-4, page 1 would have 5-8, etc.

Here is the compete endpoint that will directly call our repository (we won’t bother with a service for this example).

public async Task<IEnumerable<Car>> Get([FromRoute] bool showDeleted, int pageNumber, int pageSize )
{
    return await _carRepository.Get(showDeleted, pageNumber, pageSize);
}

When the repository is called, the parameters are passed in, and it does its magic. For now, however, we are only concerned with documenting this method. We do that with XML comments, designated by three forward slash marks (///).

First, we will document the purpose of the endpoint; that is, what it does. We do that in the summary

/// <summary>
/// Get all the cars in the Database 
/// </summary>

Next, we want to document the parameters

/// <param name="returnDeletedRecords">If true, the method will return all the records,
/// including the ones that have been deleted</param>
/// <param name="pageOffset">which page to display</param>
/// <param name="pageSize">how many records to display per page</param>

Finally, I’m going to document the possible response codes. Note, these are, by no means, all that you can document, but they’ll give us a good idea of how it is done

/// <response code="200">Cars returned</response>
/// <response code="404">Specified Car not found</response>
/// <response code="500">An Internal Server Error prevented the request from being executed.</response>

When an error is encountered and we return one of these codes, the text we’ve specified goes along for the ride, which is enormously helpful to the calling client.

At the top of the swagger page is our summary

At each parameter is our designated text

Finally, each of the error codes is documented

It is a best practice to document all of the endpoints. Many developers go further, and document all of the methods in both the service and the repository; that is a decision to be made by the team. Since the endpoint is the only thing visible to the client, it has the greatest claim on your time and effort. Remember, “comments rust,” and that is as true for Swagger comments as any other.

Unknown's avatar

About Jesse Liberty

** Note ** Jesse is currently looking for a new position. You can learn more about him at https://jesseliberty.bio Thank you. Jesse Liberty has three decades of experience writing and delivering software projects and is the author of 2 dozen books and a couple dozen online courses. His latest book, Building APIs with .NET, is now available wherever you buy your books. Liberty was a Team Lead and Senior Software Engineer for various corporations, a Senior Technical Evangelist for Microsoft, a Distinguished Software Engineer for AT&T, a VP for Information Services for Citibank and a Software Architect for PBS. He is a 13 year Microsoft MVP.
This entry was posted in API, C#, Essentials and tagged . Bookmark the permalink.

2,572 Responses to API Part 7 – Swagger Comments

  1. Courtney's avatar Courtney says:

    RepairCdDvD v2.7 is a data recovery tool designed for recovering information from damaged CD and DVD media, effectively restoring data from corrupted DVDs. This software proves very helpful in cases when discs are scratched or poorly recorded. The operations of the program are performed without any risk, and the process of disk repair is carried out by means of an ISO image.

  2. @@#LowPrice&HighQuality-Guestpost【dr50+real-traffic】Telegram @buycasinolink

  3. Jasper's avatar Jasper says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  4. Lupe's avatar Lupe says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  5. Carleen's avatar Carleen says:

    RepairCdDvD v2.7 is a data recovery tool designed for recovering information from damaged CD and DVD media, effectively restoring data from corrupted DVDs. This software proves very helpful in cases when discs are scratched or poorly recorded. The operations of the program are performed without any risk, and the process of disk repair is carried out by means of an ISO image.

  6. Chana's avatar Chana says:

    RepairCdDvD v2.7 is a data recovery tool designed for recovering information from damaged CD and DVD media, effectively restoring data from corrupted DVDs. This software proves very helpful in cases when discs are scratched or poorly recorded. The operations of the program are performed without any risk, and the process of disk repair is carried out by means of an ISO image.

  7. EarnestCor's avatar EarnestCor says:

    Hello! I realize this is sort of off-topic however I needed to ask. Does operating a well-established website such as yours take a lot of work? I am brand new to running a blog but I do write in my diary every day. I’d like to start a blog so I can share my experience and feelings online. Please let me know if you have any suggestions or tips for new aspiring blog owners. Appreciate it!
    https://drive.google.com/file/d/1rZJO0VVuKhx0lI7gEkQPQBN3NuTt0Fqx/view?usp=sharing

  8. Willene's avatar Willene says:

    RepairCdDvD v2.7 is a data recovery tool designed for recovering information from damaged CD and DVD media, effectively restoring data from corrupted DVDs. This software proves very helpful in cases when discs are scratched or poorly recorded. The operations of the program are performed without any risk, and the process of disk repair is carried out by means of an ISO image.

  9. Mistie's avatar Mistie says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  10. Gabriela's avatar Gabriela says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  11. Herta's avatar Herta says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  12. Dewayne's avatar Dewayne says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  13. Kasey's avatar Kasey says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  14. Antonio's avatar Antonio says:

    RepairCdDvD v2.7 is a data recovery tool designed for recovering information from damaged CD and DVD media, effectively restoring data from corrupted DVDs. This software proves very helpful in cases when discs are scratched or poorly recorded. The operations of the program are performed without any risk, and the process of disk repair is carried out by means of an ISO image.

  15. Belkis's avatar Belkis says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  16. Palmer's avatar Palmer says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  17. Edie's avatar Edie says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  18. Bonny's avatar Bonny says:

    RepairCdDvD v2.7 is a data recovery tool designed for recovering information from damaged CD and DVD media, effectively restoring data from corrupted DVDs. This software proves very helpful in cases when discs are scratched or poorly recorded. The operations of the program are performed without any risk, and the process of disk repair is carried out by means of an ISO image.

  19. Violette's avatar Violette says:

    RepairCdDvD v2.7 is a data recovery tool designed for recovering information from damaged CD and DVD media, effectively restoring data from corrupted DVDs. This software proves very helpful in cases when discs are scratched or poorly recorded. The operations of the program are performed without any risk, and the process of disk repair is carried out by means of an ISO image.

  20. Carlena's avatar Carlena says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  21. binaryforge – Found practical insights today; sharing this article with colleagues later.

  22. bitcore – Pages loaded fast, images appeared sharp, and formatting stayed consistent.

  23. Alfredo's avatar Alfredo says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  24. Refugio's avatar Refugio says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  25. Ronald's avatar Ronald says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  26. Kieth's avatar Kieth says:

    RepairCdDvD v2.7 is a data recovery tool designed for recovering information from damaged CD and DVD media, effectively restoring data from corrupted DVDs. This software proves very helpful in cases when discs are scratched or poorly recorded. The operations of the program are performed without any risk, and the process of disk repair is carried out by means of an ISO image.

  27. Tammara's avatar Tammara says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

  28. Hans's avatar Hans says:

    RepairCdDvD v2.7 is a software tool designed to retrieve data from scratched or damaged CDs and DVDs, including formats such as CD+R/RW and DVD+R/RW, BD.

Comments are closed.