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

  3. Reynaldo's avatar Reynaldo 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. Sharla's avatar Sharla 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. Oren's avatar Oren 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. Lucien's avatar Lucien 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. Mitchel's avatar Mitchel 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. Harlan's avatar Harlan 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.

  9. Jerry's avatar Jerry 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. 12play's avatar 12play says:

    บทความนี้ ให้ข้อมูลดี ครับ
    ผม เพิ่งเจอข้อมูลเกี่ยวกับ เนื้อหาในแนวเดียวกัน

    สามารถอ่านได้ที่ 12play
    เผื่อใครสนใจ
    เพราะให้ข้อมูลเชิงลึก
    ขอบคุณที่แชร์ คอนเทนต์ดีๆ นี้
    และอยากเห็นบทความดีๆ แบบนี้อีก

  11. Maisie's avatar Maisie 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. Garret's avatar Garret 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. Cliff's avatar Cliff 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. Yuette's avatar Yuette 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. Rochel's avatar Rochel 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. pompom 456's avatar pompom 456 says:

    บทความนี้ อ่านแล้วได้ความรู้เพิ่ม ค่ะ
    ผม ได้อ่านบทความที่เกี่ยวข้องกับ เนื้อหาในแนวเดียวกัน
    ที่คุณสามารถดูได้ที่ pompom 456
    เผื่อใครสนใจ
    มีการยกตัวอย่างที่เข้าใจง่าย
    ขอบคุณที่แชร์ ข้อมูลที่มีประโยชน์
    นี้
    จะรอติดตามเนื้อหาใหม่ๆ ต่อไป

  17. Krishna's avatar Krishna 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. Franklyn's avatar Franklyn 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. Kunststof Brillenmonturen NieuwegeinErfstede, 3431
    KH Nieuwegeinhttps://kunststofkozijnennieuwegein.nl/Kunststof brillenmonturen verschaffen veelzijdige opties in Nederland.
    Kunststof brillenmonturen ondersteunen een brede keuze
    in kleuren en vormen. Kunststof brillenmonturen helpen zowel herenbrillen als damesbrillen. Kunststof brillenmonturen verschaffen ook kinderbrillen en sportbrillen. Kunststof brillenmonturen verlenen een assortiment van transparante en gekleurde
    monturen. Kunststof brillenmonturen ondersteunen voor comfortabele pasvormen door lichtgewicht materiaal.

    Kunststof brillenmonturen verlenen garantie op duurzaamheid.
    Kunststof brillenmonturen bieden multifocale
    glazen voor verschillende sterktes. Kunststof
    brillenmonturen ondersteunen oogzorg met een oogmeting.
    Kunststof brillenmonturen faciliteren de optie om zonnebrillen te bestellen. Kunststof brillenmonturen bieden acetaat
    en plastic als materiaalkeuze. Kunststof brillenmonturen leveren transparante kunststof
    brillenglazen. Kunststof brillenmonturen bieden titanium en metalen als alternatieve monturen. Kunststof brillenmonturen faciliteren voor een breed assortiment aan merken. Kunststof brillenmonturen bieden aantrekkelijke prijzen voor elke klant.
    Kunststof brillenmonturen ondersteunen onderhoud en reparatie.
    Kunststof brillenmonturen helpen ook de keuze voor multifocale lenzen.
    Kunststof brillenmonturen verschaffen coatings voor extra
    bescherming. Kunststof brillenmonturen verschaffen leesbrillen voor optimaal zicht.
    Kunststof brillenmonturen verlenen een collectie van moderne en klassieke stijlen.
    Kunststof brillenmonturen leveren ook op maat gemaakte oplossingen. Kunststof brillenmonturen ondersteunen een naadloze brillenervaring in Nederland.
    Kunststof brillenmonturen verlenen een perfecte balans
    tussen stijl en functionaliteit. Kunststof brillenmonturen helpen een innovatieve kijk op brillenmode.

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

  22. Jordan's avatar Jordan 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. Wilmer's avatar Wilmer 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. Verlene's avatar Verlene 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. Iva's avatar Iva 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. Shauna's avatar Shauna says:

    ข้อมูลชุดนี้ มีประโยชน์มาก ค่ะ
    ผม ได้อ่านบทความที่เกี่ยวข้องกับ
    เนื้อหาในแนวเดียวกัน
    ซึ่งอยู่ที่ Shauna
    เผื่อใครสนใจ
    เพราะอธิบายไว้ละเอียด
    ขอบคุณที่แชร์ ข้อมูลที่มีประโยชน์ นี้
    และอยากเห็นบทความดีๆ แบบนี้อีก

Comments are closed.