SwaggerExtension.cs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // <author></author>
  2. // <date></date>
  3. // <description></description>
  4. using ERP.Framework.Constants;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Microsoft.OpenApi.Models;
  7. using Swashbuckle.AspNetCore.Annotations;
  8. using Swashbuckle.AspNetCore.SwaggerGen;
  9. namespace ERP.Framework.Extensions
  10. {
  11. public static class SwaggerExtension
  12. {
  13. public static void AddSwagger(this IServiceCollection services)
  14. {
  15. services.AddSwaggerGen(options =>
  16. {
  17. var openApiSecurityScheme = new OpenApiSecurityScheme
  18. {
  19. Reference = new OpenApiReference
  20. {
  21. Type = ReferenceType.SecurityScheme,
  22. Id = AuthConstant.HEADER
  23. },
  24. Scheme = "oauth2",
  25. Name = AuthConstant.HEADER,
  26. In = ParameterLocation.Header,
  27. Type = SecuritySchemeType.ApiKey
  28. };
  29. var securityRequirement = new OpenApiSecurityRequirement { [openApiSecurityScheme] = new List<string>() };
  30. // 设置 Swagger UI 的标题和版本 (描述,联系人,许可证)
  31. options.SwaggerDoc("v1", new OpenApiInfo { Title = "ERP", Version = "v1" });
  32. // 安全方案
  33. options.AddSecurityDefinition(AuthConstant.HEADER, openApiSecurityScheme);
  34. // 请求头
  35. options.AddSecurityRequirement(securityRequirement);
  36. // 自定义逻辑
  37. options.OperationFilter<SwaggerDescriptionFilter>();
  38. options.OperationFilter<SwaggerSummaryFilter>();
  39. });
  40. }
  41. private class PrefixDocumentFilter : IDocumentFilter
  42. {
  43. private readonly string _prefix;
  44. public PrefixDocumentFilter(string prefix)
  45. {
  46. _prefix = prefix;
  47. }
  48. public void Apply(OpenApiDocument swaggerDoc, DocumentFilterContext context)
  49. {
  50. var paths = new OpenApiPaths();
  51. foreach (var path in swaggerDoc.Paths)
  52. {
  53. paths.Add(_prefix + path.Key, path.Value);
  54. }
  55. swaggerDoc.Paths = paths;
  56. }
  57. }
  58. private class SwaggerDescriptionFilter : IOperationFilter
  59. {
  60. public void Apply(OpenApiOperation operation, OperationFilterContext context)
  61. {
  62. var attribute = context.MethodInfo
  63. .GetCustomAttributes(typeof(SwaggerOperationAttribute), false)
  64. .FirstOrDefault() as SwaggerOperationAttribute;
  65. if (attribute != null && !string.IsNullOrEmpty(attribute.Description))
  66. {
  67. operation.Description = attribute.Description;
  68. }
  69. }
  70. }
  71. public class SwaggerSummaryFilter : IOperationFilter
  72. {
  73. public void Apply(OpenApiOperation operation, OperationFilterContext context)
  74. {
  75. var attribute = context.MethodInfo
  76. .GetCustomAttributes(typeof(SwaggerOperationAttribute), false)
  77. .FirstOrDefault() as SwaggerOperationAttribute;
  78. if (attribute != null && !string.IsNullOrEmpty(attribute.Summary))
  79. {
  80. operation.Summary = attribute.Summary;
  81. }
  82. }
  83. }
  84. }
  85. }