skip to content

In a .NET 10 ASP.NET Core API, when would you keep Swashbuckle or NSwag instead of the built-in Microsoft.AspNetCore.OpenApi generator?

level: middleimportance: should knowfreq 50%

answer

  1. Swashbuckle left the templates in .NET 9
  2. AddSwaggerGen, UseSwagger, UseSwaggerUI
  3. Swashbuckle 10 still defaults to 3.0
  4. NSwag: AddOpenApiDocument, UseOpenApi, NJsonSchema
  5. filters versus transformers

basics

~20 s

New .NET 10 APIs normally use Microsoft.AspNetCore.OpenApi. Keep Swashbuckle when an existing app relies on its filters, annotations, CLI or Swagger 2.0 output, and NSwag when you also want its C# or TypeScript client generation from the same toolchain.

solid answer

~40 s

Since .NET 9 the templates use `Microsoft.AspNetCore.OpenApi`: `AddOpenApi`/`MapOpenApi`, transformers, 3.1 by default, trimming and Native AOT support, no UI. **Swashbuckle.AspNetCore** 10 is still maintained: `AddSwaggerGen(o => o.SwaggerDoc("v1", ...))`, `UseSwagger()` at `/swagger/{documentName}/swagger.json`, `UseSwaggerUI()` at `/swagger`, customised through `IOperationFilter`, `IDocumentFilter` and `ISchemaFilter`. Its `SwaggerOptions.OpenApiVersion` still defaults to 3.0, it can emit Swagger 2.0, and version 10 moved to Microsoft.OpenApi 2 as well. **NSwag** uses its own model on NJsonSchema: `AddOpenApiDocument()` (3.0) or `AddSwaggerDocument()` (2.0), `UseOpenApi()`, `UseSwaggerUi()`, plus client code generators. Keep a library when migrating would cost more than it saves: many filters, a Swagger 2.0 consumer, or NSwag-generated clients already in the build.

code

csharp · 10 lines
csharp
// Swashbuckle.AspNetCore 10
using Microsoft.OpenApi;   // not Microsoft.OpenApi.Models in 10.x

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(o =>
    o.SwaggerDoc("v1", new OpenApiInfo { Title = "Flight status", Version = "v1" }));

var app = builder.Build();
app.UseSwagger(o => o.OpenApiVersion = OpenApiSpecVersion.OpenApi3_1);
app.UseSwaggerUI();                    // UI at /swagger

go deeper

for a junior

Recall that new templates use Microsoft.AspNetCore.OpenApi and that Swashbuckle and NSwag are community packages you add yourself.

for a middle

Compare the registration calls, default routes and default OpenAPI versions of the three generators, and how each is customised.

for a senior

Judge a migration: count the filters, check consumers' version needs and client generation, and plan the Microsoft.OpenApi 2 break whichever way you go.

for a principal

Set a standard generator across services so published documents share one version and one set of conventions, and decide when legacy generators are retired.

## Three generators, one source of truth All three generators read the same thing: ASP.NET Core's **ApiExplorer** descriptions of your endpoints. They differ in their object model, defaults, extension points and what else they do. Since .NET 9 the templates ship only the built-in one; the other two are community packages you add yourself. | | Microsoft.AspNetCore.OpenApi | Swashbuckle.AspNetCore 10.2 | NSwag 14.7 | |---|---|---|---| | Register | `AddOpenApi()` | `AddSwaggerGen(o => o.SwaggerDoc(...))` | `AddOpenApiDocument()` or `AddSwaggerDocument()` | | Serve | `MapOpenApi()` | `UseSwagger()` or `MapSwagger()` | `UseOpenApi()` | | Default route | `/openapi/{documentName}.json` | `/swagger/{documentName}/swagger.json` | `/swagger/{documentName}/swagger.json` | | Default version | OpenAPI 3.1 | OpenAPI 3.0 | 3.0 (`AddSwaggerDocument`: 2.0) | | Object model | Microsoft.OpenApi 2 | Microsoft.OpenApi 2 | NSwag's own, on NJsonSchema | | Customise with | transformers | filters | processors and settings | | UI | none | `UseSwaggerUI()` at `/swagger` | `UseSwaggerUi()`, `UseReDoc()` | ## When the built-in generator is the default choice - It is what `dotnet new webapi` adds, it ships in the ASP.NET Core repository with each release, and it supports trimming and Native AOT. - Document, operation and schema **transformers** cover what most apps customise. - It reads Minimal API metadata (`WithName`, `WithSummary`, `Produces`) and, in .NET 10, XML comments, without extra packages. Its gaps are deliberate: no UI, so you add Scalar or Swagger UI on top, and no client generation. ## When to keep Swashbuckle Swashbuckle remains maintained and its 10.x line targets .NET 8, 9 and 10. Reasons to keep it in an existing app: - A body of **filters** (`IOperationFilter`, `IDocumentFilter`, `ISchemaFilter`) and **annotations** such as `[SwaggerOperation]` from `Swashbuckle.AspNetCore.Annotations`, enabled with `EnableAnnotations()`, that would all need rewriting as transformers. - A consumer that needs **Swagger 2.0**, which Swashbuckle can still write by setting `OpenApiVersion` to `OpenApiSpecVersion.OpenApi2_0`. - Its CLI, `swagger tofile`, already wired into a pipeline. - Newtonsoft.Json serialization, supported through the separate `Swashbuckle.AspNetCore.Newtonsoft` package. Two cautions. Swashbuckle 10 moved to Microsoft.OpenApi 2, so upgrading from 9 breaks filters exactly as .NET 10 breaks built-in transformers: the `Microsoft.OpenApi.Models` namespace is gone, `Nullable` is gone, references are `*Reference` classes. And unlike the built-in generator it still defaults to **3.0**; opt in with `options.OpenApiVersion = OpenApiSpecVersion.OpenApi3_1` in `UseSwagger`. ## When to keep NSwag NSwag's selling point is the **toolchain**: the same project that serves the document can generate C# and TypeScript clients, run from MSBuild through `NSwag.MSBuild` or interactively in NSwagStudio. If a team already ships NSwag-generated clients, keeping NSwag on the server keeps one tool and one set of conventions. Its document model is NSwag's own, built on NJsonSchema rather than Microsoft.OpenApi, so built-in transformers and Swashbuckle filters do not port to it. ## Migrating from Swashbuckle to the built-in generator 1. Replace `AddSwaggerGen` and `SwaggerDoc` with `AddOpenApi()`, moving `OpenApiInfo` details into a document transformer. 2. Port each `IDocumentFilter` to an `IOpenApiDocumentTransformer`, each `IOperationFilter` to an `IOpenApiOperationTransformer` and each `ISchemaFilter` to an `IOpenApiSchemaTransformer`. 3. Replace `[SwaggerOperation]` summaries with `WithSummary`, `WithDescription` or `[EndpointSummary]`. 4. Replace `UseSwagger()` with `MapOpenApi()` and repoint every consumer to `/openapi/v1.json`. 5. Diff the old and new documents before switching consumers; the default version also moves from 3.0 to 3.1. ## Mixing them - You can run the built-in generator and serve **Swagger UI** from `Swashbuckle.AspNetCore.SwaggerUI` alone, pointing it at `/openapi/v1.json`; the ASP.NET Core docs show that pairing. - Avoid two generators producing two different documents for the same API; consumers will pick different ones. - `WithOpenApi()` was the bridge between Minimal API metadata and Swashbuckle; it is obsolete in .NET 10, and Swashbuckle's migration guide also tells you to remove it.

  • Can you use Swagger UI without Swashbuckle's generator?
    Yes. Reference only `Swashbuckle.AspNetCore.SwaggerUI` and call `app.UseSwaggerUI(o => o.SwaggerEndpoint("/openapi/v1.json", "v1"))`. The built-in generator produces the document and Swagger UI only renders it; the ASP.NET Core docs describe this pairing for development.
  • What breaks when moving an app from Swashbuckle 9 to 10?
    Swashbuckle 10 adopted Microsoft.OpenApi 2: `using Microsoft.OpenApi.Models` becomes `using Microsoft.OpenApi`, filters receive interfaces like `IOpenApiSchema`, `Nullable` becomes the `JsonSchemaType.Null` flag, `.Reference` becomes `*Reference` classes, and `AddSecurityRequirement` takes a `Func<OpenApiDocument, OpenApiSecurityRequirement>`. Its guide recommends upgrading to 9.0.6 first.

  • Swashbuckle is deprecated in .NET 10 and no longer works on net10.0.
  • Swashbuckle 10 writes OpenAPI 3.1 by default, just like the built-in generator.
  • Built-in transformers can be reused unchanged as NSwag document processors.
  • UseSwaggerUI requires AddSwaggerGen; it cannot render a document from MapOpenApi.
  • The built-in package includes Swagger UI, so a separate UI package is never needed.

Assumes .NET 10 with Swashbuckle.AspNetCore 10.2 and NSwag 14.7: Swashbuckle was removed from templates in .NET 9, its 10.x moved to Microsoft.OpenApi 2 but still defaults to OpenAPI 3.0.