AutoSDK
The goal of this project is to automate and minimize the effort of maintaining high-quality .NET SDKs generated based on the OpenAPI specification, mainly to strengthen the AI ecosystem within .NET. The code generated by this library is also actively used in dozens of our various SDKs of varying complexity levels and in the LangChain.NET project, which ensures that it is tested in various scenarios and is ready for them.
Inspired by NSwag ❤️.
🔥Features🔥
- Detects your TargetFramework and generates optimal code for it (including net6.0/net7.0/net8.0 improvements)
- Supports .Net Framework/.Net Standard
- Supports OpenAPI 3.1 specs via internal conversion to OpenAPI 3.0 (includes support for nullability, examples, const)
- 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
- Supports SSE (Server-Sent Events) through the
application/x-ndjson
content type - Uses Incremental Source Generators for efficient generation and caching.
- 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 |
|