Code-first (the default with Swashbuckle) generates the OpenAPI spec automatically from your existing C# code, meaning the code is the source of truth and documentation follows implementation. Design-first instead starts by hand-authoring the OpenAPI YAML/JSON spec as the contract, then uses code generation tools (like NSwag's server stub generation) to scaffold matching controller interfaces from that spec, letting API design and stakeholder review happen before any implementation code is written -- valuable for API-first organizations needing early contract agreement across teams.
Example
# Design-first: author the spec first
# api-spec.yaml defines the full contract
# Generate server stubs from the spec
nswag openapi2cscontroller /input:api-spec.yaml /output:GeneratedControllers.cs
# Developers then implement the generated interface's logic
Real-world example
A large enterprise with separate frontend and backend teams adopts design-first API development, having both teams review and agree on an OpenAPI spec before backend implementation begins, avoiding costly late-stage contract renegotiation.
Likely follow-ups
What are the trade-offs of design-first for smaller teams or rapidly iterating projects?
How do you keep a design-first spec and its generated stubs in sync as requirements evolve?
Controllers vs Minimal APIsRESTful Web APIs & Controllers
Swashbuckle integrates with Asp.Versioning by generating a separate SwaggerDoc per registered API version (using IApiVersionDescriptionProvider to discover them dynamically), and configuring SwaggerUI with a corresponding SwaggerEndpoint per version, producing a dropdown in the UI letting consumers browse documentation specific to each API version rather than one merged, potentially confusing document mixing all versions together.
An API supporting both v1 and v2 shows a version selector dropdown in Swagger UI, letting a developer integrating against the legacy v1 contract view accurate, version-specific documentation without seeing v2-only endpoints mixed in.
Likely follow-ups
How does ConfigureSwaggerOptions dynamically discover registered API versions?
What happens to a deprecated version's documentation once it's fully removed from the API?
OpenAPI 3.1 introduced the `webhooks` top-level field (distinct from regular `paths`), specifically for documenting server-to-client callback patterns where your API calls a URL the client registered, rather than the client calling your API -- letting you describe the expected payload shape a webhook consumer should expect to receive, which Swashbuckle and newer OpenAPI tooling increasingly support as OpenAPI 3.1 adoption grows.
A payments platform documents its webhook payload shape (sent to merchant-registered callback URLs when a payment completes) using OpenAPI 3.1's webhooks section, giving integrators a clear, versioned contract for what their webhook receiver endpoint should expect.
Likely follow-ups
What OpenAPI version does Swashbuckle currently target, and does it support 3.1's webhooks field?
How do webhook payloads differ from a typical request/response documented in `paths`?
SwaggerDoc's OpenApiInfo object accepts a Title, Version, Description, Contact, and License, populating the top-level info section of the OpenAPI document and Swagger UI's header -- giving consumers essential context (who maintains the API, how to get support, licensing terms) beyond just the endpoint-level details.
builder.Services.AddSwaggerGen(c => {
c.SwaggerDoc("v1", new OpenApiInfo {
Title = "Order Management API",
Version = "v1",
Description = "Handles order creation, tracking, and fulfillment.",
Contact = new OpenApiContact { Name = "API Support", Email = "api-support@company.com" },
License = new OpenApiLicense { Name = "Proprietary" }
});
});
Real-world example
A public API's Swagger UI header prominently displays a support email and a clear description of the API's purpose, reducing the number of basic 'what does this API do' support inquiries the team receives.
Likely follow-ups
How does this metadata surface in generated client SDKs?
What other top-level OpenAPI fields (like termsOfService) are commonly populated?
API Documentation with Swagger/OpenAPIRESTful Web APIs & Controllers
OpenAPI's components/schemas section defines reusable type definitions once, referenced elsewhere via $ref pointers instead of duplicating the full schema inline at every usage -- Swashbuckle automatically deduplicates C# types used across multiple endpoints into this shared schemas section (using the type name as the schema key), which is why giving DTOs clear, unique names matters, since naming collisions between different types with the same short name can cause schema generation conflicts.
// Both endpoints reference the same ProductDto schema via $ref
// rather than duplicating its full definition twice in the OpenAPI document
[HttpGet("{id}")]
public ActionResult<ProductDto> GetProduct(int id) { ... }
[HttpPost]
public ActionResult<ProductDto> CreateProduct(ProductDto dto) { ... }
// Swashbuckle generates ONE #/components/schemas/ProductDto, referenced by both operations
Real-world example
A team encounters a Swashbuckle schema ID conflict error after two unrelated features both defined a class named 'Response' in different namespaces, requiring a custom SchemaId resolver to disambiguate them in the generated document.
Likely follow-ups
How do you resolve a schema naming conflict using a custom SchemaId function?
What's the benefit of $ref-based reuse for very large APIs with hundreds of endpoints?
Data Types & StructuresRESTful Web APIs & Controllers