|
|
@ -1,6 +1,8 @@ |
|
|
using System; |
|
|
using System; |
|
|
using System.Collections.Generic; |
|
|
using System.Collections.Generic; |
|
|
using System.Linq; |
|
|
using System.Linq; |
|
|
|
|
|
using System.Text.RegularExpressions; |
|
|
|
|
|
using Microsoft.AspNetCore.Mvc.Abstractions; |
|
|
using Microsoft.OpenApi.Models; |
|
|
using Microsoft.OpenApi.Models; |
|
|
using Swashbuckle.AspNetCore.SwaggerGen; |
|
|
using Swashbuckle.AspNetCore.SwaggerGen; |
|
|
|
|
|
|
|
|
@ -8,7 +10,9 @@ namespace Volo.Abp.Swashbuckle; |
|
|
|
|
|
|
|
|
public class AbpSwashbuckleDocumentFilter : IDocumentFilter |
|
|
public class AbpSwashbuckleDocumentFilter : IDocumentFilter |
|
|
{ |
|
|
{ |
|
|
protected string[] ActionUrlPrefixes = new[] {"Volo."}; |
|
|
protected virtual string[] ActionUrlPrefixes { get; set; } = new[] { "Volo." }; |
|
|
|
|
|
|
|
|
|
|
|
protected virtual string RegexConstraintPattern => @":regex\(([^()]*)\)"; |
|
|
|
|
|
|
|
|
public virtual void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) |
|
|
public virtual void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) |
|
|
{ |
|
|
{ |
|
|
@ -17,7 +21,7 @@ public class AbpSwashbuckleDocumentFilter : IDocumentFilter |
|
|
.Where(actionDescriptor => !string.IsNullOrEmpty(actionDescriptor.DisplayName) && |
|
|
.Where(actionDescriptor => !string.IsNullOrEmpty(actionDescriptor.DisplayName) && |
|
|
ActionUrlPrefixes.Any(actionUrlPrefix => !actionDescriptor.DisplayName.Contains(actionUrlPrefix))) |
|
|
ActionUrlPrefixes.Any(actionUrlPrefix => !actionDescriptor.DisplayName.Contains(actionUrlPrefix))) |
|
|
.DistinctBy(actionDescriptor => actionDescriptor.AttributeRouteInfo?.Template) |
|
|
.DistinctBy(actionDescriptor => actionDescriptor.AttributeRouteInfo?.Template) |
|
|
.Select(actionDescriptor => actionDescriptor.AttributeRouteInfo?.Template?.EnsureStartsWith('/').Replace("?", "")) |
|
|
.Select(RemoveRouteParameterConstraints) |
|
|
.Where(actionUrl => !string.IsNullOrEmpty(actionUrl)) |
|
|
.Where(actionUrl => !string.IsNullOrEmpty(actionUrl)) |
|
|
.ToList(); |
|
|
.ToList(); |
|
|
|
|
|
|
|
|
@ -25,4 +29,30 @@ public class AbpSwashbuckleDocumentFilter : IDocumentFilter |
|
|
.Paths |
|
|
.Paths |
|
|
.RemoveAll(path => !actionUrls.Contains(path.Key)); |
|
|
.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; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
route = Regex.Replace(route, RegexConstraintPattern, ""); |
|
|
|
|
|
|
|
|
|
|
|
while (route.Contains(':')) |
|
|
|
|
|
{ |
|
|
|
|
|
var startIndex = route.IndexOf(":", StringComparison.Ordinal); |
|
|
|
|
|
var endIndex = route.IndexOf("}", startIndex); |
|
|
|
|
|
|
|
|
|
|
|
if (endIndex == -1) |
|
|
|
|
|
{ |
|
|
|
|
|
break; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
route = route.Remove(startIndex, (endIndex - startIndex)); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return route; |
|
|
|
|
|
} |
|
|
} |
|
|
} |