Back to Fastendpoints

Generated Code

.okf/generated-code.md

8.32.4 KB
Original Source

Generated Code

Roslyn generators (Src/Generator)

GeneratorPurpose
DiscoveredTypesGeneratorEmits discovered endpoint/handler/validator/etc. type lists (DiscoveredTypes) for AOT-friendly AddFastEndpoints
AccessControlGeneratorPermission constants/groups from AccessControl(...) calls; category args resolve as string literals or compile-time string constants (const, nameof, etc.) via semantic model
ReflectionGeneratorReflection cache support for reduced runtime reflection
ServiceRegistrationGeneratorDI registration from attributes
GenericProcessorTypesGeneratorGeneric pre/post processor type materialization

Whitelist for discovery includes IEndpoint, IEventHandler, ICommandHandler, stream command handlers, summaries, job storage providers, processors, command middleware, mappers, FluentValidation validators (DiscoveredTypesGenerator).

MSBuild / CLI generation

  • FastEndpoints.Generator.targets: when GenerateSerializerContexts=true, runs Generator.Cli to emit STJ serializer contexts into SerializerContextOutputPath (default Generated/FastEndpoints).
  • OpenApi targets: ExportOpenApiArtifactsBeforeAotPublish exports .json/.http when ExportOpenApiDocs/ExportHttpFiles set (aliases keep old target names). Single JIT intermediate dir + combined CLI flags when both enabled; app one-call export orchestrator (ExportOpenApiArtifactsAndExitAsync) reads those flags (see NativeAotChecker).
  • Dev mode uses locally built FastEndpoints.Generator.Cli.dll; package mode installs local tool FastEndpoints.Generator.Cli.

Do not hand-edit

  • Compiler-generated outputs under consumer Generated/ folders
  • NativeAotChecker Generated/, wwwroot/openapi/, publish aot/ (gitignored)
  • Packaged analyzer binary contents

Prefer changing generator sources under Src/Generator/*.cs or targets, then rebuild.

Consuming in-repo

Harness example:

xml
<ProjectReference Include="..\..\Src\Generator\FastEndpoints.Generator.csproj"
                  OutputItemType="Analyzer" ReferenceOutputAssembly="false"/>

And AddFastEndpoints(DiscoveredTypes.All) in Program.cs.

Sources

  • Src/Generator/DiscoveredTypesGenerator.cs
  • Src/Generator/FastEndpoints.Generator.csproj
  • Src/Generator/FastEndpoints.Generator.targets
  • TestHarness/Web/Web.csproj
  • TestHarness/NativeAotChecker/NativeAotChecker.csproj