Skip to content

Instantly share code, notes, and snippets.

@stand-sure
Last active March 22, 2024 17:01
Show Gist options
  • Save stand-sure/7533ca6b401f2a0ae0950bd1f8aa081c to your computer and use it in GitHub Desktop.
Save stand-sure/7533ca6b401f2a0ae0950bd1f8aa081c to your computer and use it in GitHub Desktop.
How to generate a populated OpenAPI spec for .net8 minimal API and hot to generate a client using Kiota

Tools to install

Generating the OpenAPI file

This is done in the csproj file for the service as a post build step. See Service.csproj.

The following generates the file into the root of the project, which is desirable, since another project is going to use the file:

dotnet swagger tofile --output $(ProjectDir)/Service_openapi.json $(TargetPath) openapi

Replace, as needed

  • Service_openapi.json, which is the name of the file to output
  • openapi, at the end of the command, which is the name of the document. This is set via the SwaggerDoc method on SwaggerGenOptions, often invoked via calling the AddSwaggerGen extension method on an IServiceCollection instance.

Generating the client

BEFORE generation it is wise to set up the folder location and add the project.

The following command will indicate what packages you will need to add:

kiota info -d "~/src/Service/Service_openapi.json" -l CSharp

The command to generate the client is as follows:

kiota generate --language CSharp --class-name MyClassName --namespace-name MyNamespace --openapi $(SolutionDir)/src/Service/Service_openapi.json --output $(SolutionDir)/src/Client

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<RootNamespace>Demos.OAS.Client</RootNamespace>
<GenerateAssemblyInfo>true</GenerateAssemblyInfo>
<FileVersion>0.1.0.0</FileVersion>
<LangVersion>latest</LangVersion>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Version>0.1.0</Version>
<Title>Demo OpenAPI Client</Title>
<Authors>stand__sure</Authors>
<RepositoryUrl>__NUGET_PACKAGE_REPO_URL__</RepositoryUrl>
<PackageId>Demos.OAS.Client</PackageId>
<Description>Kiota-generated SDK Client</Description>
<Copyright>Copyright 2024 stand__sure</Copyright>
<PackageProjectUrl>__NUGET_PACKAGE_PROJECT_URL__</PackageProjectUrl>
<PackageTags>openapi;dotnet;</PackageTags>
<AssemblyName>Demos.OAS.Client</AssemblyName>
<RuntimeIdentifiers>win-x64|osx-x64|osx-arm64|linux-x64;</RuntimeIdentifiers>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<IsPackable>true</IsPackable>
<PackageReadMeFile>README.md</PackageReadMeFile>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Kiota.Abstractions" Version="1.8.0"/>
<PackageReference Include="Microsoft.Kiota.Authentication.Azure" Version="1.1.4"/>
<PackageReference Include="Microsoft.Kiota.Http.HttpClientLibrary" Version="1.3.7"/>
<PackageReference Include="Microsoft.Kiota.Serialization.Form" Version="1.1.5"/>
<PackageReference Include="Microsoft.Kiota.Serialization.Json" Version="1.2.0"/>
<PackageReference Include="Microsoft.Kiota.Serialization.Multipart" Version="1.1.3"/>
<PackageReference Include="Microsoft.Kiota.Serialization.Text" Version="1.1.4"/>
</ItemGroup>
<ItemGroup>
<None Include="README.md" Pack="true" PackagePath="\"/>
</ItemGroup>
</Project>
<Project Sdk="Microsoft.NET.Sdk.Web">
<!-- ... elided ... -->
<!-- Generate openapi and client -->
<Target Name="OpenAPI" AfterTargets="Build" Condition="$(Configuration)=='Debug'">
<Exec Command="dotnet swagger tofile --output $(ProjectDir)/Service_openapi.json $(TargetPath) openapi" WorkingDirectory="$(TargetDir)" />
<Exec Command="kiota generate --language CSharp --class-name MyClassName --namespace-name MyNamespace --openapi $(SolutionDir)/src/Service/Service_openapi.json --output $(SolutionDir)/src/Client" WorkingDirectory="$(TargetDir)" />
</Target>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment