RA.Utilities.OpenApi
This release of RA.Utilities.OpenApi provides a robust set of tools to enhance and customize OpenAPI (Swagger) documentation in ASP.NET Core applications. It automates common modifications to your generated OpenAPI specification, reducing boilerplate and enforcing consistency.
✨ Key Features
-
DocumentInfoTransformer: Populates the top-levelinfoobject of your OpenAPI document (title, version, description, etc.) directly from yourappsettings.jsonconfiguration, allowing you to update documentation details without code changes. -
BearerSecuritySchemeTransformer: Automatically adds a "Bearer" security scheme to your OpenAPI document when JWT authentication is detected, enabling the "Authorize" button in Swagger UI for testing protected endpoints. -
HeadersParameterTransformer: Adds common, configurable headers (likex-request-id) to every API operation, ensuring consistent documentation for tracing and correlation in distributed systems. -
ResponsesDocumentTransformer: Automatically adds standardized OpenAPI responses for common HTTP status codes (400,404,409,500), using schema models fromRA.Utilities.Api.Resultsto ensure your error contracts are clearly documented. -
Simplified Setup: Includes an
AddDefaultsDocumentTransformer()extension method to register the most common transformers (DocumentInfo,BearerSecurityScheme, andHeadersParameter) with a single line of code. -
Configuration-Driven: All transformers are configurable via
appsettings.jsonusing strongly-typed settings classes (OpenApiInfoSettings,HeadersParameterSettings), keeping your application code clean and focused.
🚀 Getting Started
- Configure Settings: Add
OpenApiInfoSettingsandOpenApiHeaderssections to yourappsettings.json. - Register Services: In
Program.cs, configure the settings classes:builder.Services.Configure<OpenApiInfoSettings>(builder.Configuration.GetSection(OpenApiInfoSettings.AppSettingsKey));
builder.Services.Configure<HeadersParameterSettings>(builder.Configuration.GetSection("OpenApiHeaders")); - Add Transformers: Use the extension methods to register the transformers:
builder.Services.AddOpenApi()
.AddDefaultsDocumentTransformer(); // Adds Info, Bearer, and Headers transformers
// Register other transformers individually
builder.Services.AddOpenApiDocumentTransformer<ResponsesDocumentTransformer>(); - Map Endpoints: Finally, map the OpenAPI endpoints in your request pipeline:
app.MapOpenApi();
✨ New Features & Improvements
- Added
SuccessResponseHelpers: Introduced a new staticSuccessResponseclass with helper methods (Ok,Created,NoContent, etc.) to simplify the creation of standardized, successful API responses. This complements the existing error response models and promotes a consistent response structure across the entire API. - Improved
READMEDocumentation:- Added a new section to the
README.mddetailing the usage of the newSuccessResponsehelpers. - Updated the endpoint example for using the
Result<T>type to demonstrate a cleaner, more powerful pattern using bothSuccessResponse.Ok()for success cases andErrorResultResponse.Resultfor failure cases.
- Added a new section to the
📝 Notes
The main goal of this update is to make API endpoint logic cleaner and more readable. By providing dedicated helpers for both success and failure responses, developers can write more expressive and maintainable code while ensuring a consistent API contract for consumers.