Skip to content

OpenApi

Built in OpenAPI support, providing full support for OpenAPI.

Features

  • OpenApi metadata
  • Route path parameters
  • Route query parameters
  • Api versions
  • OpenApi types are creating removing the properties used in map-from bindings
  • Multipart support. Allowing for a file and Json payload to be sent on the same request
  • JWT bearer support

Specific lines to add

builder.Services.EnableOpenApiForWebApiEndpoint();
application.UseOpenApiUIForWebApiEndpoint();
application.UseWebApiEndpoints();

Add this line for each Api Version

builder.Services.AddOpenApiVersion("WebApiEndpoint Samples", WebApiEndpointVersions.V1_0);

Complete example

Log.Logger = new LoggerConfiguration()
             .Enrich.FromLogContext()
             .WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj}{NewLine}{Exception}")
             .CreateBootstrapLogger();

try
{
    Log.Information("Application starting up");

    var builder = WebApplication.CreateBuilder(args);

    builder.Host.UseSerilog((hostBuilderContext, loggerConfiguration) =>
                                loggerConfiguration.WriteTo.Console(outputTemplate: "[{Timestamp:HH:mm:ss.fff} {Level:u3}] {Message:lj}{NewLine}{Exception}")
                                                   .ReadFrom.Configuration(hostBuilderContext.Configuration));

    builder.Host.ConfigureServices(serviceCollection =>
    {
        serviceCollection.RegisterModule(new WebApiEndpointModule(typeof(AssemblyHook).Assembly));

        serviceCollection.RegisterModule<ApplicationModule>();
    });

    builder.Services.EnableOpenApiForWebApiEndpoint();
    builder.Services.EnableOpenApiJwtForWebApiEndpoint();

    builder.Services.AddOpenApiVersion("WebApiEndpoint Samples", WebApiEndpointVersions.V1_0);
    builder.Services.AddOpenApiVersion("WebApiEndpoint Samples", WebApiEndpointVersions.V2_0);
    builder.Services.AddOpenApiVersion("WebApiEndpoint Samples - Deprecated", WebApiEndpointVersions.V3_0, true);

    builder.Services.AddWebApiEndpointAuthorization(typeof(AssemblyHook).Assembly);
    builder.Services.AddAuthenticationJwtBearer(builder.Configuration["Jwt:Issuer"], builder.Configuration["Jwt:Audience"], builder.Configuration["Jwt:Key"]);

    var application = builder.Build();

    application.UseAuthentication();
    application.UseAuthorization();

    if (application.Environment.IsDevelopment())
    {
        application.UseOpenApiUIForWebApiEndpoint();
    }

    application.UseHttpsRedirection();

    application.UseWebApiEndpoints();

    application.Run();
}
catch (Exception exception)
{
    Log.Fatal(exception, "Application start-up failed");
}
finally
{
    Log.Information("Application shut down complete");
    Log.CloseAndFlush();
}

Example show OpenApi support with full metadata and multiple version

V1

v1

V2

v2