AutoSDK
Concepts

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:

  1. MSBuild passes your OpenAPI spec as an AdditionalFile to the compiler
  2. The SdkGenerator (an IIncrementalGenerator) reads the spec
  3. It runs the generation pipeline and emits C# source code
  4. 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:

GeneratorPurpose
SdkGeneratorMain generator — produces client, models, serialization
CliGeneratorOptional — 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 patchinggenerate.sh can preprocess specs with jq/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.

Edit on GitHub

Last updated on

On this page