#nullable enable using Microsoft.AspNetCore.Mvc.ApiExplorer; using Microsoft.AspNetCore.Mvc.Controllers; using Swashbuckle.AspNetCore.SwaggerGen; using BaseRoute = IOL.GreatOffice.Api.Endpoints.V1.BaseRoute; namespace IOL.GreatOffice.Api.Utilities; public static class SwaggerGenOptionsExtensions { /// /// Updates Swagger document to support ApiEndpoints.

/// For controllers inherited from :
/// - Replaces action Tag with [namespace]
///
public static void UseApiEndpoints(this SwaggerGenOptions options) { options.TagActionsBy(EndpointNamespaceOrDefault); } private static IList EndpointNamespaceOrDefault(ApiDescription api) { if (api.ActionDescriptor is not ControllerActionDescriptor actionDescriptor) { throw new InvalidOperationException($"Unable to determine tag for endpoint: {api.ActionDescriptor.DisplayName}"); } if (actionDescriptor.ControllerTypeInfo.GetBaseTypesAndThis().Any(t => t == typeof(BaseRoute))) { return new[] { actionDescriptor.ControllerTypeInfo.Namespace?.Split('.').Last() }; } return new[] { actionDescriptor.ControllerName }; } public static IEnumerable GetBaseTypesAndThis(this Type type) { Type? current = type; while (current != null) { yield return current; current = current.BaseType; } } }