Skip to content

Fast.OpenApi

Public members, parameters, returns, and exceptions

Targets: net8.0;net9.0;net10.0.

Contract and constraints

Generates JavaScript/TypeScript clients from Swagger JSON and application API descriptions. AddOpenApi() defaults to OpenApiSettings; it does not serve Swagger UI. OpenApiUtil generation requests swagger/{group}/swagger.json and writes beneath Fast.OpenApi in the application base directory. An existing output directory is recursively removed before generation. Call generation only from an explicitly approved maintenance operation after checking that directory. It has no output-directory or CancellationToken parameter. Blank/non-absolute addresses throw ArgumentException; a missing description provider throws ArgumentNullException. Generated Web/Mobile variants preserve download response types, expose upload progress and use unknown for unresolved schemas. Successful generation is not proof that the consumer project compiles.

Installation and examples

Install the package

bash
dotnet add package Fast.OpenApi

Example 1

csharp
using Fast.OpenApi;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Builder;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddOpenApi(builder.Configuration, "OpenApiSettings");

var app = builder.Build();
app.MapControllers();
app.Run();

Example 2

csharp
public static Task GenerateOpenApi(
    string address,
    IApiDescriptionGroupCollectionProvider apiDescriptionGroupCollectionProvider,
    List<string> groupList = null);

Example 3

csharp
using Fast.OpenApi;
using Microsoft.AspNetCore.Mvc.ApiExplorer;

public sealed class ClientExporter(IApiDescriptionGroupCollectionProvider descriptions)
{
    public Task ExportAsync(Uri documentationHost, List<string> groups)
    {
        ArgumentNullException.ThrowIfNull(documentationHost);
        if (!documentationHost.IsAbsoluteUri)
            throw new ArgumentException("需要绝对地址", nameof(documentationHost));
        return OpenApiUtil.GenerateOpenApi(documentationHost.AbsoluteUri, descriptions, groups);
    }
}

Web-host examples belong in a consumer project using Microsoft.NET.Sdk.Web and a supported .NET target. IaaS examples can be used in an ordinary class library. Registration precedes builder.Build(); configure middleware and endpoints before app.Run(). Samples retain their original code and comments. No host, external service, database operation or client generator was run, and these snippets have not been compiled in this update.

Source reference