Add OpenAPI client code generation to .NET 6 apps using dotnet-openapi, NSwag and service references in Rider

Abdullah Ilgaz
2 min readNov 21, 2022

--

OpenAPI is a specification for describing REST APIs. It is a standard that is used by many tools and services. It is also a standard that is supported by .NET 6. In this article, I will show you how to use OpenAPI to generate client code for .NET 6 apps.

Getting started

To get started, you need OpenAPI JSON or YAML file for your API. I’ve created a minimal API using .NET 6 and Swashbuckle. It generates OpenAPI JSON file at /swagger/v1/swagger.json endpoint.

Adding OpenAPI client code generation to .NET 6 apps using dotnet-openapi

The first way to add OpenAPI client code generation to .NET 6 apps is to use dotnet-openapi tool. It is a .NET tool that is installed with .NET 6 SDK. It can be used to add OpenAPI reference to .NET 6 apps.

Adding OpenAPI reference to .NET 6 apps

To add OpenAPI reference to .NET 6 apps, you need to run the following command:

dotnet openapi add file swagger.json

This command will add OpenAPI reference to your .NET 6 app. It will also add OpenApiReference item to your project file. It will look like this:

<ItemGroup>
<OpenApiReference Include="swagger.json" />
</ItemGroup>

Generating client code using OpenAPI reference in Rider

With Rider, you can generate client code using OpenAPI reference. To do that, you need to right-click on your project file and select Run option.

--

--

Abdullah Ilgaz

Muslim • Enthusiast • Serverless Developer • Code Artisan • MVP • fullstack, serverless, aws, dotnet, typescript, reactjs, nextjs