From 7f162ab11684a2afddd2773c476b8df0248c6849 Mon Sep 17 00:00:00 2001 From: sunpengfei <i@angelzzz.com> Date: 星期五, 01 八月 2025 15:54:32 +0800 Subject: [PATCH] feat:文档开发 --- FlexJobApi.User.Application/Menus/Queries/MenuAllQueryHandler.cs | 4 + FlexJobApi.Core/settings.json | 3 - FlexJobApi.Core/FlexJobApiCoreStartup.cs | 3 + FlexJobApi.Core/Utils/SwaggerUtils/CustomOperationIdFilter.cs | 2 FlexJobApi.Core/Utils/SwaggerUtils/EnumSchemaFilter.cs | 40 ++++++++++++++++++++ FlexJobApi.Core/Utils/XmlDocUtils/XmlDocUtils.cs | 34 ++++++++--------- 6 files changed, 63 insertions(+), 23 deletions(-) diff --git a/FlexJobApi.Core/FlexJobApiCoreStartup.cs b/FlexJobApi.Core/FlexJobApiCoreStartup.cs index 2c53b2d..43e850c 100644 --- a/FlexJobApi.Core/FlexJobApiCoreStartup.cs +++ b/FlexJobApi.Core/FlexJobApiCoreStartup.cs @@ -55,7 +55,8 @@ services.AddSpecificationDocuments(options => { - options.OperationFilter<FurionCustomOperationIdFilter>(); + options.OperationFilter<CustomOperationIdFilter>(); + options.SchemaFilter<EnumSchemaFilter>(); }); services.AddControllers() diff --git a/FlexJobApi.Core/Utils/SwaggerUtils/FurionCustomOperationIdFilter.cs b/FlexJobApi.Core/Utils/SwaggerUtils/CustomOperationIdFilter.cs similarity index 93% rename from FlexJobApi.Core/Utils/SwaggerUtils/FurionCustomOperationIdFilter.cs rename to FlexJobApi.Core/Utils/SwaggerUtils/CustomOperationIdFilter.cs index 2dbfe8e..e6701e4 100644 --- a/FlexJobApi.Core/Utils/SwaggerUtils/FurionCustomOperationIdFilter.cs +++ b/FlexJobApi.Core/Utils/SwaggerUtils/CustomOperationIdFilter.cs @@ -9,7 +9,7 @@ namespace FlexJobApi.Core { - public class FurionCustomOperationIdFilter : IOperationFilter + public class CustomOperationIdFilter : IOperationFilter { public void Apply(OpenApiOperation operation, OperationFilterContext context) { diff --git a/FlexJobApi.Core/Utils/SwaggerUtils/EnumSchemaFilter.cs b/FlexJobApi.Core/Utils/SwaggerUtils/EnumSchemaFilter.cs new file mode 100644 index 0000000..31491dd --- /dev/null +++ b/FlexJobApi.Core/Utils/SwaggerUtils/EnumSchemaFilter.cs @@ -0,0 +1,40 @@ +锘縰sing Microsoft.OpenApi.Any; +using Microsoft.OpenApi.Models; +using Swashbuckle.AspNetCore.SwaggerGen; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace FlexJobApi.Core +{ + public class EnumSchemaFilter : ISchemaFilter + { + public void Apply(OpenApiSchema schema, SchemaFilterContext context) + { + if (context.Type.IsEnum) + { + // 鏋氫妇绫诲瀷鐨勬弿杩帮紙鏉ヨ嚜鏋氫妇鏈韩鐨勬敞閲婏級 + schema.Description ??= GetEnumDescription(context.Type); + } + } + + // 鑾峰彇鏋氫妇绫诲瀷鐨勬敞閲� + private string GetEnumDescription(Type enumType) + { + var xmlDoc = XmlDocUtils.GetXmlDocAsync().Result; + var member = XmlDocUtils.GetXmlDocMemberAsync(enumType, xmlDoc).Result; + var description = member?.Summary ?? ""; + foreach (var enumValue in Enum.GetValues(enumType)) + { + var enumMember = enumType.GetMember(enumValue.ToString()).First(); + var enumXmlDocMember = XmlDocUtils.GetXmlDocMemberAsync(enumMember, xmlDoc).Result; + description += $"{enumValue}-{enumXmlDocMember?.Summary} "; + } + return description; + } + } +} diff --git a/FlexJobApi.Core/Utils/XmlDocUtils/XmlDocUtils.cs b/FlexJobApi.Core/Utils/XmlDocUtils/XmlDocUtils.cs index bddb8ce..ea7cf5f 100644 --- a/FlexJobApi.Core/Utils/XmlDocUtils/XmlDocUtils.cs +++ b/FlexJobApi.Core/Utils/XmlDocUtils/XmlDocUtils.cs @@ -24,6 +24,8 @@ { private const string cacheKey = "XmlDoc"; + public static XmlDoc XmlDoc { get; set; } + /// <summary> /// 鐢熸垚娉ㄩ噴鏂囨。 /// </summary> @@ -75,6 +77,8 @@ } } + XmlDoc = xmlDoc; + if (xmlDoc.Members.Any()) { var json = JsonConvert.SerializeObject(xmlDoc); @@ -90,17 +94,19 @@ /// <returns></returns> public static async Task<XmlDoc> GetXmlDocAsync() { - XmlDoc xmlDoc = null; - var json = await App.GetService<IDistributedCache>().GetStringAsync(cacheKey); - if (!string.IsNullOrEmpty(json)) + if (XmlDoc == null) { - xmlDoc = JsonConvert.DeserializeObject<XmlDoc>(json); + var json = await App.GetService<IDistributedCache>().GetStringAsync(cacheKey); + if (!string.IsNullOrEmpty(json)) + { + XmlDoc = JsonConvert.DeserializeObject<XmlDoc>(json); + } + if (XmlDoc == null) + { + XmlDoc = await BuildXmlDocAsync(); + } } - if (xmlDoc == null) - { - xmlDoc = await BuildXmlDocAsync(); - } - return xmlDoc; + return XmlDoc; } /// <summary> @@ -113,15 +119,7 @@ { if (xmlDoc == null) { - var json = await App.GetService<IDistributedCache>().GetStringAsync(cacheKey); - if (!string.IsNullOrEmpty(json)) - { - xmlDoc = JsonConvert.DeserializeObject<XmlDoc>(json); - } - } - if (xmlDoc == null) - { - xmlDoc = await BuildXmlDocAsync(); + xmlDoc = await GetXmlDocAsync(); } if (xmlDoc != null && memberInfo != null) { diff --git a/FlexJobApi.Core/settings.json b/FlexJobApi.Core/settings.json index 2809bcb..05ffed3 100644 --- a/FlexJobApi.Core/settings.json +++ b/FlexJobApi.Core/settings.json @@ -11,8 +11,7 @@ "Name": "瀛欓箯椋�" } } - ], - "EnumToNumber": true + ] }, "CorsAccessorSettings": { "WithExposedHeaders": [ diff --git a/FlexJobApi.User.Application/Menus/Queries/MenuAllQueryHandler.cs b/FlexJobApi.User.Application/Menus/Queries/MenuAllQueryHandler.cs index 28eb804..d2bba62 100644 --- a/FlexJobApi.User.Application/Menus/Queries/MenuAllQueryHandler.cs +++ b/FlexJobApi.User.Application/Menus/Queries/MenuAllQueryHandler.cs @@ -26,6 +26,8 @@ public async Task<MenuAllCallback> Handle(MenuAllQuery request, CancellationToken cancellationToken) { + var config = new TypeAdapterConfig(); + config.Default.MaxDepth(10); var callback = new MenuAllCallback(); callback.Items = await menuRep.AsQueryable() .Include(it => it.Childrens.Where(c => @@ -37,7 +39,7 @@ it.Type == EnumMenuType.Menu || it.Type == EnumMenuType.Page || it.Type == EnumMenuType.Modal) - .ProjectToType<MenuAllCallbackItem>() + .ProjectToType<MenuAllCallbackItem>(config) .ToListAsync(cancellationToken); return callback; } -- Gitblit v1.9.1