According to OpenAPI 3.0 it isn’t possible. But what if you really want it to be? Do you have to just settle and allow your Swagger documentation to be wrong? I’m here to show you how to make optional route parameters with Swagger and ASP.NET Core.

Before we begin let’s evaluate the scenario. ASP.NET Core allows us to define optional route parameters by adding a question-mark at the end of the key. I don’t want to debate the merits or technicalities of this functionality. Let’s also not argue whether or not we’re breaking OpenAPI specs by doing it. The fact is, it’s possible in ASP.NET Core and it’d be nice to support it in Swagger. Code for today’s post is located on my GitHub. All pertinent examples are also inline images.

Wait, what’s Swagger?

On the off-chance you navigated to this post and don’t know what Swagger is, how about I give a quick introduction? “Swagger is a set of rules for a format describing REST apis… as a result, it can be used to share documentation among product managers, testers and developers…” – Getting Started with Swagger.

One common usage of Swagger is to also provide an interface via Swagger UI. Swagger UI allows you to visualize and interact with the API’s resources. It is similar to yet more directed than Postman or like tooling.

#.net #.net core #asp.net #programming #openapi

Optional Route Parameters with Swagger and ASP.NET Core
31.05 GEEK