Src/Generator.Cli/README.md
A .NET CLI tool for generating JSON serializer contexts for FastEndpoints applications. This tool analyzes your FastEndpoints projects and generates optimized System.Text.Json serializer contexts for improved performance through source generation.
dotnet tool install --global FastEndpoints.Generator.Cli
dotnet tool install --global FastEndpoints.Generator.Cli --version 8.0.0
dotnet tool update --global FastEndpoints.Generator.Cli
Generate serializer contexts for a project:
fastendpoints-generator MyProject.csproj
Specify a custom output directory for generated files:
fastendpoints-generator MyProject.csproj --output Generated/FastEndpoints
Force regeneration even if files are up to date:
fastendpoints-generator MyProject.csproj --force
fastendpoints-generator <project-file-path> [--force] [--output <path>]
Options:
--force Force regeneration even if files are up to date
--output <path> Custom output path for generated files
The tool analyzes your FastEndpoints classes and generates:
SerializerContexts.g.cs - A partial JsonSerializerContext class with [JsonSerializable] attributes for all types used in your endpoints (requests, responses, and their dependencies).
SerializerContextExtensions.g.cs - Extension methods for JsonSerializerOptions to easily register the generated context.
After running the tool, you'll see output like:
Analyzing project: MyProject
Found 42 source files.
Found 15 type declarations.
Found 8 endpoints with 12 serializable types.
Found endpoint: MyProject.Features.Users.GetUserEndpoint
Found endpoint: MyProject.Features.Users.CreateUserEndpoint
...
Generated files:
Generated/FastEndpoints/SerializerContexts.g.cs
Generated/FastEndpoints/SerializerContextExtensions.g.cs
The FastEndpoints.Generator package includes MSBuild targets that automatically install and run this tool during build. To enable automatic generation:
FastEndpoints.Generator package to your project:dotnet add package FastEndpoints.Generator
.csproj:<PropertyGroup>
<GenerateSerializerContexts>true</GenerateSerializerContexts>
</PropertyGroup>
<PropertyGroup>
<SerializerContextOutputPath>Generated/FastEndpoints</SerializerContextOutputPath>
</PropertyGroup>
The tool will be automatically installed/updated on the first build and will generate the serializer contexts before compilation.
In your application startup, use the extension method to register the serializer context. The method returns the same JsonSerializerOptions instance so calls can be chained across multiple projects:
using System.Text.Json;
var options = new JsonSerializerOptions
{
// Your options
}.AddSerializerContextsFromMyProject(); // Extension method generated by the tool
// Use with FastEndpoints
app.UseFastEndpoints(c =>
{
c.Serializer.Options.AddSerializerContextsFromMyProject(); // Extension method generated by the tool
});
// Chain contexts from referenced projects
options.AddSerializerContextsFromFeatureA()
.AddSerializerContextsFromMyProject();
Or manually register the context:
var context = new GeneratedSerializerContext(new JsonSerializerOptions());
options.TypeInfoResolverChain.Insert(0, context);
Endpoint Discovery: The tool scans your project for classes that inherit from FastEndpoints base classes (Endpoint, EndpointWithoutRequest, EndpointWithMapper, etc.).
Type Analysis: For each endpoint, it analyzes the generic type arguments to identify request and response types.
Dependency Resolution: It recursively analyzes the properties of each type to find all types that need serialization.
Smart Filtering: Built-in filtering excludes system types, framework types, and types that don't need serialization.
Incremental Generation: The tool uses a cache file (.fastendpoints-generator-cache) to avoid unnecessary regeneration when source files haven't changed.
The tool automatically excludes:
If you get "command not found" after installation, ensure the global tools directory is in your PATH:
# Windows (PowerShell)
$env:PATH += ";$env:USERPROFILE\.dotnet\tools"
# Linux/macOS
export PATH="$PATH:$HOME/.dotnet/tools"
--force to bypass the cache and force regeneration