OpenApiSourceGenerator 0.3.0-beta

This is a prerelease version of OpenApiSourceGenerator.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package OpenApiSourceGenerator --version 0.3.0-beta
                    
NuGet\Install-Package OpenApiSourceGenerator -Version 0.3.0-beta
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="OpenApiSourceGenerator" Version="0.3.0-beta">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="OpenApiSourceGenerator" Version="0.3.0-beta" />
                    
Directory.Packages.props
<PackageReference Include="OpenApiSourceGenerator">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add OpenApiSourceGenerator --version 0.3.0-beta
                    
#r "nuget: OpenApiSourceGenerator, 0.3.0-beta"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package OpenApiSourceGenerator@0.3.0-beta
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=OpenApiSourceGenerator&version=0.3.0-beta&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=OpenApiSourceGenerator&version=0.3.0-beta&prerelease
                    
Install as a Cake Tool

CI codecov

OpenApiSourceGenerator

A straightforward C# source generator that generates strongly-typed C# classes from OpenAPI specifications (OpenAPI 3.x) at compile time. No additional build steps or tools required!

Features

  • ✨ Compile-time code generation - Classes are generated automagically during build
  • 🚀 Zero runtime overhead - All code generation happens at compile time
  • 📝 OpenAPI 3.x support - Reads YAML and JSON OpenAPI specifications
  • Built on top of Microsoft.OpenApi
  • System.Text.Json compatible

Installation

Install via NuGet Package Manager:

dotnet add package OpenApiSourceGenerator

Or via Package Manager Console:

Install-Package OpenApiSourceGenerator

Getting Started

  1. Add your OpenAPI specification file to your project (YAML or JSON format):

    openapi/
    └── openapi.yaml
    
  2. Ensure the file is included in your project with AdditionalFiles:

    <ItemGroup>
      <AdditionalFiles Include="openapi\openapi.yaml" />
    </ItemGroup>
    
  3. Build your project - Classes will be automatically generated!

Usage Example

Given an OpenAPI specification:

openapi: 3.0.0
info:
  title: Pet Store API
  version: 1.0.0
components:
  schemas:
    Pet:
      type: object
      properties:
        id:
          type: integer
        name:
          type: string
        status:
          $ref: '#/components/schemas/PetStatus'
    PetStatus:
      type: string
      enum:
      - available
      - pending
      - sold

The generator will create:

public class Pet
{
    public int Id { get; set; }
    public string Name { get; set; }
    public PetStatus Status { get; set; }
}

public enum PetStatus
{
    Available,
    Pending,
    Sold
}

Use the generated classes in your code:

var pet = new Pet
{
    Id = 1,
    Name = "Fluffy",
    Status = PetStatus.Available
};

Known Limitations

The following OpenAPI features are currently not supported or only partially supported. Schemas that use these features will either be silently skipped or cause a build-time NotImplementedException, which surfaces as a Roslyn generator diagnostic (CS8785).

Feature Support Matrix

Feature Status Notes
Object schemas with primitive/object/array properties ✅ Supported Core functionality
$ref references ✅ Supported Object and array item references
required properties ✅ Supported Emits C# required modifier
enum ✅ Supported String enum values generate named C# enum members; integer enum values generate named members with numeric assignments. Inline property enums generate a separate enum named after the containing schema and property.
format ⚠️ Not supported format values are ignored; all integer fields map to int (e.g. int64 stays int, not long) and format modifiers for string fields (e.g. date-time, uuid, byte) are ignored, leaving the type as string. See #87
nullable / OAS 3.1 null unions ❌ Not supported type: null or union types including null throw a NotImplementedException. See #88
oneOf / anyOf / allOf ❌ Not supported Composition keywords are not handled and are silently skipped. See #91
additionalProperties ❌ Not supported Dictionary/map schemas are not generated. See #89
Non-object top-level schemas ⚠️ Partially supported Top-level enum schemas generate C# enums; other non-object schemas are silently skipped
Array schemas without items ❌ Not supported Throws NotImplementedException at build time

Enum Generation

OpenAPI enum schemas generate C# enum declarations. Top-level component enums use the schema name, referenced enum properties use that generated type, and inline property enums generate a separate enum named after the containing schema and property, such as PetStatus for a status enum inside Pet. If that name is already used by another generated type, a numeric suffix is appended, such as PetStatus2.

String enum values are converted to PascalCase member names by removing non-alphanumeric separators. If multiple values normalize to the same member name, a numeric suffix is appended to keep names stable and unique. Non-negative integer enum values generate members named Value{number}, while negative values use Negative{magnitude}; all integer members have explicit numeric assignments. For example, 5 becomes Value5 = 5 and -5 becomes Negative5 = -5; values outside the int range use a long enum backing type.

Generated string enums use System.Text.Json.Serialization.JsonStringEnumConverter with JsonStringEnumMemberNameAttribute to preserve the original OpenAPI wire values.

Nullable enum schemas follow the current nullable limitation: generated enum properties are not emitted as nullable, and OAS 3.1 null unions remain unsupported.

NotImplementedException Scenarios

The following schema patterns will cause the generator to throw a NotImplementedException during compilation, which surfaces as a CS8785 diagnostic:

  • A property or schema with type: null
  • An array property (type: array) without an items definition
  • An array items schema that is not a primitive type or an object reference (e.g. items: { type: array })

Workarounds

Until full support is added, consider the following workarounds:

  • format: If you need precise types such as long, DateTimeOffset, or Guid, manually add the generated partial class with the correct property type in your project, or post-process the generated code.
  • nullable: Avoid using type: null or OAS 3.1 null union types in schemas until support is added in #88.
  • oneOf/anyOf/allOf: Replace composition with explicit property duplication in your spec, or define types manually.
  • additionalProperties: Define dictionary properties manually.
  • Build-time failures: If the generator encounters an unsupported schema, it throws a NotImplementedException that surfaces as a CS8785 diagnostic, failing the build. Remove or exclude the problematic schema(s) from your spec until support is available.

Configuration

Place OpenAPI files in your project and mark them as AdditionalFiles:

<ItemGroup>
  <AdditionalFiles Include="**\*.yaml" />
  <AdditionalFiles Include="**\openapi.json" />
</ItemGroup>

Requirements

  • Consuming projects must target .NET 10 or higher.
  • The source generator assembly targets .NET Standard 2.0 for compiler/analyzer loadability, but the generated code uses modern .NET APIs.

How It Works

This package uses Roslyn Source Generators and Microsoft.OpenApi to analyze OpenAPI specification files during compilation and generate corresponding C# classes. The generated code is added directly to your compilation, providing IntelliSense support and compile-time type checking.

Support

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.5.1-beta 64 9/18/2026
0.5.0-beta 70 8/21/2026
0.4.0-beta 75 8/20/2026
0.3.0-beta 71 8/19/2026
0.2.0-beta 73 8/18/2026
0.1.1-beta 76 7/9/2026
0.1.0-beta 76 3/18/2026
0.0.5-alpha 80 1/17/2026
0.0.4-alpha 104 12/20/2025
0.0.3-alpha 208 12/6/2025
0.0.1-alpha 173 12/3/2025

Initial release of OpenApiSourceGenerator.
           Features:
           - Compile-time source generation from OpenAPI 3.x specifications
           - Support for YAML and JSON formats
           - Automatic C# class generation from schemas
           - Zero runtime overhead