#nullable enable
using IOL.GreatOffice.Api.Endpoints;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Controllers;
using Swashbuckle.AspNetCore.SwaggerGen;
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(EndpointBase))) {
return new[] {
actionDescriptor.ControllerTypeInfo.Namespace?.Split('.').Last()
};
}
return new[] {
actionDescriptor.ControllerName
};
}
private static IEnumerable GetBaseTypesAndThis(this Type type) {
var current = type;
while (current != null) {
yield return current;
current = current.BaseType;
}
}
}