Skip to content

Commit

Permalink
提供EndpointGroupName文档分组的支持 #8
Browse files Browse the repository at this point in the history
  • Loading branch information
vipwan committed Oct 15, 2023
1 parent 78bd33d commit 908958a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 8 deletions.
25 changes: 25 additions & 0 deletions Biwen.QuickApi.DemoWeb/Apis/EndpointGroupApi.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@


namespace Biwen.QuickApi.DemoWeb.Apis
{

[QuickApi("endpointgroup")]
[QuickApiSummary("分组测试", "分组测试")]
[EndpointGroupName("group1")]
public class EndpointGroupApi : BaseQuickApi
{
public override Task<EmptyResponse> ExecuteAsync(EmptyRequest request)
{
return Task.FromResult(EmptyResponse.New);
}


public override RouteHandlerBuilder HandlerBuilder(RouteHandlerBuilder builder)
{
builder.WithTags("group");

//builder.WithGroupName("group1");
return base.HandlerBuilder(builder);
}
}
}
1 change: 0 additions & 1 deletion Biwen.QuickApi.DemoWeb/Apis/MiddlewareApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace Biwen.QuickApi.DemoWeb.Apis
{



[QuickApi("middleware")]
public class MiddlewareApi : BaseQuickApi
{
Expand Down
54 changes: 47 additions & 7 deletions Biwen.QuickApi.DemoWeb/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using NSwag;
using System.Text.Json.Serialization;
using System.Text.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;

var builder = WebApplication.CreateBuilder(args);

Expand Down Expand Up @@ -46,15 +42,53 @@
options.DefaultChallengeScheme = "Cookies";
});


#region swagger 文档


//swagger
builder.Services.AddQuickApiDocument(options =>
{
options.UseControllerSummaryAsTagDescription = true;
options.DocumentName = "Quick API ALL";
//options.ApiGroupNames = new[] { };//未指定展示全部Api
options.PostProcess = document =>
{
document.Info = new OpenApiInfo
{
Version = "Quick API ALL",
Title = "Quick API testcase",
Description = "Biwen.QuickApi 测试用例",
TermsOfService = "https://github.com/vipwan",
Contact = new OpenApiContact
{
Name = "欢迎 Star & issue",
Url = "https://github.com/vipwan/Biwen.QuickApi"
},
License = new OpenApiLicense
{
Name = "MIT License",
Url = "https://github.com/vipwan/Biwen.QuickApi/blob/master/LICENSE.txt"
}
};
};
},
new SecurityOptions());

builder.Services.AddQuickApiDocument(options =>
{
options.UseControllerSummaryAsTagDescription = true;
options.DocumentName = "Quick API Admin&Group1";
options.ApiGroupNames = new[] { "admin", "group1" }; //文档分组指定
options.PostProcess = document =>
{
document.Info = new OpenApiInfo
{
Version = "Quick API V1",
Version = "Quick API V2",
Title = "Quick API testcase",
Description = "Biwen.QuickApi 测试用例",
TermsOfService = "https://github.com/vipwan",
Expand All @@ -73,6 +107,11 @@
},
new SecurityOptions());

#endregion




// Add services to the container.
builder.Services.AddScoped<HelloService>();
// keyed services
Expand All @@ -96,12 +135,12 @@




if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
//swagger ui
app.UseQuickApiSwagger();

app.MapGet("/", () => Results.Redirect("/swagger")).ExcludeFromDescription();
}
else
Expand All @@ -121,7 +160,8 @@
var groupAdmin = apis.FirstOrDefault(x => x.Group == "admin");
groupAdmin.RouteGroupBuilder?
.WithTags("authorization") //自定义Tags
//.RequireHost("localhost:5101") //模拟需要指定Host访问接口
//.RequireHost("localhost:5101") //模拟需要指定Host访问接口
.WithGroupName("admin") //自定义EndpointGroupName
;

// Gen方式
Expand Down
4 changes: 4 additions & 0 deletions Biwen.QuickApi.Generator/QuickApiSourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class QuickApiSourceGenerator : IIncrementalGenerator
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.OutputCaching;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.Options;
using Biwen.QuickApi;
using Biwen.QuickApi.Attributes;
Expand Down Expand Up @@ -137,6 +138,9 @@ public static RouteGroupBuilder MapGenQuickApis(this IEndpointRouteBuilder app,
//outputcache
var $4Cache = typeof($3).GetCustomAttribute<OutputCacheAttribute>();
if ($4Cache != null) $4.WithMetadata($4Cache);
//endpointgroup
var $4EndpointgroupAttribute = typeof($3).GetCustomAttribute<EndpointGroupNameAttribute>();
if ($4EndpointgroupAttribute != null) $4.WithMetadata($4EndpointgroupAttribute);
";

#endregion
Expand Down
7 changes: 7 additions & 0 deletions Biwen.QuickApi/ServiceRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,13 @@ public static (string Group, RouteGroupBuilder RouteGroupBuilder)[] MapBiwenQuic
rhBuilder.WithMetadata(outputCacheAttribute);
}

//endpointgroup
var endpointgroupAttribute = apiType.GetCustomAttribute<EndpointGroupNameAttribute>();
if (endpointgroupAttribute != null)
{
rhBuilder.WithMetadata(endpointgroupAttribute);
}

//OpenApi 生成
//var method = apiType.GetMethod("ExecuteAsync")!;
//var parameter = method.GetParameters()[0]!;
Expand Down

0 comments on commit 908958a

Please sign in to comment.