OpenApiGenerator 0.22.6-dev.7
dotnet add package OpenApiGenerator --version 0.22.6-dev.7
NuGet\Install-Package OpenApiGenerator -Version 0.22.6-dev.7
<PackageReference Include="OpenApiGenerator" Version="0.22.6-dev.7"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
<PackageVersion Include="OpenApiGenerator" Version="0.22.6-dev.7" />
<PackageReference Include="OpenApiGenerator"> <PrivateAssets>all</PrivateAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets> </PackageReference>
paket add OpenApiGenerator --version 0.22.6-dev.7
#r "nuget: OpenApiGenerator, 0.22.6-dev.7"
#:package OpenApiGenerator@0.22.6-dev.7
#addin nuget:?package=OpenApiGenerator&version=0.22.6-dev.7&prerelease
#tool nuget:?package=OpenApiGenerator&version=0.22.6-dev.7&prerelease
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
- Used in 10+ real SDKs and adapted to solve various problems
🚀Quick start🚀
CLI (Recommended)
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:
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.csfile 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📚
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.22.6-dev.7 | 285 | 9/9/2024 | |
| 0.22.6-dev.6 | 111 | 9/9/2024 | |
| 0.22.6-dev.4 | 113 | 9/7/2024 | |
| 0.22.6-dev.2 | 110 | 9/7/2024 | |
| 0.22.6-dev.1 | 116 | 9/7/2024 | |
| 0.22.5 | 997 | 9/7/2024 | |
| 0.22.4 | 234 | 9/7/2024 | |
| 0.22.3 | 235 | 9/7/2024 | |
| 0.22.2 | 219 | 9/7/2024 | |
| 0.22.1 | 223 | 9/7/2024 | |
| 0.22.0 | 233 | 9/7/2024 | |
| 0.21.1 | 227 | 9/6/2024 | |
| 0.21.0 | 209 | 9/6/2024 | |
| 0.20.2 | 221 | 8/31/2024 | |
| 0.20.1 | 210 | 8/31/2024 | |
| 0.20.0 | 209 | 8/30/2024 | |
| 0.19.0 | 210 | 8/30/2024 | |
| 0.18.0 | 211 | 8/29/2024 | |
| 0.17.1 | 198 | 8/28/2024 | |
| 0.17.0 | 227 | 8/28/2024 |
⭐ Last 10 features:
- feat: Updated MinVer/Microsoft.OpenAPI 2024-09-07
- feat: Added OpenApiDocument Simplify extension. 2024-09-06
- feat: Added xml doc for Named AnyOfs. 2024-09-06
- feat: Added Unix timestamp detection. 2024-08-30
- feat: Added partial methods for constructors te set up/validate things. 2024-08-29
- feat: Added OperationContext. 2024-08-28
- feat: Added generation of main constructor with apiKey/username. 2024-08-24
- 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
🐞 Last 10 bug fixes:
- fix: Fixed issues after update NuGet packages. 2024-09-08
- fix: Fixed GenerateDocs in init command. 2024-09-07
- fix: Fixed small issues with cli init command. 2024-09-07
- fix: Fixed init command issue with binary files. 2024-09-07
- fix: Fixed init command cicd files path. 2024-09-07
- fix: Fixed init command solution tests. 2024-09-07
- fix: Fixed init command directory creation. 2024-09-07
- fix: Added missing .gitignore for init command. 2024-09-07
- fix: Fixed init command separator. 2024-09-07
- fix: Fixed type detection for assemblyAI spec. 2024-09-06