Skip to content

Instantly share code, notes, and snippets.

@stand-sure
Last active March 22, 2024 17:01
Show Gist options
  • Select an option

  • Save stand-sure/7533ca6b401f2a0ae0950bd1f8aa081c to your computer and use it in GitHub Desktop.

Select an option

Save stand-sure/7533ca6b401f2a0ae0950bd1f8aa081c to your computer and use it in GitHub Desktop.

Revisions

  1. stand-sure revised this gist Mar 22, 2024. 1 changed file with 6 additions and 3 deletions.
    9 changes: 6 additions & 3 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -6,12 +6,13 @@

    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.
    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.
    - `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.

    @@ -20,7 +21,9 @@ often invoked via calling the `AddSwaggerGen` extension method on an `IServiceCo
    *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`
  2. stand-sure created this gist Mar 22, 2024.
    40 changes: 40 additions & 0 deletions Client.csproj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    <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>
    26 changes: 26 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    ## Tools to install
    - [dotnet swagger](https://github.com/domaindrivendev/Swashbuckle.AspNetCore)
    - [kiota](https://github.com/microsoft/kiota)

    ## 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`
    11 changes: 11 additions & 0 deletions Service.csproj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    <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>