namespace IOL.BookmarkThing.Server.Utilities;
public static class SwaggerGenOptionsExtensions
{
///
/// Updates Swagger document to support ApiEndpoints.
/// - Replaces action Tag with [namespace]
///
public static void UseApiEndpoints(this SwaggerGenOptions options) {
options.TagActionsBy(EndpointNamespaceOrDefault);
}
private static IEnumerable GetBaseTypesAndThis(this Type type) {
var current = type;
while (current != null) {
yield return current;
current = current.BaseType;
}
}
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(BaseV1Route))) {
return new[] {
actionDescriptor.ControllerTypeInfo.Namespace?.Split('.').Last(),
};
}
return new[] {
actionDescriptor.ControllerName,
};
}
}