From 88110f536f9c3843ecf5016122e101f8a424af77 Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Sat, 22 Jan 2022 22:43:38 +0100 Subject: Initial commit --- .../Utilities/SwaggerGenOptionsExtensions.cs | 37 ++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/server/Utilities/SwaggerGenOptionsExtensions.cs (limited to 'src/server/Utilities/SwaggerGenOptionsExtensions.cs') 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 +{ + /// + /// 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 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, + }; + } +} -- cgit v1.3