Creating a public-facing API requires you to somehow provide documentation to your consumers. Luckily for us, Swagger has been around for a while, and it provides tremendous value straight out of the box. With some tweaks, we can make it even better, such as showing all possible values of an enum, as we’re going to see in this article.
TL;DR
A simple example can be found in this repository, more specifically in this commit.
Analyzing the code
Our changes will be on top of a project created in a previous article, called Super Fair Cassino API. The project provides a simple “Bet” endpoint, which ensures that the house always wins, of course. The first thing that we need for our example is, obviously, an enum. So let’s create a very simple one called “Game”, which our API consumers can specify when placing bets.
The only exceptional thing in this snippet is the JsonConverter attribute. With that attribute, Swagger will display the enum values as text, instead of 0, 1 and 2. The next and last thing we need to do is to add the new parameter to our endpoint.
With the [FromQuery] attribute, we specify that this parameter will come from the request query string. And that’s all we need! Now once we run our API, we will see all the available values in Swagger. If you are consuming the swagger.json file, you can find the values under Components > Schemas.