Skip to content

About

Generate modern C# code from Avro schemas

Resources

Stars

11 stars

Watchers

2 watching

Forks

Repository files navigation

Avro Source Generator

NuGet stable NuGet preview NuGet downloads

CI pipeline Documentation Minimum .NET SDK: 10.0.100 License: MIT

Avro Source Generator

Avro Source Generator generates strongly typed C# models from Avro schemas (.avsc), protocols (.avpr), and IDL files (.avdl) during your normal build.

  • Integrate with Apache.Avro, Chr.Avro, or generate standalone models.
  • Use partial records or classes, nullable properties, and modern C# features.
  • Share named types across inputs and resolve IDL imports.
  • Keep models up to date without a separate code-generation command.

Check out the documentation — setup, configuration, Kafka, and troubleshooting.

Prerequisites

Use .NET SDK 10.0.100 or later. Visual Studio users should use Visual Studio 2026 18.0 or later with .NET 10 support.

The upcoming 0.8.0 preview removes Chr.Avro support. See the migration guide; this removal is not yet included in published packages.

The quick start below uses stable AvroSourceGenerator 0.7.0 and targets net10.0. 0.8.0 preview features, including native C# unions, have separate requirements.

Installation & Basic Usage

From your existing project directory, install the generator and Apache runtime:

dotnet add package AvroSourceGenerator --version 0.7.0
dotnet add package Apache.Avro --version 1.12.2

In your .csproj, update the installed generator reference to a build-only dependency and include your schemas (keep one reference per package):

<ItemGroup>
  <PackageReference Include="AvroSourceGenerator" Version="0.7.0"
                    PrivateAssets="all" ExcludeAssets="runtime" />
  <PackageReference Include="Apache.Avro" Version="1.12.2" />
  <AdditionalFiles Include="schemas/*.avsc" />
</ItemGroup>

Create schemas/user.avsc:

{
  "type": "record",
  "name": "User",
  "namespace": "example",
  "fields": [
    { "name": "Name", "type": "string" },
    { "name": "Email", "type": ["null", "string"], "default": null }
  ]
}

Use the generated model in your application:

using example;

var user = new User
{
    Name = "Ada",
    Email = "ada@example.com",
};

Console.WriteLine($"{user.Name} <{user.Email}>");

Run dotnet build. The generator creates example.User during compilation. The code above prints Ada <ada@example.com>.

You can also use Chr.Avro or standalone models, and include protocols, IDL, and imports.

Example Output

The quick-start model has this public shape (abridged):

namespace example;

public partial record User
{
    public required string Name { get; init; }
    public string? Email { get; init; }
}

The Apache target also generates the ISpecificRecord implementation. Read about generated models and extending partial types.

Documentation

Topic Guide
First generated model Getting started
Input formats, imports, and shared types Schemas and imports
Types, nullability, and partial declarations Generated models
Apache.Avro, Chr.Avro, or no runtime Avro libraries
MSBuild properties and defaults Configuration
Typed producers, consumers, and schema registration Kafka and Schema Registry
Build diagnostics and common fixes Troubleshooting
Preview packages and native unions 0.8.0 preview guide

Command-line tool

An on-demand .NET tool, AvroSourceGenerator.Cli (avro-sg compile), is in development. See the preview CLI guide; the CLI is not yet available in the documented package releases.

Samples

These links target stable examples. Samples in the current main checkout target .NET 11 and use the development SDK selected by global.json. Kafka samples also require Docker. See running repository samples for details.

Extending Generated Types

Add members in your own matching partial record or class; generated enums cannot be extended with partial declarations. See the partial-type example.

Configuration

Set MSBuild properties in your .csproj. The configuration guide documents defaults and examples.

Contributing

Bug reports, feature suggestions, and documentation improvements are welcome through GitHub issues and pull requests.

See repository guidance for build and test commands, package compatibility checks for SDK validation, and documentation maintenance for site preview and publishing instructions.

License

Licensed under the MIT License.

About

Generate modern C# code from Avro schemas

Resources

Stars

11 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages