From cb8b1c5de9527a7dd2539606f7e38560613d7927 Mon Sep 17 00:00:00 2001 From: lihanbo Date: Sat, 2 Nov 2024 10:37:04 +0800 Subject: [PATCH] =?UTF-8?q?105040=20Update=20Swagger=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E5=88=86=E7=BB=84=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LFlow.Base/Base.xml | 217 ++++++++++++++++++ LFlow.Base/LFlow.Base.csproj | 2 + LFlow.Base/Program.cs | 31 ++- .../ApiExplorerGroupPerVersionConvention.cs | 25 ++ .../LFlow.OnlineManegement.csproj | 2 + LFlow.Permission/LFlow.Permission.csproj | 3 + LFlow.Role/LFlow.Role.csproj | 2 + .../LFlow.UserManagement.csproj | 2 + .../LFlow.VersionManagement.csproj | 2 + 9 files changed, 284 insertions(+), 2 deletions(-) create mode 100644 LFlow.Base/Base.xml create mode 100644 LFlow.Base/Utils/ApiExplorerGroupPerVersionConvention.cs diff --git a/LFlow.Base/Base.xml b/LFlow.Base/Base.xml new file mode 100644 index 0000000..aa64fcc --- /dev/null +++ b/LFlow.Base/Base.xml @@ -0,0 +1,217 @@ + + + + LFlow.Base + + + + + 程序对象 + + + + + 默认的增删改查仓储 + + + + + + + 根据ID删除对象 + + + + + + + + 根据ID获取对象 + + + + + + + 批量查询(分页) + + + + + + + + + 报错或是更新对象 + + + + + + + + 搜索 + + + + + + + 查找ID + + + + + + + 基础控制器 + + Route("api/[controller]/[action]") ApiController + + + + 控制器接口 + + + + + 数据模型接口 Repo层用 + + + + + ID + + + + + 模型顶层接口 Service层用 + + + + + 模块接口 + + + + + 通用的仓库对象接口 + + 数据模型 + 主键 + + + + 获取单个对象 + + + + + + + 删除单个对象 + + + + + + + 保存与更新 + + + + + + + 获取所有对象列表(默认分页) + + + + + + + + 根据条件搜索对象列表 + + + + + + + 根据条件搜索主键列表 + + + + + + + 服务接口 + + + + + + 从子文件夹中加载DLL + + + + + 控制器模型约定 + + + + + 配置 + + + + + + 返回结果包装 + + + + + + 将一个对象映射到另一个对象 + + + + + + + + 将IModel对象转换为where条件 + + + + + + + 返回结果包装 (分页) + + + + + + 注册所有模型 + + + + + + + 注册所有服务 + + + + + + + 注册所有数据仓库 + + + + + + diff --git a/LFlow.Base/LFlow.Base.csproj b/LFlow.Base/LFlow.Base.csproj index f72642b..0fd003d 100644 --- a/LFlow.Base/LFlow.Base.csproj +++ b/LFlow.Base/LFlow.Base.csproj @@ -8,6 +8,8 @@ ../LFlow_Bin/ false false + True + Base.xml diff --git a/LFlow.Base/Program.cs b/LFlow.Base/Program.cs index 13b7716..0b0c195 100644 --- a/LFlow.Base/Program.cs +++ b/LFlow.Base/Program.cs @@ -46,16 +46,19 @@ public static class Program builder.Services.AddEndpointsApiExplorer(); - builder.Services.AddSwaggerGen(); builder.Services.AddHttpContextAccessor(); Log.Logger.Information("ConfigureSqlSugar"); builder.Services.ConfigureSqlSugar(); - builder.Services.AddControllers(); + builder.Services.AddControllers(c => + { + c.Conventions.Add(new ApiExplorerGroupPerVersionConvention()); + }); builder.Services.AddSwaggerGen(u => { + u.SwaggerDoc("v1", new Microsoft.OpenApi.Models.OpenApiInfo { Version = "Ver.1", @@ -67,6 +70,22 @@ public static class Program Email = "noemail@ling.chat" } }); + u.IncludeXmlComments("Base.xml", true); + // 获取自定义的xml目录 + var xmlPath = Path.Combine(System.AppContext.BaseDirectory, "Services"); + DirectoryInfo dir = new(xmlPath); + // 获取目录下的所有xml文件,并设置为swagger文档包含文件 + dir.GetFiles("*.xml").ToList().ForEach(f => + { + u.IncludeXmlComments(f.FullName, true); + + u.SwaggerDoc(Path.GetFileNameWithoutExtension(f.Name), new Microsoft.OpenApi.Models.OpenApiInfo + { + Version = "Ver.1", + Title = Path.GetFileNameWithoutExtension(f.Name), + Description = "LFlow api test and document", + }); + }); }); // 配置跨域策略 builder.Services.AddCors(options => @@ -118,6 +137,14 @@ public static class Program app.UseSwaggerUI(u => { u.DocumentTitle = "LFlow"; + var xmlPath = Path.Combine(System.AppContext.BaseDirectory, "Services"); + DirectoryInfo dir = new(xmlPath); + // 获取目录下的所有xml文件,并设置为swagger文档包含文件 + dir.GetFiles("*.xml").ToList().ForEach(f => + { + var progName = Path.GetFileNameWithoutExtension(f.Name); + u.SwaggerEndpoint($"/swagger/{progName}/swagger.json", progName); + }); }); } // 在启动后调用Sqlsugar进行CodeFirst diff --git a/LFlow.Base/Utils/ApiExplorerGroupPerVersionConvention.cs b/LFlow.Base/Utils/ApiExplorerGroupPerVersionConvention.cs new file mode 100644 index 0000000..a822ca4 --- /dev/null +++ b/LFlow.Base/Utils/ApiExplorerGroupPerVersionConvention.cs @@ -0,0 +1,25 @@ +using Microsoft.AspNetCore.Mvc.ApplicationModels; +using Serilog; + +namespace LFlow.Base.Utils +{ + /// + /// 控制器模型约定 + /// + public class ApiExplorerGroupPerVersionConvention : IControllerModelConvention + { + /// + /// 配置 + /// + /// + public void Apply(ControllerModel controller) + { + foreach (var action in controller.Actions) + { + action.ApiExplorer.GroupName = controller.ControllerName; + action.ApiExplorer.IsVisible = true; + Log.Logger.Information($"ApiExplorerGroup added -> {controller.ControllerName} / {action.ActionName}"); + } + } + } +} diff --git a/LFlow.OnlineManegement/LFlow.OnlineManegement.csproj b/LFlow.OnlineManegement/LFlow.OnlineManegement.csproj index fe015fa..74032a4 100644 --- a/LFlow.OnlineManegement/LFlow.OnlineManegement.csproj +++ b/LFlow.OnlineManegement/LFlow.OnlineManegement.csproj @@ -8,6 +8,8 @@ false false true + True + ..\LFlow_Bin\Services\OnlineManegement.xml diff --git a/LFlow.Permission/LFlow.Permission.csproj b/LFlow.Permission/LFlow.Permission.csproj index d04a79d..e6b6008 100644 --- a/LFlow.Permission/LFlow.Permission.csproj +++ b/LFlow.Permission/LFlow.Permission.csproj @@ -13,6 +13,9 @@ false true true + + True + ..\LFlow_Bin\Services\Permission.xml \ No newline at end of file diff --git a/LFlow.Role/LFlow.Role.csproj b/LFlow.Role/LFlow.Role.csproj index 7a46b84..cb46e0f 100644 --- a/LFlow.Role/LFlow.Role.csproj +++ b/LFlow.Role/LFlow.Role.csproj @@ -9,6 +9,8 @@ false true true + True + ..\LFlow_Bin\Services\Role.xml diff --git a/LFlow.UserManagement/LFlow.UserManagement.csproj b/LFlow.UserManagement/LFlow.UserManagement.csproj index 1f235b1..19517a9 100644 --- a/LFlow.UserManagement/LFlow.UserManagement.csproj +++ b/LFlow.UserManagement/LFlow.UserManagement.csproj @@ -8,6 +8,8 @@ false false true + True + ..\LFlow_Bin\Services\UserManagement.xml diff --git a/LFlow.VersionManagement/LFlow.VersionManagement.csproj b/LFlow.VersionManagement/LFlow.VersionManagement.csproj index e9187a5..5e972bd 100644 --- a/LFlow.VersionManagement/LFlow.VersionManagement.csproj +++ b/LFlow.VersionManagement/LFlow.VersionManagement.csproj @@ -8,6 +8,8 @@ false false true + True + ..\LFlow_Bin\Services\VersionManagement.xml