OpenApiGenerator 0.16.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package OpenApiGenerator --version 0.16.1                
NuGet\Install-Package OpenApiGenerator -Version 0.16.1                
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.16.1">
  <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.16.1                
#r "nuget: OpenApiGenerator, 0.16.1"                
#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.16.1

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

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 nullable enable/trimming/native AOT compilation/CLS compliance
  • Tested on GitHub 220k lines OpenAPI specification
  • Supports OneOf/AnyOf/AllOf/Not schemas
  • Supports Enums for System.Text.Json
  • Efficient O(n) implementation, fully suitable for large/super large OpenAPI specifications

🚀Quick start🚀

You can use the CLI to generate the code.

dotnet tool install --global openapigenerator.cli --prerelease
rm -rf Generated
oag generate openapi.yaml \
    --namespace Namespace \
    --clientClassName YourApi \
    --targetFramework net8.0 \
    --output Generated

It will generate the code in the "Generated" subdirectory.
It also will include polyfills for .Net Framework/.Net Standard TargetFrameworks.

Source generator

  • 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

Trimming support

CLI

CLI generates Trimming/NativeAOT compatible code by default.

Source generator

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_GenerateJsonSerializerContextTypes>true</OpenApiGenerator_GenerateJsonSerializerContextTypes>
</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.20.2 25 8/31/2024
0.20.1 24 8/31/2024
0.20.0 26 8/30/2024
0.19.0 28 8/30/2024
0.18.0 27 8/29/2024
0.17.1 29 8/28/2024
0.17.0 38 8/28/2024
0.16.4-dev.7 30 8/28/2024
0.16.3 82 8/24/2024
0.16.2 58 8/24/2024
0.16.1 73 8/23/2024
0.16.0 64 8/22/2024
0.15.8 68 8/22/2024
0.15.7 73 8/19/2024
0.15.6 69 8/18/2024
0.15.5 64 8/18/2024
0.15.4 65 8/15/2024
0.15.3 69 8/13/2024
0.15.2 72 8/13/2024
0.15.1 58 8/12/2024
0.15.0 60 8/11/2024
0.14.3 55 8/6/2024
0.14.2 51 8/5/2024
0.14.1 55 8/5/2024
0.14.0 49 8/5/2024
0.13.11 251 7/22/2024
0.13.10 98 7/21/2024
0.13.9 70 7/18/2024
0.13.8 65 7/15/2024
0.13.7 59 7/15/2024
0.13.6 72 7/14/2024
0.13.5 61 7/14/2024
0.13.4 67 7/13/2024
0.13.3 54 7/9/2024
0.13.2 58 7/8/2024
0.13.1 68 7/8/2024
0.13.0 54 7/8/2024
0.12.2 58 7/8/2024
0.12.1 73 7/6/2024
0.12.0 60 7/6/2024
0.11.11 73 7/6/2024
0.11.10 60 7/6/2024
0.11.9 113 6/30/2024
0.11.8 79 6/30/2024
0.11.7 77 6/30/2024
0.11.6 91 6/29/2024
0.11.4 77 6/27/2024
0.11.3 78 6/27/2024
0.11.1 102 6/20/2024
0.11.0 99 6/16/2024
0.10.8 78 6/15/2024
0.10.7 76 6/15/2024
0.10.6 71 6/15/2024
0.10.4 69 6/13/2024
0.10.2 65 6/13/2024
0.10.1 62 6/13/2024
0.10.0 68 6/13/2024
0.9.12 96 6/6/2024
0.9.11 69 6/6/2024
0.9.10 80 6/5/2024
0.9.9 80 6/5/2024
0.9.8 71 6/4/2024
0.9.7 90 6/1/2024
0.9.5 83 5/31/2024
0.9.4 76 5/31/2024
0.9.3 78 5/31/2024
0.9.1 79 5/30/2024
0.9.0 74 5/30/2024
0.8.8 70 5/28/2024
0.8.6 80 5/27/2024
0.8.5 72 5/27/2024
0.8.4 73 5/27/2024
0.8.3 75 5/27/2024
0.8.1 82 5/25/2024
0.8.0 74 5/25/2024
0.7.2 78 5/23/2024
0.7.1 81 5/22/2024
0.7.0 75 5/22/2024
0.6.9 75 5/22/2024
0.6.8 76 5/21/2024
0.6.7 76 5/21/2024
0.6.6 93 5/19/2024
0.6.5 73 5/19/2024
0.6.4 71 5/19/2024
0.6.3 73 5/19/2024
0.6.2 72 5/19/2024
0.6.1 72 5/19/2024
0.6.0 73 5/19/2024
0.5.0 78 5/19/2024
0.4.0 85 5/19/2024
0.3.2 81 5/13/2024
0.3.1 86 5/13/2024
0.3.0 76 5/13/2024
0.2.0 79 5/11/2024
0.1.5 75 5/10/2024
0.1.4 74 5/10/2024
0.1.3 302 1/8/2024
0.1.2 166 1/8/2024
0.1.1 133 1/8/2024
0.1.0-dev.37 62 1/8/2024
0.1.0-dev.35 64 1/8/2024
0.1.0-dev.34 52 1/8/2024
0.1.0-dev.33 56 1/7/2024
0.1.0-dev.10 112 12/6/2023
0.1.0-dev.5 64 12/4/2023
0.1.0-dev.4 66 11/27/2023
0.1.0-dev.3 57 11/25/2023

⭐ Last 10 features:
- feat: To MinVer. 2024-08-22
- feat: Make SourceGenerationContext public. 2024-08-19
- feat: Removed Required from enum properties with single value. 2024-08-18
- feat: Updated version. 2024-08-18
- feat: Now required enum properties with single value will have default value. 2024-08-18
- feat: Disable AnyOf validation by default. 2024-08-15
- feat: Added Enum postfix instead VariantX for some enums inside AnyOf/OneOf/AllOf. 2024-08-13
- feat: CRITICAL UPDATE - change type system. 2024-08-10
- feat: Some upgrade for SchemaContext way. 2024-08-07
- feat: Mark generated structs as partial. 2024-08-06
🐞 Last 10 bug fixes:
- fix: Fixed issue with some default values for LangSmith. 2024-08-23
- fix: Removed langsmith.json. 2024-08-23
- fix: Fixed issue with trimming for LangSmith. 2024-08-22
- fix: Fixed missing PrivateAssets in generator. 2024-08-19
- fix: Fixed small issue with xml docs params with @. 2024-08-13
- feat: Added Enum postfix instead VariantX for some enums inside AnyOf/OneOf/AllOf. 2024-08-13
- fix: Fixed some nullability issues. 2024-08-12
- fix: Fixed nullability issues. 2024-08-12
- fix: Fixed naming issues. 2024-08-12
- fix: Fixed all compilation issue with tests. 2024-08-11