Skip to content

Commit cb89a1a

Browse files
committed
doc only appear in current group
1 parent 462bce5 commit cb89a1a

2 files changed

Lines changed: 27 additions & 16 deletions

File tree

Configurations/SimApiDocOptions.cs

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,30 @@ namespace SimApi.Configurations;
66
/// <summary>
77
/// 文档组配置
88
/// </summary>
9-
public class SimApiDocGroupOption
9+
public class SimApiDocGroupOption(string id, string name, string description = "")
1010
{
1111
/// <summary>
1212
/// 文档标识
1313
/// </summary>
14-
public string Id { get; set; } = null!;
14+
public string Id { get; set; } = id;
1515

1616
/// <summary>
1717
/// 文档名称
1818
/// </summary>
19-
public string Name { get; set; } = null!;
19+
public string Name { get; set; } = name;
2020

2121
/// <summary>
2222
/// 文档描述
2323
/// </summary>
24-
public string Description { get; set; } = null!;
24+
public string Description { get; set; } = description!;
2525
}
2626

2727
/// <summary>
2828
/// 授权配置, Type支持 "SimApiAuth","ClientCredentials","Implicit","AuthorizationCode"
2929
/// </summary>
3030
public class SimApiAuthOption
3131
{
32-
public string[] Type { get; set; } = new[] { "SimApiAuth" };
32+
public string[] Type { get; set; } = ["SimApiAuth"];
3333

3434
public string Description { get; set; } = "认证服务器颁发的AccessToken";
3535

@@ -48,20 +48,15 @@ public class SimApiDocOptions
4848
/// <summary>
4949
/// 文档组配置
5050
/// </summary>
51-
public SimApiDocGroupOption[] ApiGroups { get; set; } = new[]
52-
{
53-
new SimApiDocGroupOption
54-
{
55-
Id = "api",
56-
Name = "Api",
57-
Description = "Api接口文档"
58-
}
59-
};
51+
public SimApiDocGroupOption[] ApiGroups { get; set; } =
52+
[
53+
new("api", "Api", "Api接口文档")
54+
];
6055

6156
/// <summary>
6257
/// 授权配置
6358
/// </summary>
64-
public SimApiAuthOption ApiAuth { get; set; } = new SimApiAuthOption();
59+
public SimApiAuthOption ApiAuth { get; set; } = new();
6560

6661
/// <summary>
6762
/// 文档页面标题
@@ -71,5 +66,5 @@ public class SimApiDocOptions
7166
/// <summary>
7267
/// 接口支持的调用方式
7368
/// </summary>
74-
public SubmitMethod[] SupportedMethod { get; set; } = new[] { SubmitMethod.Post };
69+
public SubmitMethod[] SupportedMethod { get; set; } = [SubmitMethod.Post];
7570
}

SimApiExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Microsoft.OpenApi.Models;
1313
using SimApi.Middlewares;
1414
using Microsoft.AspNetCore.HttpOverrides;
15+
using Microsoft.AspNetCore.Mvc;
1516
using Microsoft.Extensions.Hosting;
1617
using Microsoft.Extensions.Logging;
1718
using SimApi.Attributes;
@@ -128,6 +129,21 @@ public static IServiceCollection AddSimApi(this IServiceCollection builder,
128129
x.OperationFilter<SimApiAuthOperationFilter>();
129130
}
130131

132+
x.DocInclusionPredicate((docName, apiDesc) =>
133+
{
134+
// 获取接口标记的 GroupName(未标记则为 null)
135+
var actionGroupName = apiDesc.ActionDescriptor.EndpointMetadata
136+
.OfType<ApiExplorerSettingsAttribute>()
137+
.FirstOrDefault()?.GroupName;
138+
139+
// 情况1:接口未标记任何 GroupName(actionGroupName 为 null)
140+
if (actionGroupName == null)
141+
{
142+
return docName == "api"; // 未分组接口只属于默认分组v1
143+
}
144+
return docName == actionGroupName;
145+
});
146+
131147
x.EnableAnnotations();
132148
var haveOauth = false;
133149
var oauthFlows = new OpenApiOAuthFlows();

0 commit comments

Comments
 (0)