Skip to content

ADR-0015 — Use Native .NET OpenAPI Generation for the API Contract

Status

Accepted

Context

The example exposes a REST API (MyApp.Api). Its contract must be documented deterministically — the OpenAPI document is a pipeline artefact rendered into the MkDocs site, and a future typed client generator (APP-008, NSwag) would consume the same document. Hand-written OpenAPI would drift from the code immediately.

The traditional .NET approach (Swashbuckle) reflects over the running application at runtime to build the document. Since .NET 9, ASP.NET Core ships native OpenAPI support that can emit the document at build time, with no reflection pass and no separate server.

Decision

Adopt the native Microsoft.AspNetCore.OpenApi document generation (Microsoft.AspNetCore.OpenApi + Microsoft.Extensions.ApiDescription.Server 10.0.15 in MyApp.Api.csproj):

  1. AddOpenApi() / MapOpenApi() register the document; the GetDocument build target makes dotnet build itself emit docs/generated/openapi.json (OpenAPI 3.1) — no runtime reflection, no Swashbuckle.
  2. The JSON is a git-ignored deterministic artefact, re-emitted on every build.
  3. scripts/generate-openapi-docs.py renders the human-readable docs/application/api-reference.md (endpoint inventory, parameters, request/response schemas) from the document, carrying the XML-comment summaries into the published page.
  4. The same document is the input for the future client-SDK generation (APP-008) — one contract, many consumers.

Consequences

Positive

  • The contract is generated by the compiler, from the same build that the quality gates ran on — it cannot disagree with the deployed code.
  • No runtime reflection: compatible with trim/AOT and with headless build environments (the document exists immediately after dotnet build).
  • OpenAPI 3.1 native; document title, versions, and summaries come from the enforced XML documentation.

Negative / Trade-offs

  • Build-time generation is only available from .NET 9+ — acceptable, the platform pins .NET 10 (APP-004).
  • The document reflects the controllers' declared route/response shapes; runtime-negotiated content is out of scope by design.

Alternatives Considered

Alternative Pros Cons / Rejected Because
Swashbuckle Mature, large ecosystem, filters Runtime reflection (no AOT/trim); separate server step to emit the document
NSwag CLI Document + SDK generation Reflection-based or hand-configured; SDK generation deferred anyway (APP-008)
Hand-written OpenAPI Full control Violates the documentation-as-code principle; drifts immediately