Parcourir la source

新增 appconfig.json.swagger.enableSchemaId启用SchemaId命名空间NamespaceassemblyNameList程序集列表,用于启用SchemaId命名空间,主要用于对接第三方nuget包输入输出实体生成swagger.json可能异常的问题
新增 SchemaIdAttribute特性自定义实体SchemaId,同时支持前缀名+modelName+后缀名

zhontai il y a 1 an
Parent
commit
5348955472

+ 4 - 0
src/hosts/ZhonTai.Host/Configs/appconfig.json

@@ -30,6 +30,10 @@
     "enableOrderTagsDocumentFilter": true,
     //启用枚举属性名
     "enableJsonStringEnumConverter": false,
+    //启用SchemaId命名空间
+    "enableSchemaIdNamespace": false,
+    //程序集列表,用于启用SchemaId命名空间
+    "assemblyNameList": [],
     //路由前缀,如配置微服务文档地址:doc/module/swagger
     "routePrefix": "admin/swagger",
     //地址

+ 34 - 0
src/platform/ZhonTai.Admin/Core/Attributes/SchemaIdAttribute.cs

@@ -0,0 +1,34 @@
+using System;
+
+namespace ZhonTai.Admin.Core.Attributes;
+
+/// <summary>
+/// SchemaId
+/// </summary>
+[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true)]
+public class SchemaIdAttribute : Attribute
+{
+    /// <summary>
+    /// 前缀
+    /// </summary>
+    public string Prefix { get; set; }
+
+    /// <summary>
+    /// 后缀
+    /// </summary>
+    public string Suffix { get; set; }
+
+    /// <summary>
+    /// SchemaId
+    /// </summary>
+    public string SchemaId { get; set; }
+
+    public SchemaIdAttribute()
+    {
+    }
+
+    public SchemaIdAttribute(string schemaId)
+    {
+        SchemaId = schemaId;
+    }
+}

+ 10 - 0
src/platform/ZhonTai.Admin/Core/Configs/AppConfig.cs

@@ -131,6 +131,16 @@ public class SwaggerConfig
     /// </summary>
     public bool EnableJsonStringEnumConverter { get; set; } = false;
 
+    /// <summary>
+    /// 启用SchemaId命名空间
+    /// </summary>
+    public bool EnableSchemaIdNamespace { get; set; } = false;
+
+    /// <summary>
+    /// 程序集列表
+    /// </summary>
+    public string[] AssemblyNameList { get; set; }
+
     private string _RoutePrefix = "swagger";
     /// <summary>
     /// 访问地址

+ 51 - 16
src/platform/ZhonTai.Admin/Core/HostApp.cs

@@ -59,6 +59,7 @@ using Microsoft.AspNetCore.Diagnostics.HealthChecks;
 using Microsoft.Extensions.Caching.Distributed;
 using ZhonTai.Admin.Core.Captcha;
 using NLog;
+using StackExchange.Profiling.Internal;
 
 namespace ZhonTai.Admin.Core;
 
@@ -179,22 +180,6 @@ public class HostApp
         }
     }
 
-    /// <summary>
-    /// 实体类型重命名
-    /// </summary>
-    /// <param name="modelType"></param>
-    /// <returns></returns>
-    private string DefaultSchemaIdSelector(Type modelType)
-    {
-        if (!modelType.IsConstructedGenericType) return modelType.Name.Replace("[]", "Array");
-
-        var prefix = modelType.GetGenericArguments()
-            .Select(DefaultSchemaIdSelector)
-            .Aggregate((previous, current) => previous + current);
-
-        return modelType.Name.Split('`').First() + prefix;
-    }
-
     /// <summary>
     /// 配置服务
     /// </summary>
@@ -357,6 +342,56 @@ public class HostApp
                 });
 
                 options.ResolveConflictingActions(apiDescription => apiDescription.First());
+
+                string DefaultSchemaIdSelector(Type modelType)
+                {
+                    var modelName = modelType.Name;
+                    if (appConfig.Swagger.EnableSchemaIdNamespace)
+                    {
+                        var nameSpaceList = appConfig.Swagger.AssemblyNameList;
+                        if (nameSpaceList?.Length > 0)
+                        {
+                            var nameSpace = modelType.Namespace;
+                            if(nameSpaceList.Where(a => nameSpace.Contains(a)).Any())
+                            {
+                                modelName = modelType.FullName;
+                            }
+                        }
+                        else
+                        {
+                            modelName = modelType.FullName;
+                        }
+                    }
+
+                    if (modelType.IsConstructedGenericType)
+                    {
+                        var prefix = modelType.GetGenericArguments()
+                        .Select(DefaultSchemaIdSelector)
+                        .Aggregate((previous, current) => previous + current);
+
+                        modelName = modelName.Split('`').First() + prefix;
+                    }
+                    else
+                    {
+                        modelName = modelName.Replace("[]", "Array");
+                    }
+
+                    if (modelType.IsDefined(typeof(SchemaIdAttribute)))
+                    {
+                        var swaggerSchemaIdAttribute = modelType.GetCustomAttribute<SchemaIdAttribute>();
+                        if (swaggerSchemaIdAttribute.SchemaId.NotNull())
+                        {
+                            return swaggerSchemaIdAttribute.SchemaId;
+                        }
+                        else
+                        {
+                            return swaggerSchemaIdAttribute.Prefix + modelName + swaggerSchemaIdAttribute.Suffix;
+                        }
+                    }
+
+                    return modelName;
+                }
+
                 options.CustomSchemaIds(modelType => DefaultSchemaIdSelector(modelType));
 
                 //支持多分组

+ 30 - 7
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -44,6 +44,26 @@
             不生成特性
             </summary>
         </member>
+        <member name="T:ZhonTai.Admin.Core.Attributes.SchemaIdAttribute">
+            <summary>
+            SchemaId
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Attributes.SchemaIdAttribute.Prefix">
+            <summary>
+            前缀
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Attributes.SchemaIdAttribute.Suffix">
+            <summary>
+            后缀
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Attributes.SchemaIdAttribute.SchemaId">
+            <summary>
+            SchemaId
+            </summary>
+        </member>
         <member name="T:ZhonTai.Admin.Core.Attributes.SingleInstanceAttribute">
             <summary>
             单例注入
@@ -425,6 +445,16 @@
             启用枚举属性名
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.SwaggerConfig.EnableSchemaIdNamespace">
+            <summary>
+            启用SchemaId命名空间
+            </summary>
+        </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.SwaggerConfig.AssemblyNameList">
+            <summary>
+            程序集列表
+            </summary>
+        </member>
         <member name="P:ZhonTai.Admin.Core.Configs.SwaggerConfig.RoutePrefix">
             <summary>
             访问地址
@@ -1865,13 +1895,6 @@
             </summary>
             <param name="args"></param>
         </member>
-        <member name="M:ZhonTai.Admin.Core.HostApp.DefaultSchemaIdSelector(System.Type)">
-            <summary>
-            实体类型重命名
-            </summary>
-            <param name="modelType"></param>
-            <returns></returns>
-        </member>
         <member name="M:ZhonTai.Admin.Core.HostApp.ConfigureServices(Microsoft.Extensions.DependencyInjection.IServiceCollection,Microsoft.AspNetCore.Hosting.IWebHostEnvironment,Microsoft.Extensions.Configuration.IConfiguration,ZhonTai.Common.Helpers.ConfigHelper,ZhonTai.Admin.Core.Configs.AppConfig)">
             <summary>
             配置服务