liveBook · Manning

Swashbuckle

As explained in Building Web APIs with ASP.NET Core

Swashbuckle is a .NET toolset that generates OpenAPI descriptions and provides interactive Swagger documentation for ASP.NET Core APIs.

Swashbuckle is a set of ASP.NET Core services, middleware, and tools for documenting a web API with OpenAPI. It generates a machine-readable swagger.json description of available endpoints and provides SwaggerUI, a browser-based view that can also be used to test them. This gives developers a way to discover an API even though the API itself has no conventional user interface. section 3.5 section 11.2.1

In an ASP.NET Core web API created with Visual Studio’s Enable OpenAPI support option, the Swashbuckle setup is already present. The generator service creates the description; one middleware exposes its JSON endpoint, and another serves SwaggerUI. The shown middleware runs only in the Development environment. section 3.2.2

// ...
 
builder.Services.AddSwaggerGen();    #1
 
// ...
 
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();                #2
    app.UseSwaggerUI();              #3
}

In Program.cs, AddSwaggerGen registers generation, while UseSwagger and UseSwaggerUI expose the generated document and its browser interface during development. section 3.2.2

To check the result in the board-game API:

  • Run the application in Debug mode and open /swagger/v1/swagger.json to inspect the generated OpenAPI description. section 3.2.2
  • Open /swagger to browse the same documentation in SwaggerUI and inspect the endpoints and return types. section 3.2.2

The defaults are a starting point: the generator uses OpenAPI 3.0 and assumes API version 1.0. Service and middleware options can change settings such as the JSON endpoint. Swashbuckle can also refine generated documentation through data attributes and a filter pipeline, including descriptions of operations, parameters, and responses. section 3.2.2 section 11.5

Endpoint grouping shows why customization can matter. Swashbuckle automatically groups controller actions by controller name using OpenAPI tags. Minimal API endpoints lack that controller association and fall into a generic group; a SwaggerOperation attribute can assign related endpoints a more useful tag. section 11.2.7

app.MapGet("/auth/test/1",
    [Authorize]
    [EnableCors("AnyOrigin")]
    [SwaggerOperation(
Tags = new[] { "Auth" }, #1
        Summary = "Auth test #1 (authenticated users).",
        Description = "Returns 200 - OK if called by " +
        "an authenticated user regardless of its role(s).")]

The SwaggerOperation attribute assigns an Auth tag to an authorization-related Minimal API endpoint and supplies its summary and description. section 11.2.7

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Walks through service and middleware setup, verification, and endpoint tagging; identifies deeper generator customization without detailing its filter implementation.
Examples
Uses Program.cs registration, swagger.json and SwaggerUI URLs, and a SwaggerOperation tag on an authorization endpoint.
What it assumes
Assumes an ASP.NET Core web API project and explains the roles of its Swagger generator service and middleware.
What you can do after
Operate/configure it to inspect generated API documentation and group Minimal API endpoints with explicit tags.
How it connects
Connects OpenAPI, SwaggerUI, ASP.NET Core middleware, controllers, Minimal APIs, and endpoint tags.

Also explained in

Compare how 3 books explain Swashbuckle →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.