-
Notifications
You must be signed in to change notification settings - Fork 0
Development Notes
This guide is for developers contributing to the Atc.Rest.Api.SourceGenerator project. It covers the architecture, testing setup, and common development tasks.
The project uses a three-layer architecture that separates shared logic from Roslyn-specific code and CLI:
+------------------------------------------------------------+
| ๐ฎ Atc.Rest.Api.SourceGenerator |
| (Roslyn Source Generators - netstandard2.0) |
| +-------------------+ +-------------------+ +---------+ |
| |ApiServerGenerator| |ApiClientGenerator| |ApiDomain| |
| +--------+----------+ +---------+---------+ +----+----+ |
| | | | |
| +-----------------------+-----------------+ |
| v |
| +----------------------------------------------------------+ |
| | ๐ง Helpers (DiagnosticHelpers - Roslyn-specific) | |
| | ๐ Extractors (RoslynSchemaExtractor - assembly scan)| |
| +----------------------------------------------------------+ |
+-----------------------------+--------------------------------+
| depends on
v
+------------------------------------------------------------+
| ๐ฆ Atc.Rest.Api.Generator |
| (Shared Library - netstandard2.0) |
| +--------------------------------------------------------+ |
| | ๐ง Extractors (ALL 12 - OpenAPI -> Parameters) | |
| | SchemaExtractor, EnumExtractor, HandlerExtractor, | |
| | ResultClassExtractor, EndpointDefinitionExtractor, | |
| | HttpClientExtractor, EndpointPerOperationExtractor... | |
| +--------------------------------------------------------+ |
| +--------------------------------------------------------+ |
| | โ๏ธ Services (CodeGenerationService) | |
| +--------------------------------------------------------+ |
| +---------------+ +--------------+ +------------------+ |
| |โ๏ธConfigurations| | โ
Validators | | ๐ Models | |
| +---------------+ +--------------+ +------------------+ |
+------------------------------------------------------------+
โจ Benefits of this architecture:
- ๐งช Testability: Shared logic tested without Roslyn dependencies
- ๐ Reusability: Generator library powers both Source Generator AND CLI tool
- ๐ ๏ธ Maintainability: Clear separation of concerns
- โก Build performance: Smaller source generator assembly
atc-rest-api-source-generator/
โโโ ๐ src/
โ โโโ ๐ฆ Atc.NetStandard20/ # Core utilities
โ โโโ ๐ฆ Atc.CodeGeneration.CSharp/ # Code gen library
โ โโโ ๐ฆ Atc.OpenApi/ # OpenAPI extensions
โ โโโ ๐ฆ Atc.Rest.Api.Generator/ # Shared library (ALL business logic)
โ โ โโโ ๐ Abstractions/ # ICodeGenerator, IDiagnosticReporter
โ โ โโโ ๐ Configurations/ # ServerConfig, ClientConfig, etc.
โ โ โโโ ๐ Extractors/ # ALL 12 extractors
โ โ โโโ ๐ Services/ # CodeGenerationService
โ โ โโโ ๐ Validators/ # CasingHelper, OpenApiDocumentValidator
โ โ โโโ ๐ Helpers/ # PathSegmentHelper, RuleIdentifiers
โ โโโ ๐ฎ Atc.Rest.Api.SourceGenerator/ # Roslyn generators (thin wrappers)
โ โโโ ๐ป Atc.Rest.Api.CliGenerator/ # CLI tool (net10.0)
โโโ ๐ sample/
โ โโโ ๐ Minimal/
โ โโโ ๐ PetStoreSimple/ # Basic 3-endpoint example
โ โโโ ๐ PetStoreFull/ # Extended example
โ โโโ ๐ช Showcase/ # Full-featured demo
โโโ ๐ test/
โโโ ๐งช Atc.Rest.Api.Generator.Tests/ # Shared library unit tests
โโโ ๐งช Atc.Rest.Api.Generator.IntegrationTests/# Generator integration tests
โโโ ๐งช Atc.Rest.Api.SourceGenerator.Tests/ # SourceGenerator tests
โโโ ๐งช Atc.Rest.Api.CliGenerator.Tests/ # CLI command testsThe generators follow the Extractor Pattern:
๐ OpenAPI Document -> ๐ง Extractors -> ๐ Parameter Records -> โ๏ธ Code Generators -> ๐ป C# Code| Extractor | Input | Output |
|---|---|---|
SchemaExtractor |
OpenAPI schemas | RecordsParameters |
EnumExtractor |
OpenAPI string enums | EnumParameters |
HandlerExtractor |
OpenAPI operations | InterfaceParameters |
OperationParameterExtractor |
Operation params | ClassParameters |
ResultClassExtractor |
Operation responses | Result class code |
EndpointDefinitionExtractor |
Operations | EndpointDefinitionParameters |
HandlerScaffoldExtractor |
Operations | Handler stub code |
HttpClientExtractor |
Operations | TypedClient code |
EndpointPerOperationExtractor |
Operations | Per-operation endpoint classes |
๐ก Note: Only
RoslynSchemaExtractorremains in SourceGenerator (requires Roslyn for assembly scanning).
test/
โโโ ๐งช Atc.Rest.Api.Generator.Tests/ # Shared library unit tests (198 tests)
โโโ ๐งช Atc.Rest.Api.Generator.IntegrationTests/ # Generator integration tests (52 tests)
โโโ ๐งช Atc.Rest.Api.SourceGenerator.Tests/ # SourceGenerator tests (242 tests)
โโโ ๐งช Atc.Rest.Api.CliGenerator.Tests/ # CLI command tests (16 tests)
โโโ ๐ Scenarios/ # Shared test scenarios
โโโ ๐ช Demo/ # Comprehensive demo scenario
โโโ ๐ PetStoreSimple/ # Minimal 3-endpoint API
โโโ ๐ฆ RateLimit/ # x-ratelimit-* extensions
โโโ ๐ Retry/ # x-retry-* resilience extensions
โโโ ๐ Security*/ # Security scheme variations
โโโ ๐ข Versioning*/ # API versioning strategies# ๐งช Run all tests
dotnet test
# ๐ฏ Run specific test category
dotnet test --filter "FullyQualifiedName~GeneratorComparisonTests"
# ๐ Run tests for specific scenario
dotnet test --filter "DisplayName~PetStoreSimple"
# โ
Run validation tests only
dotnet test --filter "FullyQualifiedName~ValidationTests"
# ๐ Run with verbose output
dotnet test -v detailedWe use Verify for snapshot testing generated code.
- ๐งช Tests generate code using the source generators
- ๐ Output is compared against
.verified.csbaseline files - โ Differences are shown in
.received.csfiles - ๐ Developer reviews and accepts/rejects changes
When tests fail due to generated code changes:
# ๐ Review all pending changes
dotnet verify review
# โ
Accept all changes (update baselines)
dotnet verify accept
# ๐ Accept specific file
dotnet verify accept --file "path/to/test.cs"-
๐ Create folder in
test/Scenarios/{ScenarioName}/ -
๐ Add OpenAPI spec:
{ScenarioName}.yaml -
โ๏ธ Add marker files:
.atc-rest-api-server-contracts .atc-rest-api-client-contracts .atc-rest-api-server-handlers
-
โถ๏ธ Run tests to generate.received.csfiles -
โ Review and accept:
dotnet verify accept
-
๐พ Commit
.verified.csfiles to source control
# ๐ Debug build
dotnet build
# ๐ฆ Release build (warnings as errors)
dotnet build -c Release# ๐ PetStoreSimple
dotnet run --project sample/PetStoreSimple/PetStoreSimple.Api/PetStoreSimple.Api.csproj
# ๐ PetStoreSimple with Aspire
dotnet run --project sample/PetStoreSimple/PetStoreSimple.Aspire/PetStoreSimple.Aspire.csproj
# ๐ช Showcase with Aspire
dotnet run --project sample/Showcase/Showcase.Aspire/Showcase.Aspire.csprojdotnet clean
rm -rf **/obj **/bin
dotnet buildAdd to .csproj:
<PropertyGroup>
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
</PropertyGroup>Generated files appear in:
obj/Generated/Atc.Rest.Api.SourceGenerator/
โโโ ๐ ApiServerGenerator/
โ โโโ *.g.cs
โโโ ๐ ApiClientGenerator/
โ โโโ *.g.cs
โโโ ๐ ApiServerDomainGenerator/
โโโ *.cs (physical files)-
๐ Add
Debugger.Launch()in generator:public void Initialize(IncrementalGeneratorInitializationContext context) { #if DEBUG if (!Debugger.IsAttached) { Debugger.Launch(); } #endif // ... }
-
๐๏ธ Build a project that uses the generator
-
๐ Visual Studio will prompt to attach debugger
For a complete reference of all diagnostic rules, see Analyzer Rules.
| Rule ID | Description | Solution |
|---|---|---|
| ATC_API_DEP001 | Server requires ASP.NET Core | Add <FrameworkReference Include="Microsoft.AspNetCore.App" />
|
| ATC_API_DEP003 | EndpointPerOperation requires Atc.Rest.Client | Install Atc.Rest.Client NuGet package |
| ATC_API_DEP007 | useMinimalApiPackage requires package | Install Atc.Rest.MinimalApi or set to "disabled"
|
| ATC_API_OPR001 | Missing operationId | Add operationId to each operation in OpenAPI spec |
| ATC_API_VAL002 | OpenAPI 2.0 not supported | Upgrade to OpenAPI 3.0.x specification |
<PackageReference Include="Microsoft.OpenApi.YamlReader" Version="3.0.1" />
<PackageReference Include="Microsoft.OpenApi" Version="3.0.1" />
<PackageReference Include="SharpYaml" Version="2.1.4" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.14.0" />
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.11.0" />๐ฎ Atc.Rest.Api.SourceGenerator
โโโ ๐ฆ Atc.Rest.Api.Generator # Shared library
โโโ ๐ฆ Atc.NetStandard20 # String utilities
โโโ ๐ฆ Atc.CodeGeneration.CSharp # Code generation
โโโ ๐ฆ Atc.OpenApi # OpenAPI extensions-
Microsoft.OpenApi v3.0.1: Breaking changes from v1.x
- Uses
IOpenApiSchemainterface - Pattern match
OpenApiSchemaReferencevsOpenApiSchema -
schemaRef.Reference.Idinstead ofschemaRef.Id -
JsonSchemaTypeenum instead of string types
- Uses
<PackageReference Include="StyleCop.Analyzers" Version="1.2.0-beta.507" />
<PackageReference Include="Meziantou.Analyzer" Version="2.0.256" />
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.15.0.120848" />
<PackageReference Include="AsyncFixer" Version="1.6.0" />
<PackageReference Include="SecurityCodeScan.VS2019" Version="5.6.7" />- โ
Use
#nullable enablein all generated code - ๐ Use records for models, classes for services
- โก๏ธ Prefer expression-bodied members
- ๐ XML documentation on public APIs
- ๐ค camelCase for private fields (no underscore prefix)
- ๐ด Fork the repository
- ๐ฟ Create a branch for your feature
- ๐ป Implement following existing patterns
- ๐งช Add tests (validation tests, scenario tests)
- ๐๏ธ Run
dotnet build -c Release(must pass with 0 warnings) - โ
Run
dotnet test(must pass) - ๐ Submit PR with description of changes
| Resource | Path |
|---|---|
| ๐ฆ Shared Library | src/Atc.Rest.Api.Generator/ |
| ๐ฎ Source Generators | src/Atc.Rest.Api.SourceGenerator/ |
| ๐ง Extractors (ALL 12) | src/Atc.Rest.Api.Generator/Extractors/ |
| โ๏ธ Services | src/Atc.Rest.Api.Generator/Services/ |
| โ Validators | src/Atc.Rest.Api.Generator/Validators/ |
| โ๏ธ Configurations | src/Atc.Rest.Api.Generator/Configurations/ |
| ๐ OpenAPI Extensions | src/Atc.OpenApi/Extensions/ |
| ๐งช Test Scenarios | test/Scenarios/ |
| ๐ Samples | sample/ |
๐ Home
- ๐ Getting Started with Basic
- ๐ ๏ธ Getting Started with CLI
- ๐ Working with OpenAPI
- ๐ ๏ธ Working with CLI
- ๐ Working with Security
- ๐ฆ Working with Rate Limiting
- ๐ Working with Resilience
- ๐๏ธ Working with Caching
- ๐ข Working with Versioning
- โ Working with Validations
- ๐ Analyzer Rules
- ๐บ๏ธ Roadmap
- ๐ช Showcase Demo
- ๐ง Development Notes
- ๐ฆ GitHub Repository
- ๐ฅ NuGet Package
- ๐ Report Issues