Skip to content
Unknown's avatar

Vicluar Daniel Merchan

  • My Twitter Account
  • My LinkedIn Profile
  • My Github Profile
  • Cloud
    • Amazon Web Services (AWS)
    • Azure
  • Software Development
    • .NET
    • Patterns and Practices
  • DevOps
  • Me

How to automate Swagger/OpenAPI metadata generation for your ASP.NET Core Web API

Vicluar Daniel Merchan .NET, Software Development February 29, 2020November 6, 2021 2 Minutes

OpenAPI, formerly known as Swagger, is a specification for generating machine readable metadata for describing, document and consume RestAPI. It is becoming a standard within the industry to fully document with easy complex RestAPI, like those required for Microservice architectures.

The OpenAPI/Swagger specification is language/framework agnostic. It allows RestAPI clients to understand and consume services without any knowledge of the internal implementation details.

ASP.NET Core Web API allow us to create OpenAPI/Swagger metadata in an automatic fashion through the Nuget package Swashbuckle. Let’s see how easy it is to do it.

Setting up the ASP.NET Core Web API project

First of all, we must need to create an ASP.NET Core Web API project. Steps are so easy that it is not worthy to show them here.

For our example, we are going to create a simple Web API controller that returns some mock data, like the following:

Controller code - Swagger/OpenAPI sample

As you may see, I intentionally have added some attributes to the public methods that describe the type of response each method would return. This is important for OpenAPI/Swagger to understand the type of responses your service would produce.

As this is a very simple example to highlight how to automate the Swagger metadata creation, let’s not create any other controller.

Installing and configuring Swashbuckle.AspNetCore package

Now, we need to add the Swashbuckle.AspNetCore nuget package (there is a version for .NET Framework as well) to our ASP.NET Core Web API project.

Once it is installed, we can proceed with the next steps, configuring the Swagger service in our Startup.cs class.

In the ConfigureServices method, we need to add the following code:

Configure Services - OpenAPI/Swagger

There are additional options that you can configure for your service, such as Terms of Service or License. That is up to you.

Next, we need to tell ASP.NET Core to include the use of Swagger in the Configure method in the Startup.cs class:

Code at Configure - OpenAPI/Swagger

Now, we have all set up. Let’s run the Web API to see how our Swagger metadata looks like.

Running our Web API and taking a look at Swagger metadata and UI

When running our app, at the URL /swagger/v1/swagger.json we will see the Swagger metadata, as you can see in the following image:

Swagger metadata.

An additional feature is that you will be able to see the Swagger UI running under your Web API host. Just go to the url: /swagger as you may see in the following image:

Swagger UI

From now on, you only need to code your further controllers, adding the description attributes and deploy your app. The above set up would produce Swagger metadata automatically as well as reflecting those changes in the Swagger UI.

If you want to see the full code for this sample, go to my github account where you will find the repo: here

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X
Like Loading...

Related

  • Tagged
  • ASP.NET Core Web API
  • OpenAPI
  • Swagger
  • Swagger metadata
Unknown's avatar

Published by Vicluar Daniel Merchan

Solutions Architect at AWS | Software Architect | Software Engineer | Systems Engineer | Cloud and #AWS focus | Father of a toddler. View all posts by Vicluar Daniel Merchan

Published February 29, 2020November 6, 2021

Post navigation

Previous Post Object Oriented Analysis and Design notes
Next Post Continuous Integration and Continuous Delivery pipeline in Azure DevOps for Azure App Service – Part I

RSS

  • RSS - Posts
Follow Vicluar Daniel Merchan on WordPress.com
Blog at WordPress.com.
  • Reblog
  • Subscribe Subscribed
    • Vicluar Daniel Merchan
    • Already have a WordPress.com account? Log in now.
    • Vicluar Daniel Merchan
    • Subscribe Subscribed
    • Sign up
    • Log in
    • Copy shortlink
    • Report this content
    • View post in Reader
    • Manage subscriptions
    • Collapse this bar
 

Loading Comments...
 

    %d