Advertisement

API Documentation with Swagger/OpenAPI interview questions and answers

15 API Documentation with Swagger/OpenAPI interview questions for ASP.NET Core roles, each with a model answer, examples and likely follow-ups.

15 questions
Advertisement

15 questions

Q11 Advanced Save

What is 'design-first' versus 'code-first' API development, and how does OpenAPI tooling support each approach?

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 APIs RESTful Web APIs & Controllers
Q12 Intermediate Save

How do you version your OpenAPI documentation to reflect API versioning implemented via the Asp.Versioning library?

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.
builder.Services.ConfigureOptions<ConfigureSwaggerOptions>();  // discovers versions dynamically

app.UseSwaggerUI(options => {
    foreach (var description in app.DescribeApiVersions()) {
        options.SwaggerEndpoint($"/swagger/{description.GroupName}/swagger.json", description.GroupName.ToUpperInvariant());
    }
});
Real-world example
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?
API Versioning Controllers vs Minimal APIs
Q13 Advanced Save

How do you document and test webhook or callback endpoints (server-initiated calls to a client-registered URL) in OpenAPI 3.1?

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.
Example

// Conceptual OpenAPI 3.1 webhooks section (YAML) // webhooks: // orderCompleted: // post: // requestBody: // content: // application/json: // schema: // $ref: '#/components/schemas/OrderCompletedPayload'

Real-world example
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`?
gRPC Services RESTful Web APIs & Controllers
Q14 Intermediate Save

How do you add global metadata (like a description, contact info, and license) to the top level of a generated OpenAPI document?

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/OpenAPI RESTful Web APIs & Controllers
Q15 Advanced Save

How does schema reuse work in OpenAPI (via $ref and components/schemas), and how does Swashbuckle handle types shared across multiple endpoints?

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 & Structures RESTful Web APIs & Controllers

Showing 11–15 of 15