WebApiGenerator.OpenAPI 0.8.0

dotnet add package WebApiGenerator.OpenAPI --version 0.8.0
                    
NuGet\Install-Package WebApiGenerator.OpenAPI -Version 0.8.0
                    
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="WebApiGenerator.OpenAPI" Version="0.8.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WebApiGenerator.OpenAPI" Version="0.8.0" />
                    
Directory.Packages.props
<PackageReference Include="WebApiGenerator.OpenAPI" />
                    
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 WebApiGenerator.OpenAPI --version 0.8.0
                    
#r "nuget: WebApiGenerator.OpenAPI, 0.8.0"
                    
#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 WebApiGenerator.OpenAPI@0.8.0
                    
#: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=WebApiGenerator.OpenAPI&version=0.8.0
                    
Install as a Cake Addin
#tool nuget:?package=WebApiGenerator.OpenAPI&version=0.8.0
                    
Install as a Cake Tool

OpenApi.WebApiGenerator

Generates scaffolding for Web APIs from OpenAPI specifications.

The generated functionality will route, serialize/deserialize and validate payloads according to the specification.

Supported OpenAPI version:

API frameworks supported:

.NET versions supported:

  • >=9.0

Installation

dotnet add package WebApiGenerator.OpenAPI

https://www.nuget.org/packages/WebApiGenerator.OpenAPI

Getting Started

  1. Add a reference to the generator in the project file where the API should exist:
<ItemGroup>
    <PackageReference Include="WebApiGenerator.OpenAPI" Version="x.y.z" PrivateAssets="all" />
</ItemGroup>
  1. Add a reference to your OpenAPI specification:
<ItemGroup>
    <AdditionalFiles Include="path/to/openapi.json"/>
</ItemGroup>

The first file containing the word "openapi" and have an ending of .json, .yaml or .yml will be read.

Supported data formats are:

  • JSON
  • YAML
  1. Add references to Corvus.Json.ExtendedTypes and ParameterStyleParsers.OpenAPI.
<ItemGroup>
    <PackageReference Include="Corvus.Json.ExtendedTypes" Version="4.3.13" />
    <PackageReference Include="ParameterStyleParsers.OpenAPI" Version="1.4.0" />
</ItemGroup>
  • Corvus.Json.ExtendedTypes >= 4.0.0
  • ParameterStyleParsers.OpenAPI >= 1.4.0
  1. Compile the project.

  2. Register API operations in Program.cs.

var builder = WebApplication.CreateBuilder(args);
builder.AddOperations();
var app = builder.Build();
app.MapOperations();
app.Run();

Examples:

All specifications mostly generate similar abstractions. What might differ is the location of generated resources, which follows the respective structure of the OpenAPI specification, and the JSON types, which are based on the respective schema version.

Note: The examples reference the generator through a project reference. Use a package reference instead as described above.

Implementing an API Operation

The generator generates stubbed partial classes for any operation handlers (Foo.Bar.Operation.Handler.cs) if there are none existing in the project and logs it with a compiler warning (AF1001). The classes should be copied into source control and the operation methods implemented. The operation methods have a familiar request/response design:

internal partial Task<Response> HandleAsync(Request request, CancellationToken cancellationToken);

The generated stubbed operation handler classes can be copied either manually or automatically.

Manually

Copy the content using the Solution Explorer in the IDE and create a proper file to paste it into:

  • JetBrains Rider: MyProject/Dependencies/.NET X.0/Source Generators/OpenAPI.Generator/Foo.Bar.Operation.Handler.cs
  • Visual Studio: MyProject/Dependencies/Analyzers/OpenAPI.Generator/OpenAPI.Generator.OpenApiGenerator/Foo.Bar.Operation.Handler.cs

Automatically

Let the compiler output all generated files to a directory during compilation by adding these directives to the project:

<PropertyGroup>
    <EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
    <CompilerGeneratedFilesOutputPath>GeneratedFiles</CompilerGeneratedFilesOutputPath>
</PropertyGroup>

Make sure to not include the files outputted when compiling again:

<ItemGroup>
    <Compile Remove="$(CompilerGeneratedFilesOutputPath)/**" />
</ItemGroup>

To copy the operation handlers add the following target:

<Target Name="CopyMissingOperationHandlers" 
        AfterTargets="Build" 
        Condition="'$(EmitCompilerGeneratedFiles)'=='true'">
    <ItemGroup>
        <TextFiles Include="$(CompilerGeneratedFilesOutputPath)\**\Operation.Handler.g.cs" />
    </ItemGroup>
    <Copy
        SourceFiles="@(TextFiles)"
        DestinationFiles="@(TextFiles->'generated-api-handlers\%(RecursiveDir)%(Filename)%(Extension)')"
        ContinueOnError="true" />
</Target>

Exchange generated-api-handlers to any directory.

These handlers will not be generated in subsequent compilations as the generator will detect that they already exist, but the output directory should be cleaned before compiling to avoid the same files to be copied again (and overwrite any changes done):

<Target Name="CleanSourceGeneratedFiles"
        BeforeTargets="BeforeBuild"
        DependsOnTargets="$(BeforeBuildDependsOn)"
        Condition="'$(EmitCompilerGeneratedFiles)'=='true'">
    <RemoveDir Directories="$(CompilerGeneratedFilesOutputPath)" />
</Target>

Content Negotiation

Content is negotiated for both request and responses.

See the examples for more details.

Request Body Content

Request body content is automatically mapped via the Content-Type header. The Request.Body property has content properties generated for all specified content which can be tested for nullability to figure out which one was sent.

If Body is optional, all content properties might be null.

If body is not defined for the request, there will be no Body property generated.

Response Content

Response content can be negotiated using the TryMatchAcceptMediaType method exposed by the Request class. Call it with the wanted response and it will return the best content matching the Accept header.

This method can only be used with response that define content, and it is scoped to responses defined by the current operation.

Example:

switch (request.TryMatchAcceptMediaType<Response.OK200>(out var matchedMediaType))
{
    // No match, the server decides what to do
    case false:
    // Matched any application content (application/*)
    case true when matchedMediaType == Response.OK200.AnyApplication.ContentMediaType:
        return Task.FromResult<Response>(new Response.OK200.AnyApplication(
            Components.Schemas.FooProperties.Create(name: request.Body.ApplicationJson?.Name),
            "application/json") { Headers = new Response.OK200.ResponseHeaders { Status = 2 } });
    // Matched content that has not been implemented yet by the operation handler (can be used to detect newly specified content that has not yet been implemented)
    default:
        throw new NotImplementedException($"Content media type {matchedMediaType} has not been implemented");
} 

Authentication and Authorization

OpenAPI defines security scheme objects for authentication and authorization mechanisms. The generator implement endpoint filters that corresponds to the security declaration of each operation. Do not call UseAuthentication or similar when configuring the application.

The security schemes for the security requirements declared by the operations must be implemented. Use the familiar AddAuthentication builder method to register each scheme. Security scheme object configurations are generated to the SecuritySchemes class and can be used to configure the scheme implementations.

Dependency Injection

Operations are registered as scoped dependencies. Any dependencies can be injected into them as usual via the app builder's IServiceCollection.

Options

To configure the generator add a configuration file:

<ItemGroup>
    <AdditionalFiles Include="path/to/OpenAPI.WebApiGenerator.json"/>
</ItemGroup>

Supported configuration:

  • ValidationLevel
    Description: Sets global validation level
    Values: Flag|Basic|Detailed|Verbose
    Default: Detailed

Example:

{
    "ValidationLevel": "Verbose"
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

There are no supported framework assets in this package.

Learn more about Target Frameworks and .NET Standard.

  • .NETStandard 2.0

    • 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.8.0 28 3/1/2026
0.8.0-pre-fd64fd72 42 2/27/2026
0.8.0-pre-661dc3e1 30 2/28/2026
0.8.0-pre-38027878 40 2/26/2026
0.7.3-pre-8ccf391a 42 2/24/2026
0.7.2 89 2/18/2026
0.7.2-pre-368f79e8 82 2/17/2026
0.7.1 89 2/12/2026
0.7.1-pre-9ccb6807 90 2/12/2026
0.7.0 80 2/6/2026
0.7.0-pre-c18d0d8d 90 1/30/2026
0.7.0-pre-7c661a42 103 2/5/2026
0.7.0-pre-7342dd40 85 1/30/2026
0.7.0-pre-49e06adc 83 2/5/2026
0.7.0-pre-40c4ed0b 86 1/25/2026
0.7.0-pre-3b65dd4f 83 1/25/2026
0.7.0-pre-3a5a1ab4 86 2/4/2026
0.7.0-pre-17525a76 86 1/24/2026
0.6.0 84 1/20/2026
0.6.0-pre-73ae8502 90 1/20/2026
Loading failed

# [v0.8.0](https://github.com/Fresa/OpenAPI.WebApiGenerator/compare/f52280becbcd24fc1e8f07d8415ae5df5ac7112d...a3ff9553efe49f3e9e23de6553af20591e54453e) (2026-03-01)


### Bug Fixes

* **request:** include quality value to determine content negotiation precedence for request content ([af011ba](https://github.com/Fresa/OpenAPI.WebApiGenerator/commit/af011baee490467529203577d05d8f8f9887eca4))
* **response:** move content into separate response classes to guarantee unique content signatures ([8ccf391](https://github.com/Fresa/OpenAPI.WebApiGenerator/commit/8ccf391a103d8076ad794d200462d96f28b7639f))


### Features

* **response:** add accept header content negotiation ([0b7d81a](https://github.com/Fresa/OpenAPI.WebApiGenerator/commit/0b7d81a12eed9ec5537c6686e9bb0f7375579ce9))