OpenApiGenerator.Cli
0.15.2
See the version list below for details.
dotnet tool install --global OpenApiGenerator.Cli --version 0.15.2
dotnet new tool-manifest # if you are setting up this repo dotnet tool install --local OpenApiGenerator.Cli --version 0.15.2
#tool dotnet:?package=OpenApiGenerator.Cli&version=0.15.2
nuke :add-package OpenApiGenerator.Cli --version 0.15.2
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🚀
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.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📚
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
This package has no dependencies.
Version | Downloads | Last updated | |
---|---|---|---|
0.22.6-dev.7 | 1,542 | 9/9/2024 | |
0.22.6-dev.6 | 53 | 9/9/2024 | |
0.22.6-dev.4 | 221 | 9/7/2024 | |
0.22.6-dev.2 | 83 | 9/7/2024 | |
0.22.6-dev.1 | 52 | 9/7/2024 | |
0.22.5 | 151 | 9/7/2024 | |
0.22.4 | 104 | 9/7/2024 | |
0.22.3 | 102 | 9/7/2024 | |
0.22.2 | 119 | 9/7/2024 | |
0.22.1 | 106 | 9/7/2024 | |
0.22.0 | 101 | 9/7/2024 | |
0.21.1 | 164 | 9/6/2024 | |
0.21.0 | 108 | 9/6/2024 | |
0.20.2 | 736 | 8/31/2024 | |
0.20.1 | 112 | 8/31/2024 | |
0.20.0 | 119 | 8/30/2024 | |
0.19.0 | 127 | 8/30/2024 | |
0.18.0 | 208 | 8/29/2024 | |
0.17.1 | 135 | 8/28/2024 | |
0.17.0 | 114 | 8/28/2024 | |
0.16.4-dev.7 | 58 | 8/28/2024 | |
0.16.3 | 470 | 8/24/2024 | |
0.16.2 | 135 | 8/24/2024 | |
0.16.1 | 209 | 8/23/2024 | |
0.16.0 | 181 | 8/22/2024 | |
0.15.8 | 136 | 8/22/2024 | |
0.15.7 | 261 | 8/19/2024 | |
0.15.6 | 176 | 8/18/2024 | |
0.15.5 | 124 | 8/18/2024 | |
0.15.4 | 247 | 8/15/2024 | |
0.15.3 | 186 | 8/13/2024 | |
0.15.2 | 160 | 8/13/2024 | |
0.15.1 | 144 | 8/12/2024 | |
0.15.0 | 154 | 8/11/2024 | |
0.14.3 | 283 | 8/6/2024 | |
0.14.2 | 91 | 8/5/2024 | |
0.14.1 | 81 | 8/5/2024 | |
0.14.0 | 71 | 8/5/2024 | |
0.13.11 | 391 | 7/22/2024 | |
0.13.10 | 134 | 7/21/2024 | |
0.13.9 | 135 | 7/18/2024 | |
0.13.8 | 122 | 7/15/2024 | |
0.13.7 | 105 | 7/15/2024 | |
0.13.6 | 112 | 7/14/2024 | |
0.13.5 | 110 | 7/14/2024 | |
0.13.4 | 111 | 7/13/2024 | |
0.13.3 | 125 | 7/9/2024 | |
0.13.2 | 89 | 7/8/2024 | |
0.13.1 | 104 | 7/8/2024 | |
0.13.0 | 113 | 7/8/2024 | |
0.12.2 | 115 | 7/8/2024 | |
0.12.1 | 130 | 7/6/2024 | |
0.12.0 | 122 | 7/6/2024 | |
0.11.11 | 113 | 7/6/2024 | |
0.11.10 | 112 | 7/6/2024 | |
0.11.9 | 144 | 6/30/2024 | |
0.11.8 | 131 | 6/30/2024 | |
0.11.7 | 121 | 6/30/2024 | |
0.11.6 | 125 | 6/29/2024 | |
0.11.4 | 118 | 6/27/2024 | |
0.11.3 | 138 | 6/27/2024 | |
0.11.1 | 114 | 6/20/2024 | |
0.11.0 | 123 | 6/16/2024 | |
0.10.8 | 123 | 6/15/2024 | |
0.10.7 | 121 | 6/15/2024 | |
0.10.6 | 115 | 6/15/2024 | |
0.10.4 | 116 | 6/13/2024 | |
0.10.2 | 113 | 6/13/2024 | |
0.10.1 | 107 | 6/13/2024 | |
0.10.0 | 112 | 6/13/2024 | |
0.9.12 | 112 | 6/6/2024 | |
0.9.11 | 113 | 6/6/2024 | |
0.9.10 | 117 | 6/5/2024 | |
0.9.9 | 114 | 6/5/2024 | |
0.9.8 | 127 | 6/4/2024 | |
0.9.7 | 141 | 6/1/2024 | |
0.9.5 | 122 | 5/31/2024 | |
0.9.4 | 117 | 5/31/2024 | |
0.9.3 | 125 | 5/31/2024 | |
0.9.1 | 121 | 5/30/2024 | |
0.9.0 | 117 | 5/30/2024 | |
0.8.8 | 123 | 5/28/2024 | |
0.8.6 | 117 | 5/27/2024 | |
0.8.5 | 115 | 5/27/2024 | |
0.8.4 | 132 | 5/27/2024 | |
0.8.3 | 133 | 5/27/2024 | |
0.8.1 | 136 | 5/25/2024 | |
0.8.0 | 127 | 5/25/2024 | |
0.7.2 | 125 | 5/23/2024 | |
0.7.1 | 130 | 5/22/2024 | |
0.7.0 | 143 | 5/22/2024 | |
0.6.9 | 122 | 5/22/2024 | |
0.6.8 | 125 | 5/21/2024 | |
0.6.7 | 109 | 5/21/2024 | |
0.6.6 | 121 | 5/19/2024 | |
0.6.5 | 115 | 5/19/2024 | |
0.6.4 | 129 | 5/19/2024 | |
0.6.3 | 112 | 5/19/2024 | |
0.6.2 | 116 | 5/19/2024 | |
0.6.1 | 112 | 5/19/2024 | |
0.6.0 | 113 | 5/19/2024 | |
0.5.0 | 122 | 5/19/2024 | |
0.4.0 | 124 | 5/19/2024 | |
0.3.2 | 111 | 5/13/2024 | |
0.3.1 | 103 | 5/13/2024 | |
0.3.0 | 99 | 5/13/2024 | |
0.2.0 | 106 | 5/11/2024 |
⭐ Last 10 features:
- 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
- feat: Initial MultiPart form data support. 2024-08-05
- feat: Added filenames for string/binary schemas. 2024-08-05
- feat: Added ExcludeDeprecatedOperations. 2024-08-05
- feat: Added MultipartFormData support. 2024-08-05
- feat: Added CLS Compliance support. 2024-07-19
- feat: Added xml docs for params. 2024-07-14
🐞 Last 10 bug fixes:
- 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
- fix: Fixed small issue with GitHub spec. 2024-08-11
- fix: Fixed some issues with dots in names. 2024-08-07
- fix: Fixed issue with Array MultiPart form data parameters. 2024-08-06
- fix: Don't include optional parameters with default value to multipart form data. 2024-08-05
- fix: Fixed some small Multipart form data serialization issues. 2024-08-05