Skip to content

Development Notes

David Kallesen edited this page Dec 22, 2025 · 1 revision

๐Ÿ› ๏ธ 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.


๐Ÿ—๏ธ Project Architecture

๐Ÿ“ Three-Layer Architecture

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

๐Ÿ“ Solution Structure

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 tests

๐Ÿ”„ Generator Architecture

The generators follow the Extractor Pattern:

๐Ÿ“„ OpenAPI Document -> ๐Ÿ”ง Extractors -> ๐Ÿ“‹ Parameter Records -> โš™๏ธ Code Generators -> ๐Ÿ’ป C# Code

๐Ÿ”ง Extractors (All in Atc.Rest.Api.Generator)

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 RoslynSchemaExtractor remains in SourceGenerator (requires Roslyn for assembly scanning).


๐Ÿงช Testing

๐Ÿ“ Test Project Structure

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

โ–ถ๏ธ Running Tests

# ๐Ÿงช 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 detailed

๐Ÿ“ธ Snapshot Testing with Verify

We use Verify for snapshot testing generated code.

๐Ÿ”„ How It Works

  1. ๐Ÿงช Tests generate code using the source generators
  2. ๐Ÿ“Š Output is compared against .verified.cs baseline files
  3. โŒ Differences are shown in .received.cs files
  4. ๐Ÿ‘€ Developer reviews and accepts/rejects changes

๐Ÿ‘€ Review 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"

โž• Adding a New Scenario

  1. ๐Ÿ“ Create folder in test/Scenarios/{ScenarioName}/

  2. ๐Ÿ“„ Add OpenAPI spec: {ScenarioName}.yaml

  3. โš™๏ธ Add marker files:

    .atc-rest-api-server-contracts
    .atc-rest-api-client-contracts
    .atc-rest-api-server-handlers
  4. โ–ถ๏ธ Run tests to generate .received.cs files

  5. โœ… Review and accept:

    dotnet verify accept
  6. ๐Ÿ’พ Commit .verified.cs files to source control


๐Ÿ”จ Build Commands

๐Ÿ—๏ธ Build the Solution

# ๐Ÿ› Debug build
dotnet build

# ๐Ÿ“ฆ Release build (warnings as errors)
dotnet build -c Release

โ–ถ๏ธ Run Sample Projects

# ๐Ÿ• 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.csproj

๐Ÿงน Clean Build

dotnet clean
rm -rf **/obj **/bin
dotnet build

๐Ÿ› Debugging Tips

๐Ÿ‘๏ธ See Generated Files

Add 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)

๐Ÿ” Debug Source Generator

  1. ๐Ÿ›‘ Add Debugger.Launch() in generator:

    public void Initialize(IncrementalGeneratorInitializationContext context)
    {
        #if DEBUG
        if (!Debugger.IsAttached)
        {
            Debugger.Launch();
        }
        #endif
        // ...
    }
  2. ๐Ÿ—๏ธ Build a project that uses the generator

  3. ๐Ÿ”— Visual Studio will prompt to attach debugger


๐Ÿ“‹ Diagnostic Rules Reference

For a complete reference of all diagnostic rules, see Analyzer Rules.

โŒ Common Errors

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

๐Ÿ“ฆ Dependencies

๐Ÿ”ฎ Source Generator Dependencies

<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" />

๐Ÿ”— Project References

๐Ÿ”ฎ Atc.Rest.Api.SourceGenerator
โ”œโ”€โ”€ ๐Ÿ“ฆ Atc.Rest.Api.Generator           # Shared library
โ”œโ”€โ”€ ๐Ÿ“ฆ Atc.NetStandard20                # String utilities
โ”œโ”€โ”€ ๐Ÿ“ฆ Atc.CodeGeneration.CSharp        # Code generation
โ””โ”€โ”€ ๐Ÿ“ฆ Atc.OpenApi                      # OpenAPI extensions

โš ๏ธ Important Version Notes

  • Microsoft.OpenApi v3.0.1: Breaking changes from v1.x
    • Uses IOpenApiSchema interface
    • Pattern match OpenApiSchemaReference vs OpenApiSchema
    • schemaRef.Reference.Id instead of schemaRef.Id
    • JsonSchemaType enum instead of string types

๐Ÿ“ Code Style

๐Ÿ” Analyzers Enforced

<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" />

๐Ÿ“‹ Key Conventions

  • โœ… Use #nullable enable in 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)

๐Ÿค Contributing Workflow

  1. ๐Ÿด Fork the repository
  2. ๐ŸŒฟ Create a branch for your feature
  3. ๐Ÿ’ป Implement following existing patterns
  4. ๐Ÿงช Add tests (validation tests, scenario tests)
  5. ๐Ÿ—๏ธ Run dotnet build -c Release (must pass with 0 warnings)
  6. โœ… Run dotnet test (must pass)
  7. ๐Ÿš€ Submit PR with description of changes

๐Ÿ”— Quick Links

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

โš™๏ธ Features

๐Ÿ“‹ Reference


๐Ÿ”— Resources

Clone this wiki locally