liveBook · Manning

Chapter 11: Documenting APIs with OpenAPI

What you will learn in chapter 11 of ASP.NET Core in Action, 3e

This chapter introduces OpenAPI as a way to describe a minimal API and previews using that description for documentation, testing, metadata, and generated C# clients.

After this chapter you will be able to

What it builds on

Section by section

Introduction

This chapter introduces OpenAPI as a way to describe a minimal API and previews using that description for documentation, testing, metadata, and generated C# clients.

Key terms: OpenAPI specification, OpenAPI document, RESTful APIs, minimal API, Swagger UI, metadata, NSwag, generated client, C# client, summaries

11.1 Adding an OpenAPI description to your app

This section introduces OpenAPI documents and shows how Swashbuckle generates and serves an OpenAPI description for a minimal API application.

Key terms: OpenAPI, Swagger, OpenAPI document, RESTful APIs, JSON, Swashbuckle.AspNetCore, Swashbuckle, AddSwaggerGen(), ASP.NET Core metadata services, middleware

11.2 Testing your APIs with Swagger UI

This section introduces Swagger UI as a Swashbuckle-provided web interface for exploring and testing an app’s OpenAPI-described endpoints, while revealing why endpoint metadata may need improvement.

Key terms: Swagger UI, OpenAPI, Postman, Swashbuckle, Swagger UI middleware, OpenAPI document middleware, /swagger, Try it out, Execute, schema

11.3 Adding metadata to your minimal APIs

This section explains endpoint metadata in minimal APIs, focusing on optional OpenAPI metadata that improves generated documentation and Swagger UI.

Key terms: metadata, minimal API endpoints, WithName(), LinkGenerator, routing metadata, middleware metadata, authorization, hostname filtering, output caching, OpenAPI metadata

11.4 Generating strongly typed clients with NSwag

This section introduces using NSwag, through Visual Studio or a .NET tool, to generate and keep updated a strongly typed C# client from an API’s OpenAPI description.

Key terms: strongly typed client, serialization, deserialization, OpenAPI description, NSwag, Visual Studio, Microsoft .NET OpenAPI global tool, Swashbuckle, OpenAPI description JSON file, endpoints

11.4.1 Generating a client using Visual Studio

This section shows how to use Visual Studio to generate an NSwag-based strongly typed API client from an accessible OpenAPI document and update its required packages.

Key terms: Add Service Reference, OpenAPI service reference, OpenAPI document, OpenAPI description, generated client, FruitClient, OpenApiReference, NSwag.ApiDescription.Client, Microsoft.Extensions.ApiDescription.Client, NuGet

11.4.2 Generating a client using the .NET Global tool

This section shows how to use the .NET OpenAPI global tool to add an OpenAPI reference to a console project and generate an NSwag client without Visual Studio.

Key terms: .NET OpenAPI tool, OpenAPI reference, OpenAPI description, NSwag, OpenApiReference, OpenAPIs\fruit.json, ClassName, Namespace, csproj

11.4.3 Using a generated client to call your API

This section shows how an NSwag-generated C# client is produced from an OpenAPI description, used to call API endpoints, and improved through endpoint names.

Key terms: NSwag.ApiDescription.Client, Microsoft.Extensions.ApiDescription.Client, OpenAPI description, OpenApiReference, namespace, FruitClient, HttpClient, base address, FruitPOSTAsync(), FruitGETAsync()

11.4.4 Customizing the generated code

This section shows how to configure NSwag-generated clients for System.Text.Json, interfaces, and HttpClient-based addresses, and how to force regeneration when those options do not take effect.

Key terms: System.Text.Json, Newtonsoft.Json, GenerateClientInterfaces, UseBaseUrl, BaseAddress, HttpClient, OpenApiReference, Options, /UseBaseUrl:false, /GenerateClientInterfaces:true

11.4.5 Refreshing the OpenAPI description

This section explains how to refresh a saved OpenAPI document so NSwag regenerates a client that reflects the current API description.

Key terms: OpenAPI description, point-in-time snapshot, NSwag, generated client, metadata, Connected Services, OpenAPI reference, Refresh, .NET OpenAPI tool, OpenAPI description document

11.5 Adding descriptions and summaries to your endpoints

This section introduces adding descriptions, summaries, and other metadata to minimal API endpoints so OpenAPI-based tools provide a better developer experience.

Key terms: OpenAPI description document, descriptions, summaries, metadata, minimal API endpoints, Swagger UI

11.5.1 Using fluent methods to add descriptions

This section shows how fluent metadata methods and WithOpenApi() add endpoint and parameter documentation to a minimal API’s generated OpenAPI document.

Key terms: WithSummary(), WithDescription(), WithOpenApi(), Microsoft.AspNetCore.OpenApi, OpenAPI description, Swashbuckle, lambda method, OpenAPI metadata, parameter description, id parameter

11.5.2 Using attributes to add metadata

This section shows how to replace most fluent endpoint metadata calls with attributes, while weighing the readability cost of API metadata.

Key terms: attributes, endpoint metadata, extension methods, handler method, lambda function, WithOpenApi(), Swashbuckle, OpenAPI metadata, fluent interfaces, metadata

11.5.3 Using XML documentation comments to add metadata

This section shows how to use C# XML documentation comments as OpenAPI summaries and parameter descriptions, and when that level of metadata is worthwhile.

Key terms: XML documentation comments, Swashbuckle, OpenAPI description, GenerateDocumentationFile, SwaggerGen, csproj, PropertyGroup, static method handler, .NET 7, static method endpoint handlers

11.6 Knowing the limitations of OpenAPI

This section introduces the caveat that OpenAPI and Swagger have limitations, especially for increasingly complex APIs, before examining those challenges.

Key terms: OpenAPI, Swagger, Swagger UI, code generation

11.6.1 Not all APIs can be described by OpenAPI

This section explains why OpenAPI cannot fully describe certain dynamic, aggregated, or unusually secured APIs.

Key terms: OpenAPI specification, REST APIs, Hypertext As the Engine of Application State, HATEOAS, links, resource state, backend APIs, API gateway, unified API, code generation

11.6.2 Generated code is opinionated

This section explains that generated API clients reflect their generator's design choices, so customization costs, exception behavior, and incompatible generated types can sometimes outweigh their benefits.

Key terms: code generation, OpenAPI documents, generated client, NSwag, exceptions, process flow, error status code, unexpected status code, ProductsClient, CartClient

11.6.3 Tooling often lags the specification

This section explains why independently evolving OpenAPI tools can limit a documentation and client-generation workflow, and when OpenAPI is worth adopting despite those limits.

Key terms: OpenAPI specification, OpenAPI Initiative, ASP.NET Core metadata, Swashbuckle, NSwag, Swagger UI, tool chain, OpenAPI-compatible document, OpenAPI documents, code generation

11.7 Summary

This summary recaps how to generate, expose, enrich, consume, and recognize the limits of OpenAPI descriptions for ASP.NET Core APIs.

Where this is used later

Concepts explained in this chapter

← Chapter 10: Configuring an ASP.NET Core application Chapter 12: Saving data with Entity Framework Core →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.