增加Swagger枚举说明支持
This commit is contained in:
parent
c551872c8f
commit
cbdbf24839
22
Falcon.Extend/SwaggerGenOptionsExtend.cs
Normal file
22
Falcon.Extend/SwaggerGenOptionsExtend.cs
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Swashbuckle.AspNetCore.SwaggerGen;
|
||||||
|
|
||||||
|
namespace Falcon.Extend
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// SwaggerGenOptions扩展
|
||||||
|
/// </summary>
|
||||||
|
public static class SwaggerGenOptionsExtend
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 增加xml文件枚举说明支持
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="options">SwaggerGenOptions选项</param>
|
||||||
|
/// <param name="xmlPath">XML文件路径</param>
|
||||||
|
/// <returns>SwaggerGenOptions</returns>
|
||||||
|
public static SwaggerGenOptions AddXmlEnumEnable(this SwaggerGenOptions options,string xmlPath) {
|
||||||
|
options.DocumentFilter<SwaggerXmlEnumFilter>(xmlPath);
|
||||||
|
return options;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -17,7 +17,10 @@ namespace Falcon.Extend
|
|||||||
{
|
{
|
||||||
private readonly XPathNavigator _xmlNavigator;
|
private readonly XPathNavigator _xmlNavigator;
|
||||||
|
|
||||||
public static List<Type> AllTypes { get; set; }
|
/// <summary>
|
||||||
|
/// 存放程序集中的所有枚举类型
|
||||||
|
/// </summary>
|
||||||
|
public static List<Type> AllTypes { get; set; } = null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 通过提供应用程序文档生成枚举说明
|
/// 通过提供应用程序文档生成枚举说明
|
||||||
@ -26,12 +29,18 @@ namespace Falcon.Extend
|
|||||||
public SwaggerXmlEnumFilter(string xmlPath) {
|
public SwaggerXmlEnumFilter(string xmlPath) {
|
||||||
_xmlNavigator = new XPathDocument(xmlPath).CreateNavigator();
|
_xmlNavigator = new XPathDocument(xmlPath).CreateNavigator();
|
||||||
|
|
||||||
if(AllTypes == null) {
|
AllTypes = AllTypes ?? GetEnumTypes();
|
||||||
AllTypes = new List<Type>();
|
}
|
||||||
foreach(var ass in AppDomain.CurrentDomain.GetAssemblies()) {
|
|
||||||
AllTypes.AddRange(ass.GetTypes().Where(m => m.IsEnum));
|
/// <summary>
|
||||||
}
|
/// 初始化程序集中所有枚举类型
|
||||||
|
/// </summary>
|
||||||
|
public virtual List<Type> GetEnumTypes() {
|
||||||
|
var types = new List<Type>();
|
||||||
|
foreach(var ass in AppDomain.CurrentDomain.GetAssemblies()) {
|
||||||
|
types.AddRange(ass.GetTypes().Where(m => m.IsEnum));
|
||||||
}
|
}
|
||||||
|
return types;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Apply(OpenApiDocument swaggerDoc,DocumentFilterContext context) {
|
public void Apply(OpenApiDocument swaggerDoc,DocumentFilterContext context) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user