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!
- ✨ 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
Install via NuGet Package Manager:
dotnet add package OpenApiSourceGeneratorOr via Package Manager Console:
Install-Package OpenApiSourceGenerator-
Add your OpenAPI specification file to your project (YAML or JSON format):
openapi/ └── openapi.yaml -
Ensure the file is included in your project with
AdditionalFiles:<ItemGroup> <AdditionalFiles Include="openapi\openapi.yaml" /> </ItemGroup>
-
Build your project - Classes will be automatically generated!
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
format: int64
name:
type: string
status:
type: stringThe generator will create:
public class Pet
{
public long Id { get; set; }
public string Name { get; set; }
public string Status { get; set; }
}Use the generated classes in your code:
var pet = new Pet
{
Id = 1,
Name = "Fluffy",
Status = "available"
};Place OpenAPI files in your project and mark them as AdditionalFiles:
<ItemGroup>
<AdditionalFiles Include="**\*.yaml" />
<AdditionalFiles Include="**\openapi.json" />
</ItemGroup>- .NET Standard 2.0 or higher
- C# 7.3 or higher
This package uses Roslyn Source Generators 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.
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.