5 changed files with 60 additions and 20 deletions
@ -0,0 +1,41 @@
|
||||
using Microsoft.AspNetCore.Mvc.ApiExplorer; |
||||
using Microsoft.OpenApi.Models; |
||||
using Swashbuckle.AspNetCore.SwaggerGen; |
||||
using System; |
||||
using System.Collections.Generic; |
||||
using System.Linq; |
||||
using System.Reflection; |
||||
using System.Text; |
||||
using System.Threading.Tasks; |
||||
|
||||
namespace Elight.Utility |
||||
{ |
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] |
||||
public class HiddenApiAttribute : System.Attribute |
||||
{ |
||||
} |
||||
|
||||
public class HiddenApiFilter : IDocumentFilter |
||||
{ |
||||
public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context) |
||||
{ |
||||
foreach (ApiDescription apiDescription in context.ApiDescriptions) |
||||
{ |
||||
if (apiDescription.TryGetMethodInfo(out MethodInfo method)) |
||||
{ |
||||
if (method.ReflectedType.CustomAttributes.Any(t => t.AttributeType == typeof(HiddenApiAttribute)) |
||||
|| method.CustomAttributes.Any(t => t.AttributeType == typeof(HiddenApiAttribute))) |
||||
{ |
||||
string key = "/" + apiDescription.RelativePath; |
||||
if (key.Contains("?")) |
||||
{ |
||||
int idx = key.IndexOf("?", System.StringComparison.Ordinal); |
||||
key = key.Substring(0, idx); |
||||
} |
||||
swaggerDoc.Paths.Remove(key); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
Loading…
Reference in new issue