diff options
Diffstat (limited to 'src/server/Utilities/SwaggerGenOptionsExtensions.cs')
| -rw-r--r-- | src/server/Utilities/SwaggerGenOptionsExtensions.cs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/server/Utilities/SwaggerGenOptionsExtensions.cs b/src/server/Utilities/SwaggerGenOptionsExtensions.cs new file mode 100644 index 0000000..0203f77 --- /dev/null +++ b/src/server/Utilities/SwaggerGenOptionsExtensions.cs @@ -0,0 +1,37 @@ +namespace IOL.BookmarkThing.Server.Utilities; + +public static class SwaggerGenOptionsExtensions +{ + /// <summary> + /// Updates Swagger document to support ApiEndpoints.<br/><br/> + /// For controllers inherited from <see cref="BaseRoute"/>:<br/> + /// - Replaces action Tag with <c>[namespace]</c><br/> + /// </summary> + public static void UseApiEndpoints(this SwaggerGenOptions options) { + options.TagActionsBy(EndpointNamespaceOrDefault); + } + + private static IEnumerable<Type> GetBaseTypesAndThis(this Type type) { + var current = type; + while (current != null) { + yield return current; + current = current.BaseType; + } + } + + private static IList<string> 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, + }; + } +} |
