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. Ignacio's avatar Ignacio 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. Timsothybioli's avatar Timsothybioli says:

    Hi! This is my 1st comment here so I just wanted to give a quick shout out and say I truly enjoy reading through your posts. Can you suggest any other blogs/websites/forums that go over the same topics? Thank you so much!
    https://smartbuildforum.com.ua/yak-zrobyty-idealnyi-shov-na-plastyku-pislya-1.html

  3. Adrian Dugre's avatar Adrian Dugre says:

    boostsignals – Loved the layout today; clean, simple, and genuinely user-friendly overall.

  4. Sook's avatar Sook 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. Florencio's avatar Florencio 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.

  6. Louis's avatar Louis 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.

  7. Cordell's avatar Cordell 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.

  8. Norris's avatar Norris 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. Diamond's avatar Diamond 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. Deeanna's avatar Deeanna 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. Jamie's avatar Jamie 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. Austin's avatar Austin 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.

  13. Jon's avatar Jon 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. Manuela's avatar Manuela 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.

  15. Pennie's avatar Pennie 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.

  16. Latoria's avatar Latoria 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.

  17. Thea's avatar Thea 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.

  18. Pennie's avatar Pennie 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. Christen's avatar Christen 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.

  20. Katherina's avatar Katherina 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.

  21. Camellia's avatar Camellia 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.

  22. Sunday's avatar Sunday 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.

  23. Shaun's avatar Shaun 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.

  24. seonexus – Navigation felt smooth, found everything quickly without any confusing steps.

  25. seoshift – Overall, professional vibe here; trustworthy, polished, and pleasantly minimal throughout.

  26. Hung Mcnish's avatar Hung Mcnish says:

    seovault – Appreciate the typography choices; comfortable spacing improved my reading experience.

Comments are closed.