OpenApiGenerator 0.6.8

dotnet add package OpenApiGenerator --version 0.6.8
NuGet\Install-Package OpenApiGenerator -Version 0.6.8
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenApiGenerator" Version="0.6.8">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add OpenApiGenerator --version 0.6.8
#r "nuget: OpenApiGenerator, 0.6.8"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install OpenApiGenerator as a Cake Addin
#addin nuget:?package=OpenApiGenerator&version=0.6.8

// Install OpenApiGenerator as a Cake Tool
#tool nuget:?package=OpenApiGenerator&version=0.6.8

OpenApiGenerator

Allows you to partially (for example, only models) or completely generate a native (without dependencies) C# client sdk according to the OpenAPI specification.
Inspired by NSwag ❤️.

🔥Features🔥

  • Uses Incremental Source Generators for efficient generation and caching.
  • Detects your TargetFramework and generates optimal code for it (including net6.0/net7.0/net8.0 improvements)
  • Supports .Net Framework/.Net Standard
  • Does not contain dependencies for modern versions of dotnet
  • Only System.Text.Json dependency for .Net Framework/.Net Standard
  • Any generated methods provide the ability to pass a CancellationToken
  • Allows partial generation (models only) or end points filtering
  • Available under MIT license for general users and most organizations
  • Uses https://github.com/microsoft/OpenAPI.NET for parsing OpenAPI specification
  • Supports trimming/native AOT compilation

🚀Quick start🚀

  • Install the package
dotnet add package OpenApiGenerator
  • Add the following optional settings to your csproj file to customize generation. You can check all settings here:


<ItemGroup Label="OpenApiGenerator">
    <AdditionalFiles Include="$(MSBuildThisFileDirectory)../../../docs/openapi.yaml" />
</ItemGroup>


<PropertyGroup Label="OpenApiGenerator">
    <OpenApiGenerator_Namespace>Ollama</OpenApiGenerator_Namespace>
    <OpenApiGenerator_ClassName>OllamaApi</OpenApiGenerator_ClassName>

    
    <OpenApiGenerator_GenerateSdk>false</OpenApiGenerator_GenerateSdk>
    <OpenApiGenerator_GenerateModels>true</OpenApiGenerator_GenerateModels>
    <OpenApiGenerator_GenerateMethods>true</OpenApiGenerator_GenerateMethods>
    <OpenApiGenerator_GenerateConstructors>true</OpenApiGenerator_GenerateConstructors>
    <OpenApiGenerator_IncludeOperationIds>getPet;deletePet</OpenApiGenerator_IncludeOperationIds>
    <OpenApiGenerator_ExcludeOperationIds>getPet;deletePet</OpenApiGenerator_ExcludeOperationIds>
    <OpenApiGenerator_IncludeModels>Pet;Model</OpenApiGenerator_IncludeModels>
    <OpenApiGenerator_ExcludeModels>Pet;Model</OpenApiGenerator_ExcludeModels>
</PropertyGroup>
  • It's all! Now you can build your project and use the generated code. You also can use IDE to see the generated code in any moment, this is a example for Rider:
    rider_show_generated_code.png

CLI

You can use the CLI to generate the code.

dotnet tool install --global openapigenerator.cli --prerelease
oag --help
oag generate openapi.yaml

It will generate the code in the "openapi" subdirectory.

Trimming support

Since there are two source generators involved, we will have to create a second project so that the generator for the JsonSerializerContext will “see” our models

  • Create new project for your models. And disable methods/constructors generation:
<PropertyGroup Label="OpenApiGenerator">
    <OpenApiGenerator_GenerateSdk>false</OpenApiGenerator_GenerateSdk>
    <OpenApiGenerator_GenerateModels>true</OpenApiGenerator_GenerateModels>
    <OpenApiGenerator_GenerateSuperTypeForJsonSerializerContext>true</OpenApiGenerator_GenerateSuperTypeForJsonSerializerContext>
</PropertyGroup>
  • Reference this project in your main project.
  • Add SourceGenerationContext.cs file to your main project with the following content:
using System.Text.Json.Serialization;

namespace Namespace;

[JsonSourceGenerationOptions(DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull)]
[JsonSerializable(typeof(OpenApiGeneratorTrimmableSupport))]
internal sealed partial class SourceGenerationContext : JsonSerializerContext;
  • Add the following settings to your main csproj file:
<PropertyGroup Label="OpenApiGenerator">
    <OpenApiGenerator_GenerateSdk>false</OpenApiGenerator_GenerateSdk>
    <OpenApiGenerator_GenerateMethods>true</OpenApiGenerator_GenerateMethods>
    <OpenApiGenerator_GenerateConstructors>true</OpenApiGenerator_GenerateConstructors>
    <OpenApiGenerator_JsonSerializerContext>Namespace.SourceGenerationContext</OpenApiGenerator_JsonSerializerContext>
</PropertyGroup>
  • Add these settings to your new and main csproj file to enable trimming(or use Directory.Build.props file):
<PropertyGroup Label="Trimmable" Condition="$([MSBuild]::IsTargetFrameworkCompatible('$(TargetFramework)', 'net6.0'))">
    <IsAotCompatible>true</IsAotCompatible>
    <EnableTrimAnalyzer>true</EnableTrimAnalyzer>
    <IsTrimmable>true</IsTrimmable>
    <SuppressTrimAnalysisWarnings>false</SuppressTrimAnalysisWarnings>
    <TrimmerSingleWarn>false</TrimmerSingleWarn>
</PropertyGroup>
  • It's all! Now you can build your project and use the generated code with full trimming/nativeAOT support.

📚Examples of use in real SDKs📚

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • No dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.6.8 0 5/21/2024
0.6.7 0 5/21/2024
0.6.6 48 5/19/2024
0.6.5 32 5/19/2024
0.6.4 30 5/19/2024
0.6.3 32 5/19/2024
0.6.2 31 5/19/2024
0.6.1 31 5/19/2024
0.6.0 32 5/19/2024
0.5.0 37 5/19/2024
0.4.0 43 5/19/2024
0.3.2 70 5/13/2024
0.3.1 66 5/13/2024
0.3.0 65 5/13/2024
0.2.0 71 5/11/2024
0.1.5 68 5/10/2024
0.1.4 67 5/10/2024
0.1.3 295 1/8/2024
0.1.2 161 1/8/2024
0.1.1 128 1/8/2024
0.1.0-dev.37 61 1/8/2024
0.1.0-dev.35 60 1/8/2024
0.1.0-dev.34 49 1/8/2024
0.1.0-dev.33 53 1/7/2024
0.1.0-dev.10 111 12/6/2023
0.1.0-dev.5 63 12/4/2023
0.1.0-dev.4 63 11/27/2023
0.1.0-dev.3 54 11/25/2023

⭐ Last 10 features:
- feat: Added MethodNamingConvention support. 2024-05-21
- feat: Added GenerateSuperTypeForJsonSerializerContext. 2024-05-19
- feat: Added trimming support. 2024-05-19
- feat: Now it generates full sdk by default. 2024-05-19
- feat: Added enum support for query/path parameters. 2024-05-19
- feat: Added generation of models from parameters. 2024-05-19
- feat: Added ability to serialize different types of parameters. 2024-05-19
- feat: Added initial support of query parameters. 2024-05-18
- feat: Added path parameters support. 2024-05-17
- feat: Updated OpenAI OpenAPI spec. 2024-05-13
🐞 Last 10 bug fixes:
- fix: Fixed generation for implicit array enums. 2024-05-21
- fix: Fixed issue with incorrect request type names. 2024-05-21
- fix: Fixed .json/url generation. 2024-05-21
- fix: Fixed incorrect requestUri generation. 2024-05-19
- fix: Fixed docs warning. 2024-05-19
- fix: Add default values to parameters. 2024-05-19
- fix: Fixed some docs warnings. 2024-05-19
- fix: ApiClient class now is sealed. 2024-05-19
- fix: Fixed missing docs warning. 2024-05-19
- fix: Fixed GetCSharpType parents Last() calls. 2024-05-17