using Microsoft.Extensions.Options;
using Microsoft.OpenApi.Models;
using System.Reflection;
namespace DocumentService.API.Configuration
{
///
/// Provides extension methods for configuring Swagger/OpenAPI documentation.
///
public static class SwaggerConfiguration
{
///
/// Adds Swagger documentation generation to the service collection.
///
/// The service collection to add Swagger to.
/// Configuration to read SwaggerSettings from.
/// The modified service collection.
public static IServiceCollection AddSwaggerDocumentation(
this IServiceCollection services,
IConfiguration configuration)
{
var swaggerSettings = configuration.GetSection(SwaggerSettings.SectionName).Get()
?? new SwaggerSettings();
services.AddSwaggerGen(options =>
{
options.SwaggerDoc(swaggerSettings.Version, new OpenApiInfo
{
Title = swaggerSettings.Title,
Version = swaggerSettings.Version,
Description = swaggerSettings.Description
});
// Resolve conflicting actions: Keep first variant
// DualInputDocumentFilter will merge both variants into single operation
options.ResolveConflictingActions(apiDescriptions => apiDescriptions.First());
// Add document filter to merge operations with different content types
options.DocumentFilter();
// XML-Kommentare einbinden
var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
options.IncludeXmlComments(xmlPath);
});
return services;
}
}
}