HealthChecksDocumentTransformer
Namespace: RA.Utilities.OpenApi.DocumentTransformers
The HealthChecksDocumentTransformer is an IOpenApiDocumentTransformer that adds your service's health check endpoints to the OpenAPI document.
🎯 Purpose
Endpoints mapped with MapHealthChecks carry no API metadata, so ASP.NET Core's OpenAPI generator skips them and they never appear in the generated document.
For consumers, load balancers, and ops tooling that probe these endpoints, the API contract is incomplete without them.
This transformer documents the readiness endpoint (/health) with the JSON report schema written by UIResponseWriter.WriteHealthCheckUIResponse and the liveness endpoint (/alive) with its plain text response — no attributes or manual OpenAPI edits required.
⚙️ How It Works
- Documents
GET /health: Adds an operation (summary "Service health") that returns the aggregate health status of the service together with the result of each registered health check, with200and503responses described asapplication/json. - Documents
GET /alive: Adds an operation (summary "Service liveness") that returns the liveness state of the service as plain text, based on the health checks tagged withlive, with200and503responses described astext/plain. - Adds component schemas:
HealthCheckReport(aggregatestatus,totalDuration, and per-checkentries) andHealthCheckEntry(data,description,duration,exception,status,tags). Thestatusproperty is an enum ofHealthy,Degraded, andUnhealthy. - Adds the
Healthtag with a description, and applies it to both operations. - Idempotent: Paths, schemas, and tags already present in the document are left untouched.
- Stays anonymous: Registered after
BearerSecurityDocumentTransformer, so no security scheme is applied to the health check operations. They must stay reachable without authentication.
🚀 Usage
The transformer is registered automatically when you use the default set of transformers via AddDefaultsDocumentTransformer:
// Program.cs
using HealthChecks.UI.Client;
using Microsoft.AspNetCore.Diagnostics.HealthChecks;
using RA.Utilities.OpenApi.Extensions;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenApi(options =>
{
options.AddDefaultsDocumentTransformer();
});
var app = builder.Build();
// JSON report for /health (the schema documented by the transformer)
app.MapHealthChecks("/health", new HealthCheckOptions
{
ResponseWriter = UIResponseWriter.WriteHealthCheckUIResponse
});
// Plain-text liveness state for /alive
app.MapHealthChecks("/alive", new HealthCheckOptions
{
Predicate = registration => registration.Tags.Contains("live")
});
app.MapOpenApi();
app.Run();
UIResponseWriter.WriteHealthCheckUIResponse comes from the AspNetCore.HealthChecks.UI.Client package; add it to your application if you want the documented JSON shape. The transformer itself only describes the contract — it does not write the health response.
With this setup, the generated document contains both health check operations under the Health tag, including their response schemas.
AddHealthChecksDocumentTransformer(IDictionary<string, string> tags) currently registers HeadersParameterTransformer instead of HealthChecksDocumentTransformer, and the tags argument is ignored. Use AddDefaultsDocumentTransformer() until this is corrected.