AutoSDK
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.
1 2 3 4 5 6 7 |
|
It also will include polyfills for .Net Framework/.Net Standard TargetFrameworks.
Source generator
- Install the package
1
dotnet add package AutoSDK.SourceGenerators
- Add the following optional settings to your csproj file to customize generation. You can check all settings here:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<!-- This generator automatically detects all .yaml files in the project directory and adds them to the generation --> <!-- If your yaml file is not in the project directory, you can specify it manually --> <ItemGroup Label="AutoSDK"> <AdditionalFiles Include="$(MSBuildThisFileDirectory)../../../docs/openapi.yaml" /> </ItemGroup> <!-- All settings optional --> <PropertyGroup Label="AutoSDK"> <AutoSDK_Namespace>Ollama</AutoSDK_Namespace> <AutoSDK_ClassName>OllamaApi</AutoSDK_ClassName> <!-- By default, it generates all models/methods. You can disable this behavior using these properties --> <AutoSDK_GenerateSdk>false</AutoSDK_GenerateSdk> <AutoSDK_GenerateModels>true</AutoSDK_GenerateModels> <AutoSDK_GenerateMethods>true</AutoSDK_GenerateMethods> <AutoSDK_GenerateConstructors>true</AutoSDK_GenerateConstructors> <AutoSDK_IncludeOperationIds>getPet;deletePet</AutoSDK_IncludeOperationIds> <AutoSDK_ExcludeOperationIds>getPet;deletePet</AutoSDK_ExcludeOperationIds> <AutoSDK_IncludeModels>Pet;Model</AutoSDK_IncludeModels> <AutoSDK_ExcludeModels>Pet;Model</AutoSDK_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:
1 2 3 4 5 |
|
SourceGenerationContext.cs
file to your main project with the following content:
1 2 3 4 5 6 7 |
|
1 2 3 4 5 6 |
|
1 2 3 4 5 6 7 |
|