Source Generators
How AutoSDK uses Roslyn incremental source generators for compile-time code generation.
AutoSDK uses Roslyn incremental source generators to generate SDK code at compile time — no runtime reflection, no external build tools.
How It Works
When you add the AutoSDK NuGet package to your project:
- MSBuild passes your OpenAPI spec as an
AdditionalFileto the compiler - The
SdkGenerator(anIIncrementalGenerator) reads the spec - It runs the generation pipeline and emits C# source code
- The compiler includes the generated code in your compilation
<!-- Your .csproj -->
<ItemGroup>
<PackageReference Include="AutoSDK" Version="*-*" PrivateAssets="all" />
<AdditionalFiles Include="openapi.yaml" />
</ItemGroup>Incremental Generation
AutoSDK's generator is incremental — it only regenerates when the input spec changes. This makes builds fast even for large specs:
- The generator caches the parsed spec and type graph
- Only schema changes trigger regeneration
- IDE (VS, Rider) gets IntelliSense for generated types in real-time
Generator Architecture
AutoSDK includes two generators:
| Generator | Purpose |
|---|---|
SdkGenerator | Main generator — produces client, models, serialization |
CliGenerator | Optional — generates a CLI wrapper for the SDK (when AutoSDK_GenerateCli=true) |
Both are implemented as IIncrementalGenerator for optimal performance.
Source Generator vs CLI
The source generator approach has trade-offs compared to the CLI:
Source Generator Advantages
- Zero build step — Just add a NuGet package
- IDE integration — IntelliSense, go-to-definition, refactoring
- Always in sync — Regenerates on every build when spec changes
CLI Advantages
- Simpler trimming — No two-project pattern needed
- Inspectable output — Generated files are on disk, easy to review
- CI flexibility — Run in any environment with the CLI installed
- Spec patching —
generate.shcan preprocess specs withjq/yq
Recommendation
Use the CLI for most production SDKs. It's simpler, more flexible, and produces inspectable output. The source generator is best for rapid prototyping or when you want zero configuration.
Last updated on