diff --git a/Configurations/SimApiDocOptions.cs b/Configurations/SimApiDocOptions.cs index d921caa..2271ad2 100644 --- a/Configurations/SimApiDocOptions.cs +++ b/Configurations/SimApiDocOptions.cs @@ -6,22 +6,22 @@ namespace SimApi.Configurations; /// /// 文档组配置 /// -public class SimApiDocGroupOption +public class SimApiDocGroupOption(string id, string name, string description = "") { /// /// 文档标识 /// - public string Id { get; set; } = null!; + public string Id { get; set; } = id; /// /// 文档名称 /// - public string Name { get; set; } = null!; + public string Name { get; set; } = name; /// /// 文档描述 /// - public string Description { get; set; } = null!; + public string Description { get; set; } = description!; } /// @@ -29,7 +29,7 @@ public class SimApiDocGroupOption /// public class SimApiAuthOption { - public string[] Type { get; set; } = new[] { "SimApiAuth" }; + public string[] Type { get; set; } = ["SimApiAuth"]; public string Description { get; set; } = "认证服务器颁发的AccessToken"; @@ -48,20 +48,15 @@ public class SimApiDocOptions /// /// 文档组配置 /// - public SimApiDocGroupOption[] ApiGroups { get; set; } = new[] - { - new SimApiDocGroupOption - { - Id = "api", - Name = "Api", - Description = "Api接口文档" - } - }; + public SimApiDocGroupOption[] ApiGroups { get; set; } = + [ + new("api", "Api", "Api接口文档") + ]; /// /// 授权配置 /// - public SimApiAuthOption ApiAuth { get; set; } = new SimApiAuthOption(); + public SimApiAuthOption ApiAuth { get; set; } = new(); /// /// 文档页面标题 @@ -71,5 +66,5 @@ public class SimApiDocOptions /// /// 接口支持的调用方式 /// - public SubmitMethod[] SupportedMethod { get; set; } = new[] { SubmitMethod.Post }; + public SubmitMethod[] SupportedMethod { get; set; } = [SubmitMethod.Post]; } \ No newline at end of file diff --git a/SimApiExtensions.cs b/SimApiExtensions.cs index 684d9b3..b8e06bf 100644 --- a/SimApiExtensions.cs +++ b/SimApiExtensions.cs @@ -12,6 +12,7 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.OpenApi.Models; using SimApi.Middlewares; using Microsoft.AspNetCore.HttpOverrides; +using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Logging; using SimApi.Attributes; @@ -128,6 +129,21 @@ public static class SimApiExtensions x.OperationFilter(); } + x.DocInclusionPredicate((docName, apiDesc) => + { + // 获取接口标记的 GroupName(未标记则为 null) + var actionGroupName = apiDesc.ActionDescriptor.EndpointMetadata + .OfType() + .FirstOrDefault()?.GroupName; + + // 情况1:接口未标记任何 GroupName(actionGroupName 为 null) + if (actionGroupName == null) + { + return docName == "api"; // 未分组接口只属于默认分组v1 + } + return docName == actionGroupName; + }); + x.EnableAnnotations(); var haveOauth = false; var oauthFlows = new OpenApiOAuthFlows();