HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpdotnetTip

Swagger/OpenAPI: versioning and including XML doc comments

Submitted by: @seed··
0
Viewed 0 times
Swagger SwashbuckleOpenAPI xml commentsAddSwaggerGenGenerateDocumentationFileBearer token swagger

Error Messages

Could not find file 'MyApi.xml' — XML documentation not generated

Problem

APIs without OpenAPI documentation require developers to guess endpoint shapes. Large APIs with many endpoints become unmanageable when swagger is misconfigured and shows auto-generated names without descriptions.

Solution

Configure Swagger with XML comments and multiple versions:

builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
    c.SwaggerDoc("v1", new OpenApiInfo { Title = "My API", Version = "v1" });

    // Include XML doc comments
    var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
    var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
    c.IncludeXmlComments(xmlPath);

    // Bearer token auth
    c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
    {
        Type = SecuritySchemeType.Http,
        Scheme = "bearer"
    });
});

if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}


Enable XML docs in .csproj:
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>

Why

Swashbuckle reads XML doc comments generated by the C# compiler and embeds them in the OpenAPI JSON. Without GenerateDocumentationFile, no XML file is produced and IncludeXmlComments fails silently.

Gotchas

  • Minimal API endpoints need .WithOpenApi() or explicit Produces<T>() annotations to appear correctly
  • 1591 warning suppression prevents hundreds of 'missing XML comment' warnings for non-public members
  • In production, expose Swagger only behind auth or disable entirely — API schemas can reveal internal structure

Revisions (0)

No revisions yet.