From dd45cb1b3433bd54a7e16b9af6973f14f2282aa6 Mon Sep 17 00:00:00 2001 From: Engincan VESKE <43685404+EngincanV@users.noreply.github.com> Date: Thu, 16 Feb 2023 11:42:40 +0300 Subject: [PATCH 1/2] Update AbpSwashbuckleDocumentFilter.cs --- .../AbpSwashbuckleDocumentFilter.cs | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs b/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs index 5e86c04189..2b61037487 100644 --- a/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs +++ b/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; @@ -8,7 +9,7 @@ namespace Volo.Abp.Swashbuckle; public class AbpSwashbuckleDocumentFilter : IDocumentFilter { - protected string[] ActionUrlPrefixes = new[] {"Volo."}; + protected virtual string[] ActionUrlPrefixes { get; set; } = new[] { "Volo." }; public virtual void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { @@ -17,7 +18,7 @@ public class AbpSwashbuckleDocumentFilter : IDocumentFilter .Where(actionDescriptor => !string.IsNullOrEmpty(actionDescriptor.DisplayName) && ActionUrlPrefixes.Any(actionUrlPrefix => !actionDescriptor.DisplayName.Contains(actionUrlPrefix))) .DistinctBy(actionDescriptor => actionDescriptor.AttributeRouteInfo?.Template) - .Select(actionDescriptor => actionDescriptor.AttributeRouteInfo?.Template?.EnsureStartsWith('/').Replace("?", "")) + .Select(RemoveRouteParameterConstraints) .Where(actionUrl => !string.IsNullOrEmpty(actionUrl)) .ToList(); @@ -25,4 +26,23 @@ public class AbpSwashbuckleDocumentFilter : IDocumentFilter .Paths .RemoveAll(path => !actionUrls.Contains(path.Key)); } + + protected virtual string RemoveRouteParameterConstraints(ActionDescriptor actionDescriptor) + { + var route = actionDescriptor.AttributeRouteInfo?.Template?.EnsureStartsWith('/').Replace("?", ""); + if (string.IsNullOrWhiteSpace(route)) + { + return route; + } + + var startIndex = route.IndexOf(":", StringComparison.Ordinal); + var endIndex = route.IndexOf("}", StringComparison.Ordinal); + + if (startIndex == -1 || endIndex == -1) + { + return route; + } + + return route.Remove(startIndex, (endIndex - startIndex)); + } } \ No newline at end of file From 2ea659709883e1e13b7c2b7c325283e89696fb47 Mon Sep 17 00:00:00 2001 From: Engincan VESKE <43685404+EngincanV@users.noreply.github.com> Date: Thu, 16 Feb 2023 14:21:26 +0300 Subject: [PATCH 2/2] Support for regex constraints in HideAbpEndpoints method --- .../AbpSwashbuckleDocumentFilter.cs | 24 +++++++++++++------ 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs b/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs index 2b61037487..dd1b9705b8 100644 --- a/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs +++ b/framework/src/Volo.Abp.Swashbuckle/Volo/Abp/Swashbuckle/AbpSwashbuckleDocumentFilter.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Text.RegularExpressions; using Microsoft.AspNetCore.Mvc.Abstractions; using Microsoft.OpenApi.Models; using Swashbuckle.AspNetCore.SwaggerGen; @@ -10,6 +11,8 @@ namespace Volo.Abp.Swashbuckle; public class AbpSwashbuckleDocumentFilter : IDocumentFilter { protected virtual string[] ActionUrlPrefixes { get; set; } = new[] { "Volo." }; + + protected virtual string RegexConstraintPattern => @":regex\(([^()]*)\)"; public virtual void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) { @@ -34,15 +37,22 @@ public class AbpSwashbuckleDocumentFilter : IDocumentFilter { return route; } - - var startIndex = route.IndexOf(":", StringComparison.Ordinal); - var endIndex = route.IndexOf("}", StringComparison.Ordinal); - - if (startIndex == -1 || endIndex == -1) + + route = Regex.Replace(route, RegexConstraintPattern, ""); + + while (route.Contains(':')) { - return route; + var startIndex = route.IndexOf(":", StringComparison.Ordinal); + var endIndex = route.IndexOf("}", startIndex); + + if (endIndex == -1) + { + break; + } + + route = route.Remove(startIndex, (endIndex - startIndex)); } - return route.Remove(startIndex, (endIndex - startIndex)); + return route; } } \ No newline at end of file