SwaggerApi.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435
  1. 
  2. using Microsoft.AspNetCore.Mvc.ApiExplorer;
  3. namespace OASystem.API
  4. {
  5. public class SwaggerApi : IDocumentFilter
  6. {
  7. [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
  8. public class HideApiAttribute : System.Attribute { }
  9. public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
  10. {
  11. foreach (ApiDescription description in context.ApiDescriptions)
  12. {
  13. if (description.TryGetMethodInfo(out MethodInfo method))
  14. {
  15. if (method.ReflectedType.CustomAttributes.Any(t => t.AttributeType == typeof(HideApiAttribute))
  16. || method.CustomAttributes.Any(t => t.AttributeType == typeof(HideApiAttribute))
  17. )
  18. {
  19. string key = "/" + description.RelativePath;
  20. if (key.Contains("?"))
  21. {
  22. int idx = key.IndexOf("?", System.StringComparison.Ordinal);
  23. key = key.Substring(0, idx);
  24. }
  25. swaggerDoc.Paths.Remove(key);
  26. }
  27. }
  28. }
  29. }
  30. }
  31. }